<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

    一:安裝
      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.tkk7.com/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;
        }
     ....

    posted on 2012-10-26 22:54 abin 閱讀(1182) 評論(0)  編輯  收藏 所屬分類: memcache
    主站蜘蛛池模板: 2021精品国产品免费观看| 97人妻无码一区二区精品免费| 亚洲精品无码久久久久sm| 99久久国产亚洲综合精品| 哒哒哒免费视频观看在线www| 欧美最猛性xxxxx免费| 免费不卡视频一卡二卡| 亚洲免费视频播放| 久久久高清免费视频 | 国产一级淫片免费播放电影| 国产日产成人免费视频在线观看 | 亚洲综合视频在线观看| 亚洲自偷自拍另类图片二区| 亚洲另类激情综合偷自拍 | 一本色道久久综合亚洲精品| 免费看的一级毛片| 国产成人免费全部网站| 亚洲精品综合久久| 四虎www成人影院免费观看| 国产一精品一AV一免费孕妇| 午夜时刻免费入口| 亚洲av无码成人精品区| 亚洲午夜国产精品无码 | 免费的一级片网站| 亚洲国产人成精品| 亚洲AV无码乱码国产麻豆穿越| 亚洲韩国在线一卡二卡| 亚洲色精品三区二区一区| 亚洲精品一区二区三区四区乱码| 亚洲啪啪免费视频| 亚洲视频在线免费播放| 亚洲日韩乱码中文字幕| fc2免费人成为视频| 99在线免费观看视频| 97无码免费人妻超级碰碰夜夜| 又大又硬又爽免费视频| 亚洲av午夜成人片精品网站| 67194在线午夜亚洲| 色多多A级毛片免费看| 曰批免费视频播放在线看片二| 日韩精品无码免费专区午夜不卡|