001配置
POM 添加:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>simple-spring-memcached</artifactId>
<version>2.0.0</version>
</dependency>
app添加:
<import resource="classpath:simplesm-context.xml" />
<bean name="cardtypeIcon" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean
class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address"
value="192.168.232.201:11111,192.168.232.201:11112" />
</bean>
</property>
<property name="configuration">
<bean
class="com.google.code.ssm.providers.xmemcached.XMemcachedConfiguration">
<!--是否使用一致性哈希 -->
<property name="consistentHashing" value="true" />
<property name="operationTimeout" value="10000" />
</bean>
</property>
<property name="cacheName">
<value>cardtypeIcon</value>
</property>
</bean>
java 代码:
/**
* 通过cache查询
*
* @author libin
* @param cardtypeId
* memcached map 对应的key
* @return
*/
@ReadThroughSingleCache(namespace = "yazuo.cardtypeIcon", expiration = 60) //失效时间单位秒
@CacheName("cardtypeIcon")
public String queryCardtypeIcon(
@ParameterValueKeyProvider Integer cardtypeId) {
CardtypeIcon cardtypeIcon = new CardtypeIcon();
cardtypeIcon.setCardtypeId(cardtypeId);
cardtypeIcon = cardtypeIconService.selectCardtypeIconByID(cardtypeIcon);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != cardtypeIcon) {
return cardtypeIcon.getIcon();
}
return "";
}
执行之后再cache 中可以使用: get
yazuo.cardtypeIcon:
cardtypeId
yazuo.cardtypeIcon:是
namespace
cardtypeId:是传过来的ID
例如:
get
yazuo.cardtypeIcon:50
get yazuo.cardtypeIcon:50
VALUE yazuo.cardtypeIcon:50 0 24
55185390502646010083.png
END
遗留问题:
1、 怎么查看 cache 设置的失效时间 命令行
2、 怎么设置到从数据库查询对象为空的时候, 不放入缓存,使用 ReadThroughSingleCache 怎么设置当返回对象为空,不放入缓存
解决:按照ssm注解的意思是,只要数据库执行成功就能放到缓存, 执行失败就不放入缓存了, 所以可以采取抛出异常的方式,放整个方法失败, 这样就不会放入缓存了!
3、使用注解怎么在程序启动的时候, 加载数据库的数据到缓存
002-接口
1、增加或者更新
/**
* 通过cache查询
*
* @author libin
* @param cardtypeId
* memcached map 对应的key
* @return
*/
@ReadThroughSingleCache(namespace = WebCommonContents.CARDTYPEICON_CACHE, expiration = WebCommonContents.CARDTYPEICON_EXPIRATION)
@CacheName("cardtypeIcon")
public String queryCardtypeIcon(
@ParameterValueKeyProvider Integer cardtypeId) {
CardtypeIcon cardtypeIcon = new CardtypeIcon();
cardtypeIcon.setCardtypeId(cardtypeId);
cardtypeIcon = cardtypeIconService.selectCardtypeIconByID(cardtypeIcon);
if (null != cardtypeIcon) {
return cardtypeIcon.getIcon();
}
// TODO 返回空未处理
return "";
}
/**
* @author libin
* @return
*/
@UpdateSingleCache(namespace = WebCommonContents.CARDTYPEICON_CACHE, expiration = WebCommonContents.CARDTYPEICON_EXPIRATION)
@CacheName("cardtypeIcon")
@Override
public void updateCardtypeIcon(
@ParameterValueKeyProvider Integer cardtypeId,
@ParameterDataUpdateContent String icon) {
CardtypeIcon cardtypeIcon = new CardtypeIcon();
cardtypeIcon.setCardtypeId(cardtypeId);
cardtypeIcon = cardtypeIconService.selectCardtypeIconByID(cardtypeIcon);
if (null == cardtypeIcon) {
cardtypeIcon = new CardtypeIcon();
cardtypeIcon.setCardtypeId(cardtypeId);
cardtypeIcon.setIcon(icon);
cardtypeIcon.setLastTime(new Timestamp(System.currentTimeMillis()));
cardtypeIconService.addCardtypeIcon(cardtypeIcon);
} else {
cardtypeIcon.setIcon(icon);
cardtypeIcon.setLastTime(new Timestamp(System.currentTimeMillis()));
cardtypeIconService.updateCardtypeIconByID(cardtypeIcon);
}
}
/**
* @author libin
* @return
*/
@InvalidateSingleCache(namespace = WebCommonContents.CARDTYPEICON_CACHE)
@CacheName("cardtypeIcon")
@Override
public void deleteIcon(@ParameterValueKeyProvider Integer cardtypeId) {
CardtypeIcon cardtypeIcon = new CardtypeIcon();
cardtypeIcon.setCardtypeId(cardtypeId);
cardtypeIconService.removeCardtypeIconByObject(cardtypeIcon);
}
spring 中使用到的注解:
@CacheKeyMethod
@ReadThroughSingleCache
, @ReadThroughMultiCache
, @ReadThroughAssignCache
@InvalidateSingleCache
, @InvalidateMultiCache
, @InvalidateAssignCache
@UpdateSingleCache
, @UpdateMultiCache
, @UpdateAssignCache
@ReadCounterFromCache
@IncrementCounterInCache
, @DecrementCounterInCache
@UpdateCounterInCache
https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started#Spring_3.1_Cache_Integration
切入点是通过标签的方式来进行声明的,在项目开发时,通常在DAO的方法上加以相应的标签描述,来表示组件对该方法的拦截
组件所提供的切入点主要包括以下几种:
ReadThroughSingleCache、ReadThroughMultiCache、ReadThroughAssignCache
当遇到查询方法声明这些切入点时,组件首先会从缓存中读取数据,取到数据则跳过查询方法,直接返回。
取不到数据在执行查询方法,并将查询结果放入缓存,以便下一次获取。
InvalidateSingleCache、InvalidateMultiCache、InvalidateAssignCache
当遇到删除方法声明这些切入点时,组件会删除缓存中的对应实体
UpdateSingleCache、UpdateMultiCache、UpdateAssignCache
当遇到更新方法声明这些切入点是,组件会更新缓存中对应的实体,以便下次从缓存中读取出的数据状态是最新的
simple-spring-memcached本身并不提供cache机制的实现,只是为了cache的调用更加简单而设计的。
在cache的实现上使用的是第三方组件(如x-memcached和spy-memcached),官方给出了针对这两种组件的相关配置
附件提供一个 java 客户端,可以查询 memcached 中的key 和value , 方便测试,如果熟悉命令行的话, 可以不需要这个!