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);
}
}