java代码链接memcached集群,操作实例

package com.yr.memcached.xml;

 

import com.danga.MemCached.MemCachedClient;

import com.danga.MemCached.SockIOPool;

 

public class MyCache {

public static void main(String[] args) {

MemCachedClient client = new MemCachedClient();

String[] addr = { "192.168.1.120:11211", "192.168.1.120:11212", "192.168.1.120:11213" };

Integer[] weights = { 3 };

SockIOPool pool = SockIOPool.getInstance();// 后台守护线程socketIO连接池

pool.setServers(addr);// 设置服务器的链接地址

pool.setWeights(weights);// 设置的服务器实例的Load权重

pool.setInitConn(5);// 初始化连接数5

pool.setMinConn(5);// 设置最小连接数

pool.setMaxConn(200);// 设置最大连接数

pool.setMaxIdle(1000 * 30 * 30);// 设置最大链接时间

pool.setMaintSleep(30);// 设置最大等待时间

pool.setNagle(false);//设置是否使用Nagle算法,因为我们的通讯数据量通常都比较大(相对TCP控制数据)而且要求响应及时,因此该值需要设置为false(默认是ture)

pool.setSocketTO(30);

pool.setSocketConnectTO(0);

pool.initialize(); // 初始化连接池

 

TestBean bean = new TestBean();

bean.setName("xiao");

bean.setAge(25);

// 添加

boolean isAdd = client.add("bean", bean);

System.out.println("添加:" + isAdd);

// 修改

bean.setName("liang");

bean.setAge(18);

boolean isUpd = client.set("bean", bean);

System.out.println("修改:" + isUpd);

// 查询

TestBean beanClient = (TestBean) client.get("bean");

System.out.println("查询得到数据:" + beanClient.toString());

// 删除

boolean isDel = client.delete("bean");

System.out.println("删除:" + isDel);

client.add("key", "陈无天");

// append 往后面加值 prepend往前面加值

client.prepend("key", "aaa-");

client.append("key", "-bbb");

String str = client.get("key").toString();

System.out.println("(append,prepend)得到之后的值:" + str);

client.replace("key", "我是被替换的值^_^");

String str2 = client.get("key").toString();

System.out.println("replace替换之后得到的值:" + str2);

boolean isDelete = client.delete("key");

System.out.println("删除:" + isDelete);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值