4
Memcached
Calvin edited this page 2013-11-14 01:37:15 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##Memcached Memcached的设计,一般人看完会有洗脑的感觉,原来可以用这么简单的设计做到这么高效的事情。原作者进入Google撒手不管一段时间后,社区已渐渐接过了维护到工作,现在又开始不断更新了。

##SpyMemcached SpyMemcached是Memcahced client中较靠谱的一个,作者最近加入了CouchDB,Membase的公司,很多精力都在用Memcached协议访问这两者上了。另外XMemcached是另一个可选的国产的Client。

Spy的jar包只在它的Maven私服里有最新版本,Maven中央库没有,google code上的下载也不够新。

SpringSide的extension模块里封装了一个SpyMemcachedClient:

  1. get和getBulk,都屏蔽了异常,而且强行转换类型,如User = client.get("foo");
  2. set和delete都提供了更新后不管,以及等待updateTimeout毫秒,根据是否超时返回true or false的安全函数两种类型的函数。
  3. 透传的incr/decr,asyncIncr/asyncDecr, 其中同步的方法能设定原来无值时的默认值,而异步的不能。
  4. 还有一些未封装的函数如CAS,Add,Touch,Append/Prepend,可用getMemcachedClient()取出后调用。
  5. 应用关闭时自动调用shutdown。

在showcase的MemcachedDemo和applicationContext-memcached.xml有演示。

SpyMemcachedClient的初始设置,直接使用net.spy.memcached.spring.MemcachedClientFactoryBean创建并设置MemcachedClient。 注意协议默认是TEXT的,可改成二进制。Cluster locatorType默认是ARRAY_MOD,没有使用那著名的CONSISTENT,operationTimeout默认是2.5秒。

##JVM中的嵌入式Memcached模拟器 有些单元测试或不需要安装的集成测试,可能需要一个JVM内的嵌入式Memcached服务器,JMemcached是不错的选择。

SpringSide的test模块里,基于它封装了一个MemcachedSimulator。

在Showcase中,就使用了它,见applicationContext-memcached.xml。