今天搗鼓了下mencached 還不錯 java
依賴庫git
commons-pool-1.5.6github
java_memcached-release_2.6.6redis
slf4j-api-1.6.1api
slf4j-simple-1.6.1緩存
mencacheddom
第三方維護的win32版memcached
http://splinedancer.com/memcached-win32/ 測試
package com.Mencached; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; import java.util.Date; /** * Created with IntelliJ IDEA. * User: CHENLEI * Date: 12-10-25 * Time: 下午2:15 * To change this template use File | Settings | File Templates. * java數據提交到mencached 測試 */ public class MenCached { private static MemCachedClient mcc = new MemCachedClient(); // set up connection pool once at class load static { // server list and weights String[] servers = { "127.0.0.1:11211", // "server2.mydomain.com:1624", // "server3.mydomain.com:1624" }; Integer[] weights = { 3 }; // SockIOPool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights pool.setServers( servers ); pool.setWeights( weights ); // initialize、最小和最大鏈接數以及最大處理時間 pool.setInitConn( 5 ); pool.setMinConn( 5 ); pool.setMaxConn( 250 ); pool.setMaxIdle( 1000 * 60 * 60 * 6 ); pool.setMaintSleep(10);//thread sleep pool.setNagle( false ); pool.setSocketTO( 3000 ); //timeout read 3secs pool.setSocketConnectTO( 0 ); // initialize the connection pool pool.initialize(); } private MenCached(){} //獲取MenCached操做引用 public static MenCached getInstance() { return singlton.mencached; } private static final class singlton{ private static final MenCached mencached=new MenCached(); } /** * 添加一個指定的值到緩存中. * @param key * @param value * @return */ public boolean add(String key, Object value) { return mcc.add(key, value); } public boolean add(String key, Object value, Date expiry) { return mcc.add(key, value, expiry); } /** * 替換一個指定的值到緩存中. * @param key * @param value * @return */ public boolean replace(String key, Object value) { return mcc.replace(key, value); } /** * * @param key * @param value * @param expiry 過時 * @return */ public boolean replace(String key, Object value, Date expiry) { return mcc.replace(key, value, expiry); } /** * 刪除一個指定的值到緩存中. * @param key * @param * @return */ public boolean delete(String key) { return mcc.delete(key); } /** * 根據指定的關鍵字獲取對象. * @param key * @return */ public Object get(String key) { return mcc.get(key); } public static void main(String[] args) { MenCached cache = MenCached.getInstance(); long t1=System.currentTimeMillis(); for (int i=0;i<10;i++){ cache.add(i+"",i) ; System.out.println("value:"+cache.get(i+"")); } System.out.println(System.currentTimeMillis()-t1); // System.out.println("get value : " + cache.get("key")); } }
對於redis ,你須要下載redis,這裏我用的是window版本,配置好redis.conf後 start redis-server.exe redis.conf啓動redis.this
測試,你須要https://github.com/xetorthio/jedis/downloads
jedis
java客戶端
使用簡單,可是相關的文獻較少,要想掌握,看api,或則看看源碼咯,也不是不少
package com.Mencached; import redis.clients.jedis.Jedis; /** * Created with IntelliJ IDEA. * User: CHENLEI * Date: 12-10-27 * Time: 上午11:34 * To change this template use File | Settings | File Templates. * redis test */ public class redisDeamo { //在redis.conf中配置hosts prot private static final Jedis jedis = new Jedis("127.0.0.1",9090); public static redisDeamo getInstance(){ return sigtlon.redis; } private redisDeamo(){ } private static final class sigtlon{ private static final redisDeamo redis=new redisDeamo(); } public void setJedis(java.lang.String key, java.lang.String value){ jedis.set(key, value); } public Object getJedis(java.lang.String key){ return jedis.get(key); } //test public static void main(String[]args){ redisDeamo.getInstance().setJedis("foo","909090"); String value = (String) redisDeamo.getInstance().getJedis("foo"); System.out.println("獲取的值:"+ value); } }
實際的應用 還得深刻的去考究