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中较靠谱的一个作者最近加入了CouchDBMembase的公司很多精力都在用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. 还有一些未封装的函数如CASAdd,TouchAppend/Prepend可用getMemcachedClient()取出后调用。
  5. 应用关闭时自动调用shutdown。

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

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

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

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

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