---
title: "simple-spring-memcached集成memcache"
author: "younglibin"
date: "2014-11-18T16:19:11Z"
tags: []
categories: ["编程语言"]
slug: "simple-spring-memcached-memcache"
draft: false
lastmod: "2014-11-20T15:41:58Z"
id: 2157666
url: "https://younglibin.iteye.com/blog/2157666"
---

001配置

* * *

POM 添加：

 

&lt;dependency&gt;

   &lt;groupId&gt;com.googlecode.xmemcached&lt;/groupId&gt;

   &lt;artifactId&gt;xmemcached&lt;/artifactId&gt;

   &lt;version&gt;1.3.5&lt;/version&gt;

  &lt;/dependency&gt;

  &lt;dependency&gt;

   &lt;groupId&gt;com.google.code.simple-spring-memcached&lt;/groupId&gt;

   &lt;artifactId&gt;simple-spring-memcached&lt;/artifactId&gt;

   &lt;version&gt;2.0.0&lt;/version&gt;

  &lt;/dependency&gt;

 

app添加：

 

 

&lt;import resource="classpath:simplesm-context.xml" /&gt;

 

 

&lt;bean name="cardtypeIcon" class="com.google.code.ssm.CacheFactory"&gt;

  &lt;property name="cacheClientFactory"&gt;

   &lt;bean

    class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /&gt;

  &lt;/property&gt;

  &lt;property name="addressProvider"&gt;

   &lt;bean class="com.google.code.ssm.config.DefaultAddressProvider"&gt;

    &lt;property name="address"

     value="192.168.232.201:11111,192.168.232.201:11112" /&gt;

   &lt;/bean&gt;

  &lt;/property&gt;

  &lt;property name="configuration"&gt;

   &lt;bean

    class="com.google.code.ssm.providers.xmemcached.XMemcachedConfiguration"&gt;

    &lt;!--是否使用一致性哈希 --&gt;

    &lt;property name="consistentHashing" value="true" /&gt;

    &lt;property name="operationTimeout" value="10000" /&gt;

   &lt;/bean&gt;

  &lt;/property&gt;

  &lt;property name="cacheName"&gt;

   &lt;value&gt;cardtypeIcon&lt;/value&gt;

  &lt;/property&gt;

 &lt;/bean&gt;

 

 

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 ， 方便测试，如果熟悉命令行的话， 可以不需要这个！