package com.bennytian.memcached; |
003 | import java.io.Serializable; |
004 | import java.util.Date; |
006 | import com.danga.MemCached.MemCachedClient; |
007 | import com.danga.MemCached.SockIOPool; |
009 | @SuppressWarnings ( "serial" ) |
010 | public class MemCached implements Serializable { |
012 | private static MemCachedClient mcc = new MemCachedClient(); |
013 | private static MemCached memCached = new MemCached(); |
016 | private static final String [] services = { "127.0.0.1:11211" }; |
018 | private static final Integer[] weights = { 1 }; |
020 | private static final Integer init_connection = 5 ; |
022 | private static final Integer min_connection = 5 ; |
024 | private static final Integer max_connection = 250 ; |
026 | private static final Long max_idle = 21600L ; |
028 | private static final Long maint_sleep = 10L; |
032 | SockIOPool pool = SockIOPool.getInstance(); |
033 | pool.setServers(services); |
034 | pool.setWeights(weights); |
035 | pool.setInitConn(init_connection); |
036 | pool.setMinConn(min_connection); |
037 | pool.setMaxConn(max_connection); |
038 | pool.setMaxIdle(max_idle); |
039 | pool.setMaintSleep(maint_sleep); |
042 | pool.setNagle( false ); |
043 | pool.setSocketTO( 3000 ); |
044 | pool.setSocketConnectTO( 0 ); |
050 | mcc.setCompressEnable( true ); |
051 | mcc.setCompressThreshold(65536L); |
054 | private MemCached(){} |
056 | public static MemCached getInstance(){ |
060 | public boolean add(String key,Object value){ |
061 | return mcc.add(key, value); |
064 | public boolean add(String key,Object value,Date expiry){ |
065 | return mcc.add(key, value, expiry); |
068 | public boolean replace(String key, Object value){ |
069 | return mcc.replace(key, value); |
072 | public boolean replace(String key, Object value, Date expiry){ |
073 | return mcc.replace(key, value, expiry); |
076 | public Object get(String key){ |
080 | public static void main(String[] args) { |
081 | MemCached cache = MemCached.getInstance(); |
083 | cache.add( "11" , cache); |
084 | cache.add( "12" , new String( "1234" )); |
085 | /*for (int i = 0; i < 1000; i++) { |
086 | cache.add(i+"", cache); |
088 | System.out.println(cache.get("11")); |
089 | System.out.println(cache.get("12")); |
090 | System.out.println(System.currentTimeMillis()-d.getTime()); |
091 | //System.out.print("get value : " + cache.get("hello")); |
092 | //System.out.println(cache.get("100")); |
096 | c:>memcached.exe -d install |
098 | c:>memcached.exe -l 127.0.0.1 -m 32 -d start |
转载于:https://my.oschina.net/u/580135/blog/612235