一、安装
我是在window下安装的memcached,选择的是1.4.4版本,安装起来简单点,安装教程链接。
二、MemcachedClient客户端API使用
使用MemcachedClient连接memcached,默认端口为11211.
2.1 Java连接memcached
import java.io.IOException;
import java.net.InetSocketAddress;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException
{
// 本地连接 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessful.");
// 关闭连接
mcc.shutdown();
}
}
2.2 set插入数据
set的方法签名为,第二个参数是过期时间,单位是秒
public Future<Boolean> set(String key, int exp, Object o)
重复的set操作,会覆盖之前的值。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException
{
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
Future<Boolean> set = mcc.set("key1", 10, "hello");
//查看存储状态
System.out.println(set.get());
mcc.set("key2",2,4);
System.out.println(mcc.get("key1"));
int value = (int) mcc.get("key2");
System.out.println(value);
mcc.set("key1", 10, "welcome");
System.out.println(mcc.get("key1"));
mcc.shutdown();
}
}
输出结果为:
true
hello
4
welcome
2.3 add添加数据
当内存中已经有key存在,则会添加失败。如果内存中key不存在,则可以添加成功。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException
{
// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
// 添加数据
Future fo = mcc.set("key", 900, "hello");
// 打印状态
System.out.println(fo.get());
// 输出
System.out.println(mcc.get("key"));
// 添加
fo = mcc.add("key", 900, "memcached");
// 打印状态
System.out.println("add status:" + fo.get());
// 添加新key
fo = mcc.add("key3", 900, "key3");
// 打印状态
System.out.println("add status:" + fo.get());
// 输出
System.out.println(mcc.get("key3"));
// 关闭连接
mcc.shutdown();
}
}
输出结果为:
true
hello
add status:false
add status:true
key3
2.4 replace替换原有的key值
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException
{
//连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessful.");
// 添加第一个 key=》value 对
Future fo = mcc.set("key", 900, "1111");
// 输出执行 add 方法后的状态
System.out.println(fo.get());
// 获取键对应的值
System.out.println(mcc.get("key"));
// 添加新的 key
fo = mcc.replace("key", 900, "2222");
// 输出执行 set 方法后的状态
System.out.println(fo.get());
// 获取键对应的值
System.out.println(mcc.get("key"));
// 关闭连接
mcc.shutdown();
}
}
输出结果为:
true
1111
true
2222
2.5 append 和prepend
append在原有的key的value的末尾追加值。
prepend在原有的value的头位置添加值。
如果key不存在,则追加失败
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException
{
//连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessful.");
// 添加第一个 key=》value 对
mcc.set("key", 900, "1111");
mcc.append(1, "key", "append");
System.out.println(mcc.get("key"));
mcc.prepend(1, "key", "prepend");
System.out.println(mcc.get("key"));
// 关闭连接
mcc.shutdown();
}
}
2.6 CAS操作(重要)
import net.spy.memcached.CASResponse;
import net.spy.memcached.CASValue;
import net.spy.memcached.MemcachedClient;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException
{
// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
// 添加数据
Future fo = mcc.set("runoob", 900, "Free Education");
// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());
// 使用 get 方法获取数据
System.out.println("runoob value in cache - " + mcc.get("runoob"));
// 通过 gets 方法获取 CAS token(令牌)
CASValue casValue = mcc.gets("runoob");
// 输出 CAS token(令牌) 值
System.out.println("CAS token - " + casValue);
// 尝试使用cas方法来更新数据
CASResponse casresp = mcc.cas("runoob", casValue.getCas(), "11111");
// 输出 CAS 响应信息
System.out.println("CAS Response - " + casresp);
// 输出值
System.out.println("runoob value in cache - " + mcc.get("runoob"));
//这样更新会失败的,因为期望的casid为 mcc.gets("runoob").getCas() + 1,但实际上id为mcc.gets("runoob").getCas()
casresp = mcc.cas("runoob", mcc.gets("runoob").getCas() + 1, "2222");
// 输出 CAS 响应信息
System.out.println("CAS Response - " + casresp);
// 输出值
System.out.println("runoob value in cache - " + mcc.get("runoob"));
// 关闭连接
mcc.shutdown();
}
}
输出为:
set status:true
runoob value in cache - Free Education
CAS token - {CasValue 51/Free Education}
CAS Response - OK
runoob value in cache - 11111
CAS Response - EXISTS
runoob value in cache - 11111
memcached的每次操作,cas的id都为递增,并且cas的key一定要存在,要不然会执行失败。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Created by wzj on 2018/6/5.
*/
public class MemcachedTest
{
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException
{
// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
Future<Boolean> future = mcc.set("china", 100, "111");
System.out.println("value = " + mcc.get("china") + " cas id = " + mcc.gets("china").getCas());
mcc.cas("china", mcc.gets("china").getCas(), "222");
System.out.println("value = " + mcc.get("china") + " cas id = " + mcc.gets("china").getCas());
mcc.cas("china", mcc.gets("china").getCas(), "333");
System.out.println("value = " + mcc.get("china") + " cas id = " + mcc.gets("china").getCas());
// 关闭连接
mcc.shutdown();
}
}
输出结果:
value = 111 cas id = 57
value = 222 cas id = 58
value = 333 cas id = 59