今天捣鼓了下mencached 还不错![]()
依赖库
commons-pool-1.5.6
java_memcached-release_2.6.6
slf4j-api-1.6.1
slf4j-simple-1.6.1
mencached
第三方维护的win32版
http://splinedancer.com/memcached-win32/
1package com.Mencached; 2 3import com.danga.MemCached.MemCachedClient; 4import com.danga.MemCached.SockIOPool; 5 6import java.util.Date; 7 8/** 9 * Created with IntelliJ IDEA. 10 * User: CHENLEI 11 * Date: 12-10-25 12 * Time: 下午2:15 13 * To change this template use File | Settings | File Templates. 14 * java数据提交到mencached 测试 15 */ 16public class MenCached { 17 18 private static MemCachedClient mcc = new MemCachedClient(); 19 20 // set up connection pool once at class load 21 static { 22 // server list and weights 23 String[] servers = 24 { 25 "127.0.0.1:11211", 26// "server2.mydomain.com:1624", 27// "server3.mydomain.com:1624" 28 }; 29 30 Integer[] weights = { 3 }; 31 32 // SockIOPool 33 SockIOPool pool = SockIOPool.getInstance(); 34 35 // set the servers and the weights 36 pool.setServers( servers ); 37 pool.setWeights( weights ); 38 39 // initialize、最小和最大连接数以及最大处理时间 40 pool.setInitConn( 5 ); 41 pool.setMinConn( 5 ); 42 pool.setMaxConn( 250 ); 43 pool.setMaxIdle( 1000 * 60 * 60 * 6 ); 44 45 46 pool.setMaintSleep(10);//thread sleep 47 48 49 pool.setNagle( false ); 50 pool.setSocketTO( 3000 ); //timeout read 3secs 51 pool.setSocketConnectTO( 0 ); 52 53 // initialize the connection pool 54 pool.initialize(); 55 } 56 57 private MenCached(){} 58 59 //获取MenCached操作引用 60 public static MenCached getInstance() 61 { 62 return singlton.mencached; 63 } 64 private static final class singlton{ 65 private static final MenCached mencached=new MenCached(); 66 } 67 68 /** 69 * 添加一个指定的值到缓存中. 70 * @param key 71 * @param value 72 * @return 73 */ 74 public boolean add(String key, Object value) 75 { 76 return mcc.add(key, value); 77 } 78 79 public boolean add(String key, Object value, Date expiry) 80 { 81 return mcc.add(key, value, expiry); 82 } 83 84 /** 85 * 替换一个指定的值到缓存中. 86 * @param key 87 * @param value 88 * @return 89 */ 90 public boolean replace(String key, Object value) 91 { 92 return mcc.replace(key, value); 93 } 94 95 /** 96 * 97 * @param key 98 * @param value 99 * @param expiry 过期 100 * @return 101 */ 102 public boolean replace(String key, Object value, Date expiry) 103 { 104 return mcc.replace(key, value, expiry); 105 } 106 107 /** 108 * 删除一个指定的值到缓存中. 109 * @param key 110 * @param 111 * @return 112 */ 113 public boolean delete(String key) 114 { 115 return mcc.delete(key); 116 } 117 118 119 /** 120 * 根据指定的关键字获取对象. 121 * @param key 122 * @return 123 */ 124 public Object get(String key) 125 { 126 return mcc.get(key); 127 } 128 129 public static void main(String[] args) 130 { 131 MenCached cache = MenCached.getInstance(); 132 long t1=System.currentTimeMillis(); 133 for (int i=0;i<10;i++){ 134 cache.add(i+"",i) ; 135 System.out.println("value:"+cache.get(i+"")); 136 } 137 System.out.println(System.currentTimeMillis()-t1); 138// System.out.println("get value : " + cache.get("key")); 139 } 140}
对于redis ,你需要下载redis,这里我用的是window版本,配置好redis.conf后 start redis-server.exe redis.conf启动redis.
测试,你需要https://github.com/xetorthio/jedis/downloads
jedis
java客户端
使用简单,但是相关的文献较少,要想掌握,看api,或则看看源码咯,也不是很多
1package com.Mencached; 2 3import redis.clients.jedis.Jedis; 4 5/** 6 * Created with IntelliJ IDEA. 7 * User: CHENLEI 8 * Date: 12-10-27 9 * Time: 上午11:34 10 * To change this template use File | Settings | File Templates. 11 * redis test 12 */ 13public class redisDeamo { 14 //在redis.conf中配置hosts prot 15 private static final Jedis jedis = new Jedis("127.0.0.1",9090); 16 public static redisDeamo getInstance(){ 17 return sigtlon.redis; 18 } 19 private redisDeamo(){ 20 } 21 private static final class sigtlon{ 22 private static final redisDeamo redis=new redisDeamo(); 23 } 24 25 public void setJedis(java.lang.String key, java.lang.String value){ 26 jedis.set(key, value); 27 } 28 29 public Object getJedis(java.lang.String key){ 30 return jedis.get(key); 31 } 32 33 34 //test 35 public static void main(String[]args){ 36 redisDeamo.getInstance().setJedis("foo","909090"); 37 38 String value = (String) redisDeamo.getInstance().getJedis("foo"); 39 System.out.println("获取的值:"+ value); 40 41 } 42 43 44 45}
实际的应用 还得深入的去考究