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

来源:互联网 发布:java简历包装真实项目 编辑:程序博客网 时间:2024/05/17 03:24
之前自己写过一个通过注解和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 plaincopyprint?
    1. @ReadThroughSingleCache(namespace ="Alpha", expiration = 30
    2.     public String getDateString(@ParameterValueKeyProviderfinal 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 plaincopyprint?
    1. @InvalidateSingleCache(namespace ="Charlie"
    2.     public void updateRandomString(@ParameterValueKeyProviderfinal 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 plaincopyprint?
  1. @UpdateSingleCache(namespace = "Alpha", expiration = 30
  2.     public void overrideDateString(finalint trash, @ParameterValueKeyProviderfinal String key, 
  3.             @ParameterDataUpdateContentfinal String overrideData) { 
  4.     } 
  5.  
  6.     @UpdateSingleCache(namespace = "Bravo", expiration = 300
  7.     @ReturnDataUpdateContent 
  8.     public String updateTimestampValue(@ParameterValueKeyProviderfinal 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 plaincopyprint?
  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
原创粉丝点击