Java 使用Memcache

使用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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值