1. Memcached的Java环境设置
需要下载spymemcached-2.10.3.jar,并把这个jar放到java程序的classpath中才能使用memcached。
在下面的程序,假设memcached服务器的主机IP是192.168.1.111,并在端口11211上运行。
1. Memcached添加数据(set方法)
/**
* set方法
*/
public static void set() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11111));
// not set data into memcached server
System.out.println("set status:"
+ mcc.set("hello", 900, "helloworld"));
// Get value from cache
System.out.println("Get from Cache:" + mcc.get("hello"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2. Memcached添加数据(add方法)
/**
* add方法
*/
public static void add() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
mcc.add("key", 900, "memcached");
System.out.println(mcc.get("key"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3. Memcached替换/Replace数据(replace方法)
/**
* replace方法
*/
public static void replace() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
// 修改存在的键
System.out.println(mcc.get("key"));
mcc.replace("key", 900, "mongodb");
System.out.println(mcc.get("key"));// mongodb
// 修改不存在的键
mcc.replace("no", 900, "noexit");
System.out.println(mcc.get("no"));// null
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4. Memcached追加/append方法(append方法)
/**
* append
*/
public static void append() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.get("key"));
mcc.append("key", "nosql");
System.out.println(mcc.get("key"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
5. Memcached预先添(prepend方法)
/**
* prepend
*/
public static void prepend() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.get("key"));
mcc.prepend("key", "redis");
System.out.println(mcc.get("key"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
6. Memcached cas命令(cas方法)
cas即checked and set的意思,只有当最后一个参数和gets所获取的参数匹配时才能存储,否则返回“EXISTS”。
要运行memcached的cas命令,需要从gets命令得到memcached令牌。
/**
* cas方法
*/
public static void cas() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.get("key"));
long casToken = mcc.gets("key").getCas();
System.out.println(casToken);
mcc.cas("key", casToken, 900, "mongodb");
System.out.println(mcc.get("key"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
7. Memcached获取/get数据(get方法)
/**
* get方法
*/
public static void get() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
// not set data into memcached server
System.out.println("set status:"
+ mcc.set("hello", 900, "helloworld"));
// Get value from cache
System.out.println("Get from Cache:" + mcc.get("hello"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
8. Memcached gets命令(gets方法)
/**
* gets方法
*/
public static void gets() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
long casToken = mcc.gets("key").getCas();
System.out.println(casToken);
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
9. Memcached删除/Delete数据(delete方法)
/**
* delete方法
*/
public static void delete() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.get("key"));
mcc.delete("key");
System.out.println(mcc.get("key"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
10. Memcached递增递减数据(incr方法和decr方法)
Memcached的incr 和 decr命令用于增加现有键的数值递减。如果键未找到或如果关键的不是数字,则返回NOT_FOUND。那么CLIENT_ERROR不能增加或返回递减非数值错误。
/**
* incr方法和decr方法
*/
public static void incr_decr() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.get("num"));
mcc.incr("num", 5);
System.out.println(mcc.get("num"));
System.out.println("------------------>");
mcc.decr("num", 10);
System.out.println(mcc.get("num"));
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
11. Memcached Stats命令(stats命令)
/**
* stats方法
*/
public static void stats() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.getStats());
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
12. Memcached清除数据(flush方法)
/**
* flush_all方法
*/
public static void flush_all() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(
"192.168.1.111", 11211));
System.out.println(mcc.flush().isDone());
mcc.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}