1.下载依赖的JAR包
java_memcached-release_2.6.2.jar
commons-pool-1.5.6.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
2.JAVA程序
package com.memecached; import java.util.Date; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcachedExample { private static MemCachedClient memCachedClient = new MemCachedClient(); // 设置与缓存服务器的连接池 static { // 服务器列表和其权重 String[] servers = { "192.168.74.129:11211" }; Integer[] weights = { 3 }; // 获取socke连接池的实例对象 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); // 设置TCP的参数,连接超时等 pool.setNagle(false); pool.setSocketTO(3000); pool.setSocketConnectTO(0); // 初始化连接池 pool.initialize(); } public static void main(String[] args) { //key或value不能为空,否则不能存入 memCachedClient.set("username", "张三");//添加键值对key=username,value=张三 memCachedClient.set("username2", "张三", new Date(new Date().getTime() + 60*1000));//添加键值对key=username2,value=张三,并且指定有效期60秒 System.out.println("username="+memCachedClient.get("username")); System.out.println("username2="+memCachedClient.get("username2")); memCachedClient.replace("username", "李四");//替换键username的值为李四 memCachedClient.replace("username2", "李四", new Date(new Date().getTime() + 60*1000));//替换键username的值为李四,并且指定有效期60秒 memCachedClient.add("username3", "张三");//添加键值对key=username,value=张三,只有当username3不存在时,才会添加 System.out.println("username3="+memCachedClient.get("username3")); memCachedClient.delete("username3");//删除键值班对 System.out.println("username3="+memCachedClient.get("username3")); System.out.println("statsItems:"+memCachedClient.statsItems()); System.out.println("statsSlabs:"+memCachedClient.statsSlabs()); } }
3.JAVA程序运行结果
username=张三 username2=张三 username3=张三 username3=null statsItems:{192.168.74.129:11211={items:5:evicted_unfetched=3265507 , items:1:evicted_unfetched=0 , items:1:evicted_time=0 , items:1:expired_unfetched=1 , items:5:expired_unfetched=0 , items:1:number=2 , items:5:evicted_time=202 , items:5:evicted=3265507 , items:1:reclaimed=3 , items:1:evicted_nonzero=0 , items:5:tailrepairs=0 , items:1:evicted=0 , items:5:number=1734493 , items:1:tailrepairs=0 , items:1:age=0 , items:5:age=7372 , items:5:reclaimed=0 , items:1:outofmemory=0 , items:5:evicted_nonzero=0 , items:5:outofmemory=0 }} statsSlabs:{192.168.74.129:11211={5:chunk_size=240 , 2:incr_hits=0 , 2:cas_badval=0 , 5:delete_hits=0 , 1:cas_hits=0 , 5:total_chunks=1734493 , 2:used_chunks=0 , 1:used_chunks=2 , 5:chunks_per_page=4369 , 5:decr_hits=0 , 5:cas_badval=0 , 5:free_chunks_end=0 , 1:free_chunks=10920 , 2:touch_hits=0 , 5:get_hits=1734493 , 2:total_pages=115 , 2:delete_hits=0 , 2:get_hits=1000000 , 2:free_chunks=1004870 , 2:total_chunks=1004870 , 2:free_chunks_end=0 , 1:chunks_per_page=10922 , 5:incr_hits=0 , 1:delete_hits=5 , 1:incr_hits=0 , 2:chunk_size=120 , 5:mem_requested=364243530 , 5:cas_hits=0 , active_slabs=3 , 1:touch_hits=0 , 1:get_hits=25 , 2:cmd_set=1000000 , 1:total_chunks=10922 , 2:decr_hits=0 , 2:chunks_per_page=8738 , 1:decr_hits=0 , 5:total_pages=397 , 2:cas_hits=0 , total_malloced=537911232 , 1:cmd_set=42 , 5:cmd_set=6000000 , 1:chunk_size=96 , 1:cas_badval=0 , 1:total_pages=1 , 2:mem_requested=0 , 1:free_chunks_end=0 , 5:used_chunks=1734493 , 1:mem_requested=161 , 5:free_chunks=0 , 5:touch_hits=0 }}