少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          一:安裝
            1,如果是LINUX系統(tǒng),可以到官方網(wǎng)址http://memcached.org/進(jìn)行下載,安裝教程網(wǎng)上一大堆,這里不再敘述。
            2,如果是WINDOWS系統(tǒng)

               - 到http://code.jellycan.com/memcached/下載穩(wěn)定版。

               - 下載后解壓到某個盤下面,比如在c:\memcached,在終端(也即cmd命令界面)下輸入 ‘c:\memcached\memcached.exe -d install’ 安裝。

               - 再輸入: ‘c:\memcached\memcached.exe -d start’ 啟動。

               - 修改memcache的內(nèi)存大小,可以在注冊表里找到HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server,修改ImagePath的值為
          “C:/memcached/memcached.exe” -d runservice -m 512
            NOTE: Windows版本一般用作開發(fā)調(diào)試只用,不建議在產(chǎn)品環(huán)境中使用。

          二:JAVA連接使用Memcached
            1, 下載memcached客戶端開發(fā)包,地址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();
              }
          }

           

           

          詳細(xì)使用可以借鑒這篇文章:http://sillycat.iteye.com/blog/563615

          三:結(jié)合AOP編程
            1,對于memcached與spring aop的集成網(wǎng)上又是一堆,這里不提了。
            2,對于memcached與guice aop的集成可參考http://code.google.com/p/google-guice/wiki/AOP
            3,對于需要通過手動實現(xiàn)動態(tài)代理的方式來實現(xiàn)AOP的可以參考 http://www.aygfsteel.com/DoubleJ/archive/2008/03/04/183796.html

          四:一個簡單的環(huán)繞通知切面,不可運行,僅作參考

          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 閱讀(1185) 評論(0)  編輯  收藏 所屬分類: memcache
          主站蜘蛛池模板: 新密市| 连云港市| 滨州市| 比如县| 凭祥市| 申扎县| 枞阳县| 金寨县| 安泽县| 临安市| 河津市| 廊坊市| 正定县| 乌苏市| 琼结县| 韶山市| 新建县| 屯门区| 玛纳斯县| 敖汉旗| 北辰区| 茌平县| 深泽县| 樟树市| 出国| 湖北省| 江孜县| 天津市| 临朐县| 云浮市| 寻甸| 郑州市| 望谟县| 凤山市| 东丽区| 科技| 崇文区| 阿克苏市| 讷河市| 无锡市| 平陆县|