使用spymemcached.jar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
public
class
MemcachedJava {
public
static
void
main(String[] args) {
try
{
// 连接本地的 Memcached 服务
MemcachedClient mcc =
new
MemcachedClient(
new
InetSocketAddress(
"127.0.0.1"
,
11211
));
System.out.println(
"Connecton to server successful"
);
// 查看存储状态
Future fu = mcc.set(
"news"
,
900
,
"There is a import news today!"
);
System.out.println(
"get Status: "
+ fu.get());
System.out.println(
"获得news的值: "
+ mcc.get(
"news"
));
mcc.add(
"news"
,
900
,
"Second news"
);
System.out.println(
"get Status: "
+ fu.get());
System.out.println(
"获得news的值: "
+ mcc.get(
"news"
));
System.out.println(
"-------------------------"
);
//mcc.add("today", 900, "today news 59595");
System.out.println(
"get Status: "
+ fu.get());
System.out.println(
"获得today的值: "
+ mcc.get(
"today"
));
System.out.println(
"-------------------------"
);
//Replace
mcc.replace(
"today"
,
900
,
"today news abcd576457"
);
System.out.println(
"get Status: "
+ fu.get());
System.out.println(
"获得today的值: "
+ mcc.get(
"today"
));
System.out.println(
"-------------------------"
);
//Append
mcc.append(
"today"
,
"[Append contenet!]"
);
System.out.println(
"get Status: "
+ fu.get());
System.out.println(
"获得today的值: "
+ mcc.get(
"today"
));
System.out.println(
"-------------------------"
);
//Prepend
mcc.prepend(
"today"
,
"[Prepend contenet] "
);
System.out.println(
"get Status: "
+ fu.get());
System.out.println(
"获得today的值: "
+ mcc.get(
"today"
));
System.out.println(
"-------------------------"
);
//CAS 操作实例
CASValue casValue = mcc.gets(
"today"
);
// 输出 CAS token(令牌) 值
System.out.println(
"CAS token - "
+ casValue);
CASResponse casResponse = mcc.cas(
"today"
, casValue.getCas(),
900
,
"[Changed today data]Change data by cas"
);
// 输出 CAS 响应信息
System.out.println(
"CAS Response - "
+ casResponse);
System.out.println(
"获得today的值: "
+ mcc.get(
"today"
));
System.out.println(
"-------------------------"
);
// 关闭连接
mcc.shutdown();
}
catch
(IOException e) {
System.out.println(e.getMessage());
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
}
|
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/6208862.html,如需转载请自行联系原作者