Memcached学习(三)

本文介绍如何使用Java客户端与Memcached进行交互。通过开源库Memcached-Java-Client实现Memcached缓存设置与获取操作。示例代码展示了连接配置、键值存储及读取流程。

通过Java客户端实现与Memcached的交互,Java客户端的实现了使用了开源的Memcached-Java-Client,开源地址在GitHub上。

如下是通过该开源库实现的Memcached交互demo。

 

 1 import com.schooner.MemCached.SchoonerSockIOPool;
 2 import com.whalin.MemCached.MemCachedClient;
 3 import com.whalin.MemCached.SockIOPool;
 4 
 5 public class TestMem {
 6     
 7     private MemCachedClient mClient;
 8     
 9     public static void main(String[] arg) {
10         TestMem testMem = new TestMem();
11         testMem.test();
12     }
13     
14     public TestMem() {
15         String[] servers ={"127.0.0.1:11211", "127.0.0.1:11212", "127.0.0.1:11213", "127.0.0.1:11214"};
16         SockIOPool pool = SockIOPool.getInstance();
17         pool.setServers(servers);
18         pool.setFailover(true);
19         pool.setInitConn(5);
20         pool.setMinConn(2);
21         pool.setMaxConn(10);
22         pool.setMaintSleep(30);
23         pool.setNagle(false);
24         pool.setSocketTO(3000);
25         pool.setAliveCheck(true);
26         pool.setHashingAlg(SchoonerSockIOPool.CONSISTENT_HASH);
27         pool.initialize();
28         
29         mClient = new MemCachedClient(true);
30     }
31     
32     public void test() {
33         boolean isSucc = false;
34         isSucc = mClient.set("mKey", "mValue");
35         log("isSucc:" + isSucc);
36 
37         log("obj:" + mClient.get("mKey"));
38     }
39     
40     public static void log(Object obj) {
41         System.out.println(obj);
42     }
43 }

 

转载于:https://www.cnblogs.com/jadic/p/3679800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值