使用Simple-Spring-Memcached注解做缓存操作

 之前自己写过一个通过注解和AOP来实现缓存的代码,当时这段代码写得比较简单,之后重构时发现之前的功能实现有很大的局限。主要问题在于:

  1. key的生成规则
  2. update 与 query 的参数不一样,如何让其生成一样的key
  3. 列表缓存如何定义key及失效

最近同事推荐了一个开源项目:Simple-Spring-Memcached,它也是一个通过Annatation与AOP来完成缓存数据操作的开源项目。仔细看了一下代码,基本上把我之前碰到的问题都解决了,而且MultiCache这一块的实现超出我的预期。该项目主要优点如下:

  1. 与Spring完善集成
  2. 支持两种Memcached Java Client (spymemcached,Xmemcached)
  3. 基于Annotation方式实现缓存操作,对代码侵入性小
  4. annotation丰富,可以满足绝大部分需求

下面介绍一下其中各annotation的使用。ssm项目中的Annotation主要分成以下几类

  • SingleCache类 操作单个POJO的Cache数据,由ParameterValueKeyProvider和CacheKeyMethod来标识组装key
  • MultiCache类 操作List型的Cache数据,由ParameterValueKeyProvider和CacheKeyMethod来标识组装key
  • AssignCache类 指定key操作Cache数据,由annotation中的 assignedKey 指定key

各Annotation的详细说明

  • ReadThroughSingleCache
    作用:读取Cache中数据,如果不存在,则将读取的数据存入Cache
    key生成规则:ParameterValueKeyProvider指定的参数,如果该参数对象中包含CacheKeyMethod注解的方法,则调用其方法,否则调用toString方法
    代码示例:
  • [java]  view plain copy
    1. @ReadThroughSingleCache(namespace = "Alpha", expiration = 30)  
    2.     public String getDateString(@ParameterValueKeyProvider final String key) {  
    3.         final Date now = new Date();  
    4.         try {  
    5.             Thread.sleep(1500);  
    6.         } catch (InterruptedException ex) {  
    7.         }  
    8.         return now.toString() + ":" + now.getTime();  
    9.     }  

    InvalidateSingleCache
    作用:失效Cache中的数据
    key生成规则:

    • 使用 ParameterValueKeyProvider注解时,与ReadThroughSingleCache一致
    • 使用 ReturnValueKeyProvider 注解时,key为返回的对象的CacheKeyMethod或toString方法生成
    [java]  view plain copy
    1. @InvalidateSingleCache(namespace = "Charlie")  
    2.     public void updateRandomString(@ParameterValueKeyProvider final Long key) {  
    3.         // Nothing really to do here.  
    4.     }  
    5.   
    6.     @InvalidateSingleCache(namespace = "Charlie")  
    7.     @ReturnValueKeyProvider  
    8.     public Long updateRandomStringAgain(final Long key) {  
    9.         return key;  
    10.     }  

    UpdateSingleCache
    作用:更新Cache中的数据
    key生成规则:ParameterValueKeyProvider指定
    ParameterDataUpdateContent:方法参数中的数据,作为更新缓存的数据
    ReturnDataUpdateContent:方法调用后生成的数据,作为更新缓存的数据
    注:上述两个注解,必须与Update*系列的注解一起使用
[java]  view plain copy
  1. @UpdateSingleCache(namespace = "Alpha", expiration = 30)  
  2.     public void overrideDateString(final int trash, @ParameterValueKeyProvider final String key,  
  3.             @ParameterDataUpdateContent final String overrideData) {  
  4.     }  
  5.   
  6.     @UpdateSingleCache(namespace = "Bravo", expiration = 300)  
  7.     @ReturnDataUpdateContent  
  8.     public String updateTimestampValue(@ParameterValueKeyProvider final Long key) {  
  9.         try {  
  10.             Thread.sleep(100);  
  11.         } catch (InterruptedException ex) {  
  12.         }  
  13.         final Long now = new Date().getTime();  
  14.         final String result = now.toString() + "-U-" + key.toString();  
  15.         return result;  
  16.     }  

ReadThroughAssignCache
作用:读取Cache中数据,如果不存在,则将读取的数据存入Cache
key生成规则: ReadThroughAssignCache 注解中的 assignedKey 字段指定

  
  
[java] view plain copy
  1. @ReadThroughAssignCache(assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000)  
  2.     public List<String> getAssignStrings() {  
  3.         try {  
  4.             Thread.sleep(500);  
  5.         } catch (InterruptedException ex) {  
  6.         }  
  7.         final List<String> results = new ArrayList<String>();  
  8.         final long extra = System.currentTimeMillis() % 20;  
  9.         final String base = System.currentTimeMillis() + "";  
  10.         for (int ix = 0; ix < 20 + extra; ix++) {  
  11.             results.add(ix + "-" + base);  
  12.         }  
  13.         return results;  
  14.     }  
InvalidateAssignCache
作用:失效缓存中指定key的数据
key生成规则:assignedKey 字段指定
@InvalidateAssignCache(assignedKey = "SomePhatKey", namespace = "Echo")
    public void invalidateAssignStrings() {
    }
UpdateAssignCache
作用:更新指定缓存
key生成规则:assignedKey 字段指定
@UpdateAssignCache(assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000)
    public void updateAssignStrings(int bubpkus, @ParameterDataUpdateContent final List<String> newData) {
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值