阅读本文的前提是,你已经安装或者使用了MemCached,或具有相关的基本知识。
今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCached的客户端开发变得超乎寻常的简单,只要一行代码就行:
@ReadThroughAssignCache(assignedKey = "VETS", expiration = 300, namespace = "NELZ")
是不是很神奇?这行代码指定了MemCached的key,过期时间和命名空间。假设你的MemCached服务器IP是:196.168.10.101,端口是:12000,那么在数据调用的配置文件中只要加上下面配置代码就可以了:
<import resource="classpath:simplesm-context.xml" />
<bean id="memcachedConnectionBean" class="net.nelz.simplesm.config.MemcachedConnectionBean">
<property name="consistentHashing" value="true" />
<property name="nodeList" value="196.168.10.101:12000" />
</bean>
<bean id="memcachedClientFactory" class="net.nelz.simplesm.config.MemcachedClientFactory" >
<property name="bean" ref="memcachedConnectionBean" />
</bean>
<bean id="memcachedClient" factory-bean="memcachedClientFactory" factory-method="createMemcachedClient" />
<bean id="methodStore" class="net.nelz.simplesm.aop.CacheKeyMethodStoreImpl" />
<bean id="net.nelz.simplesm.DefaultKeyProvider" class="net.nelz.simplesm.impl.DefaultKeyProvider">
<property name="methodStore" ref="methodStore" />
</bean>
<bean id="readThroughSingleCache" class="net.nelz.simplesm.aop.ReadThroughSingleCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="readThroughMultiCache" class="net.nelz.simplesm.aop.ReadThroughMultiCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="readThroughAssignCache" class="net.nelz.simplesm.aop.ReadThroughAssignCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="updateSingleCache" class="net.nelz.simplesm.aop.UpdateSingleCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="updateMultiCache" class="net.nelz.simplesm.aop.UpdateMultiCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="updateAssignCache" class="net.nelz.simplesm.aop.UpdateAssignCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="invalidateSingleCache" class="net.nelz.simplesm.aop.InvalidateSingleCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="invalidateMultiCache" class="net.nelz.simplesm.aop.InvalidateMultiCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
<bean id="invalidateAssignCache" class="net.nelz.simplesm.aop.InvalidateAssignCacheAdvice">
<property name="cache" ref="memcachedClient" />
<property name="methodStore" ref="methodStore" />
<property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
</bean>
Simple-Spring-Memcached还提供了一个例子,在spring的petClinic例子中加入了几行代码,就实现了对MemCached的调用:
import net.nelz.simplesm.annotations.ReadThroughAssignCache;
import net.nelz.simplesm.annotations.ReadThroughSingleCache;
@ReadThroughAssignCache(assignedKey = "VETS", expiration = 300, namespace = "NELZ")
public Collection<Vet> getVets() {
System.out.println("\n ! ! !Gonna wait a bit: " + new Date() + "\n");
try {
Thread.sleep(4000);
} catch (Exception ex) {}
return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list();
}
为了加强测试的效果,在第一次读取数据时,故意停顿了一下(sleep)。