memcached java客户端的简单使用
启动Memcached服务端
./memcached -d -m10 -u root -l 192.168.1.106 -p 2222 -c 256 -P /tmp/memcached.pid
- d选项是启动一个守护进程
- m是分配给Memcache使用的内存数量,单位是MB,这里是10MB
- u是运行Memcache的用户,这里是r oot
- l 是监听的服务器I P地址,这里指定了服务器的I P地址192. 168. 1. 106 - p是监听的端口,这里设置了2222,最好是1024以上的端口
- c选项是最大运行的并发连接数,默认是1024,这里设置了256
- P是设置保存Memcache的pi d文件,这里是保存在 / t mp/ memcached. pi d
常用的还有几个需要了解:
-f 块大小增长因子,默认是1.25
- n 最小分配空间, key+val ue+f l ags 默认是 48byt e - I 每个sl ab page的大小
- v/ - vv 详细显示工作时各种参数
- 关闭Memcached,先用 ps -ef| grep memcached找到进程号,然后kill 掉
1.不使用spring代码片段CacheHelper.java
private static MemCachedClient mcc = new MemCachedClient();
private CacheHelper() {
}
static {
String[] servers ={ "127.0.0.1:11211" };
//{ "192.168.1.106:2222","192.168.1.106:2223" };
Integer[] weights = { 1};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setWeights(weights);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
pool.setMaintSleep(30);
pool.setNagle(false);// 禁用nagle算法
pool.setSocketConnectTO(0);
pool.setSocketTO(3000);// 3秒超时
pool.setHashingAlg(3);// 设置为一致性hash算法
pool.initialize();
}
public static MemCachedClient getMemCachedClient() {
return mcc;
}
MemCachedClient mcc = CacheHelper.getMemCachedClient();
//mcc.add("k2", "12345");
UserModel um = new UserModel("11","11Name",11);
mcc.set("t1", um);
Object obj = mcc.get("k2");
Object obj1 = mcc.get("t1");
System.out.println(obj);
System.out.println(obj1);
2.与spring整合
applicationContext.xml文件:
<!--memcached 客户端 SocketPool-->
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
init-method="initialize" destroy-method="shutDown">
<constructor-arg><value>neeaMemcachedPool</value></constructor-arg>
<property name="servers">
<list>
<value>127.0.0.1:11211</value>
<!--
<value>192.168.1.106:2222</value>
<value>192.168.1.106:2223</value>
-->
</list>
</property>
<property name="weights">
<list>
<value>1</value>
<!-- <value>1</value> -->
</list>
</property>
<property name="initConn">
<value>5</value>
</property>
<property name="minConn">
<value>5</value>
</property>
<property name="maxConn">
<value>250</value>
</property>
<property name="maintSleep">
<value>30</value>
</property>
<property name="nagle">
<value>false</value>
</property>
<property name="maxIdle">
<value>6000</value>
</property>
<property name="socketTO">
<value>3000</value>
</property>
</bean>
<!--memcached client-->
<bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
</bean>
@Service
public class ClientTest {
@Autowired
private MemCachedClient mcc;
public void setMcc(MemCachedClient mcc){
this.mcc = mcc;
}
public static void main(String[] args) {
//不使用spring
// MemCachedClient mcc = CacheHelper.getMemCachedClient();
//mcc.add("k2", "12345");
// UserModel um = new UserModel("11","11Name",11);
// mcc.set("t1", um);
// Object obj = mcc.get("k2");
// Object obj1 = mcc.get("t1");
// System.out.println(obj);
// System.out.println(obj1);
// List<UserModel> list = new ArrayList<UserModel>();
// UserModel um1 = new UserModel("33","11Name",11);
// UserModel um2 = new UserModel("44","22Name",22);
//
// list.add(um1);
// list.add(um2);
// ct.mcc.set("t1", list);
// ct.mcc.set("k1","springt1");
//
// Object obj = ct.mcc.get("k1");
//
// System.out.println("obj==="+obj);
//使用与spring整合
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ClientTest ct = (ClientTest)ctx.getBean("clientTest");
Object obj = ct.mcc.get("k2");
Object obj1 = ct.mcc.get("t1");
System.out.println(obj);
System.out.println(obj1);
}
}