使用SSM注解做缓存操作

之前自己写过一个通过注解和AOP来实现缓存的代码,最早的原型来自于在淘宝工作时的一个项目。当时这段代码写得比较差,之后重构时发现之前的功能实现有很大的局限。主要问题在于:

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

最近同事推荐了一个开源项目:Simple-Spring-Memcached(简称ssm),它也是一个通过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方法
    代码示例:

     

        @ReadThroughSingleCache(namespace = "Alpha", expiration = 30)
        public String getDateString(@ParameterValueKeyProvider final String key) {
            final Date now = new Date();
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ex) {
            }
            return now.toString() + ":" + now.getTime();
        }
  • InvalidateSingleCache
    作用:失效Cache中的数据
    key生成规则:

     

    • 使用 ParameterValueKeyProvider注解时,与ReadThroughSingleCache一致
    • 使用 ReturnValueKeyProvider 注解时,key为返回的对象的CacheKeyMethod或toString方法生成
    @InvalidateSingleCache(namespace = "Charlie")
    public void updateRandomString(@ParameterValueKeyProvider final Long key) {
        // Nothing really to do here.
    }

    @InvalidateSingleCache(namespace = "Charlie")
    @ReturnValueKeyProvider
    public Long updateRandomStringAgain(final Long key) {
        return key;
    }
  • UpdateSingleCache
    作用:更新Cache中的数据
    key生成规则:ParameterValueKeyProvider指定
    ParameterDataUpdateContent:方法参数中的数据,作为更新缓存的数据
    ReturnDataUpdateContent:方法调用后生成的数据,作为更新缓存的数据
    注:上述两个注解,必须与Update*系列的注解一起使用

     

        @UpdateSingleCache(namespace = "Alpha", expiration = 30)
        public void overrideDateString(final int trash, @ParameterValueKeyProvider final String key,
                @ParameterDataUpdateContent final String overrideData) {
        }
    
        @UpdateSingleCache(namespace = "Bravo", expiration = 300)
        @ReturnDataUpdateContent
        public String updateTimestampValue(@ParameterValueKeyProvider final Long key) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
            final Long now = new Date().getTime();
            final String result = now.toString() + "-U-" + key.toString();
            return result;
        }
  • ReadThroughAssignCache
    作用:读取Cache中数据,如果不存在,则将读取的数据存入Cache
    key生成规则: ReadThroughAssignCache 注解中的 assignedKey 字段指定

     

        @ReadThroughAssignCache(assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000)
        public List<String> getAssignStrings() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
            }
            final List<String> results = new ArrayList<String>();
            final long extra = System.currentTimeMillis() % 20;
            final String base = System.currentTimeMillis() + "";
            for (int ix = 0; ix < 20 + extra; ix++) {
                results.add(ix + "-" + base);
            }
            return results;
        }
  • 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) {
        }
    http://www.colorfuldays.org/program/java/ssm_memcache/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值