锁定老帖子 主题:Memcached安装、使用,与AOP集成
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2010-12-16
最后修改:2010-12-16
一:安装
- 到http://code.jellycan.com/memcached/下载稳定版。 - 下载后解压到某个盘下面,比如在c:\memcached,在终端(也即cmd命令界面)下输入 ‘c:\memcached\memcached.exe -d install’ 安装。 - 再输入: ‘c:\memcached\memcached.exe -d start’ 启动。 - 修改memcache的内存大小,可以在注册表里找到HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server,修改ImagePath的值为 package com.ea.online.memcache; import java.util.Date; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MyClass { // create a static client as most installs only need // a single instance protected static MemCachedClient mcc = new MemCachedClient(); protected static SockIOPool pool = null; // set up connection pool once at class load static { // Server list String[] servers = { "localhost:11211" }; // Specify memcached capacity Integer[] weights = { 3, 3, 2 }; /* * String[] serverlist = { "cache0.server.com:12345", * "cache1.server.com:12345" }; Integer[] weights = { new * Integer(5), new Integer(2) }; int initialConnections = 10; int * minSpareConnections = 5; int maxSpareConnections = 50; long * maxIdleTime = 1000 * 60 * 30; // 30 minutes long maxBusyTime = 1000 * * 60 * 5; // 5 minutes long maintThreadSleep = 1000 * 5; // 5 seconds * int socketTimeOut = 1000 * 3; // 3 seconds to block on reads int * socketConnectTO = 1000 * 3; // 3 seconds to block on initial * connections. If 0, then will use blocking connect (default) boolean * failover = false; // turn off auto-failover in event of server down * boolean nagleAlg = false; // turn off Nagle's algorithm on all * sockets in pool boolean aliveCheck = false; // disable health check * of socket on checkout * * SockIOPool pool = SockIOPool.getInstance(); * pool.setServers(serverlist); * pool.setWeights(weights); * pool.setInitConn(initialConnections); * pool.setMinConn(minSpareConnections); * pool.setMaxConn(maxSpareConnections); pool.setMaxIdle(maxIdleTime); * pool.setMaxBusyTime(maxBusyTime); * pool.setMaintSleep(maintThreadSleep); * pool.setSocketTO(socketTimeOut); pool.setNagle(nagleAlg); * pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH); * pool.setAliveCheck(true); pool.initialize(); */ // grab an instance of our connection pool pool = SockIOPool.getInstance(); // set the servers and the weights pool.setServers(servers); pool.setWeights(weights); // Specify main thread maintain frequency pool.setMaintSleep(30); // set some TCP settings // disable nagle pool.setNagle(false); // set the read timeout to 3 secs pool.setSocketTO(3000); // and don't set a connect timeout pool.setSocketConnectTO(0); // initialize the connection pool pool.initialize(); } // from here on down, you can call any of the client calls public static void main(String[] args) { // Test expired mcc.set("foo", "This is a test String", new Date( new Date().getTime() + 3000)); String bar = mcc.get("foo").toString(); System.out.println("test-->" + bar); while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(mcc.get("foo")); } // pool.shutDown(); } } public Around implements MethodInterceptor { .... public Object invoke(MethodInvocation mi, Object[] args) { Object obj = null; //从Memcached中获取 obj = mcc.get(this.class.getName() + mi.getMethodName() + args.hashcode()); if(obj != null) { return obj; } obj = method.invoke(args); //存入Memcached mcc.set(this.class.getName() + mi.getMethodName() + args.hashcode, obj, expiredDate); return obj; } .... }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 4928 次