论坛首页 综合技术论坛

Memcached安装、使用,与AOP集成

浏览 4928 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2010-12-16   最后修改:2010-12-16

一:安装
  1,如果是LINUX系统,可以到官方网址http://memcached.org/进行下载,安装教程网上一大堆,这里不再叙述。
  2,如果是WINDOWS系统

     - 到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的值为
“C:/memcached/memcached.exe” -d runservice -m 512
  NOTE: Windows版本一般用作开发调试只用,不建议在产品环境中使用。

二:JAVA连接使用Memcached
  1, 下载memcached客户端开发包,地址https://github.com/gwhalin/Memcached-Java-Client
  2,下面是一个连接并使用的简单例子

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();
    }
}

 
详细使用可以借鉴这篇文章:http://sillycat.iteye.com/blog/563615

三:结合AOP编程
  1,对于memcached与spring aop的集成网上又是一堆,这里不提了。
  2,对于memcached与guice aop的集成可参考http://code.google.com/p/google-guice/wiki/AOP
  3,对于需要通过手动实现动态代理的方式来实现AOP的可以参考 http://www.blogjava.net/DoubleJ/archive/2008/03/04/183796.html

四:一个简单的环绕通知切面,不可运行,仅作参考

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;
    }
 ....
}
 

 

论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics