现在的位置: 首页 > 综合 > 正文

a simple memcached client Demo

2013年09月11日 ⁄ 综合 ⁄ 共 2623字 ⁄ 字号 评论关闭
在当前的一个语音系统中,需要频繁进行I/O操作, 为了应付未来可能出现的高并发访问, 初步计划引入缓存机制。
在诸多缓存中, ehcache口碑不错, 我之前也写过一篇文章介绍ehcache的使用:
http://lcllcl987.javaeye.com/blog/222693

 但ehcache不是一个分布式缓存解决方案。
所以, 就初选memcached了:
http://www.danga.com/memcached/
 
memcached的安装交给google,略过不表。
 

下面是两个memcached client的java实现:
http://www.whalin.com/memcached/
http://code.google.com/p/spymemcached/


http://www.whalin.com/memcached/
为例, 它的文档不错, 就不多说了。 test如下:

  1. package co.cache;
  2. import com.danga.MemCached.MemCachedClient;
  3. import com.danga.MemCached.SockIOPool;
  4. public class MyMemCachedClient
  5. {
  6.     // create a static client as most installs only need
  7.     // a single instance
  8.     private static MemCachedClient memCachedClient = new MemCachedClient();
  9.     // set up connection pool once at class load
  10.     static
  11.     {
  12.         // server list and weights
  13.         String[] servers = {"192.168.0.55:11211"};
  14.         Integer[] weights = {3};
  15.         // grab an instance of our connection pool
  16.         SockIOPool pool = SockIOPool.getInstance();
  17.         // set the servers and the weights
  18.         pool.setServers(servers);
  19.         pool.setWeights(weights);
  20.         // set some basic pool settings
  21.         // 5 initial, 5 min, and 250 max conns
  22.         // and set the max idle time for a conn
  23.         // to 6 hours
  24.         pool.setInitConn(5);
  25.         pool.setMinConn(5);
  26.         pool.setMaxConn(250);
  27.         pool.setMaxIdle(1000 * 60 * 60 * 6);
  28.         // set the sleep for the maint thread
  29.         // it will wake up every x seconds and
  30.         // maintain the pool size
  31.         pool.setMaintSleep(30);
  32.         // set some TCP settings
  33.         // disable nagle
  34.         // set the read timeout to 3 secs
  35.         // and don't set a connect timeout
  36.         pool.setNagle(false);
  37.         pool.setSocketTO(3000);
  38.         pool.setSocketConnectTO(0);
  39.         // initialize the connection pool
  40.         pool.initialize();
  41.         // lets set some compression on for the client
  42.         // compress anything larger than 64k
  43.         memCachedClient.setCompressEnable(true);
  44.         memCachedClient.setCompressThreshold(64 * 1024);
  45.     }
  46.     
  47.     public static MemCachedClient getMemCachedClient()
  48.     {
  49.         return memCachedClient;
  50.     }
  51.     // from here on down, you can call any of the client calls
  52.     public static void examples()
  53.     {
  54.         memCachedClient.set("foo""This is a test String");
  55.         String bar = (String) memCachedClient.get("foo");
  56.     }
  57.     
  58.     public static void main(String[] args)
  59.     {
  60.         MyMemCachedClient.getMemCachedClient().set("foo""This is a test String.");
  61.         String bar = (String)MyMemCachedClient.getMemCachedClient().get("foo");
  62.         System.out.println(bar);
  63.     }
  64. }
  65. 和ehcache不同的是,memcached似乎只采用LRU算法, 还对付cache满员的情况。

抱歉!评论已关闭.