彩票走势图

如何简单使用MemCached

转帖|其它|编辑:郝浩|2010-10-29 14:08:20.000|阅读 1396 次

概述:阅读本文的前提是,你已经安装或者使用了MemCached,或具有相关的基本知识。今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCached的客户端开发变得超乎寻常的简单。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  阅读本文的前提是,你已经安装或者使用了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>

从simplesm-context.xml的内容中,可以看出它所封装的类和方法:

<bean id="memcachedClientFactory" class="net.nelz.simplesm.config.MemcachedClientFactory" >

<property name=&quot;bean" ref="memcachedConnectionBean&quot; />

</bean>

<bean id="memcachedClient" factory-bean="memcachedClientFactory" factory-method="createMemcachedClient" />

<bean id="methodStore&quot; class="net.nelz.simplesm.aop.CacheKeyMethodStoreImpl" />

<bean id="net.nelz.simplesm.DefaultKeyProvider" class="net.nelz.simplesm.impl.DefaultKeyProvider">

&lt;property name="methodStore" ref="methodStore" />

</bean>

<bean id="readThroughSingleCache" class="net.nelz.simplesm.aop.ReadThroughSingleCacheAdvice">

<property name="cache" ref="memcachedClient" />

<property name="methodStore" ref=&quot;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" /&gt;

</bean>

<bean id="readThroughAssignCache" class="net.nelz.simplesm.aop.ReadThroughAssignCacheAdvice">

<property name="cache" ref="memcachedClient" />

<property name=&quot;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&quot; /&gt;

<property name="methodStore" ref="methodStore&quot; />

<property name="defaultKeyProvider&quot; ref="net.nelz.simplesm.DefaultKeyProvider" />

</bean>

<bean id="updateAssignCache" class="net.nelz.simplesm.aop.UpdateAssignCacheAdvice">

<property name="cache" ref="memcachedClient&quot; />

<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&quot; />

<property name="methodStore" ref="methodStore" />

<property name=&quot;defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />

</bean>

<bean id="invalidateMultiCache" class="net.nelz.simplesm.aop.InvalidateMultiCacheAdvice">

<property name="cache" ref="memcachedClient&quot; />

<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();

}

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP