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) {
    }

memcached与spring的更多相关文章

  1. 使用Memcached、Spring AOP构建数据库前端缓存框架

    数据库访问可能是很多网站的瓶颈.动不动就连接池耗尽.内存溢出等.前面已经讲到如果我们的网站是一个分布式的大型站点,那么使用 memcached实现数据库的前端缓存是个很不错的选择:但如果网站本身足够小 ...

  2. memcached与spring集成

    一.背景 销售CRM(项目A)将负责管理项目信息系统(项目B)的支付与权限 上级要求为避免频繁调用CRM接口,中间放一级缓存,但要做到缓存中保证最新数据 因项目B已使用memcache作缓存,所以决定 ...

  3. Memcached与Spring集成的方式(待实践)

    主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...

  4. Memcached和Spring集成开发

    xml配置文件 <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" facto ...

  5. Memcached基础知识

    主要内容: Memcached基本的工作原理 Memcached的两阶段哈希 Memcached的数据存储方式 Memcached新建Item分配内存过程 Memcached的数据过期方式 Memca ...

  6. 关于 Memcached 的一些使用

    关于Memcached的一些用法, Memcached 在Windows下的版本费了很大劲,才找到.win32,win64都有.本来想自己build的,但是Cygwin下载包也是费老劲了,下不下来. ...

  7. 一篇文章让你了解并掌握memcached

    第一章 memcached简介 1.1为什么引入memcached 随着数据量的增大,访问的集中,REBMS负担加重,数据库响应恶化. Memcached是高性能的分布式内存缓存服务器,目的是通过缓存 ...

  8. Spring Boot:快速入门教程

    什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  9. Spring Boot 教程

    Spring Boot 系列教程: Spring Boot:快速入门教程 Spring Boot:整合Swagger文档 Spring Boot:整合MyBatis框架 Spring Boot:实现M ...

随机推荐

  1. Linux下RPM、tar.gz、DEB格式软件包的区别

      初接解Linux的朋友一定对软件的安装特别头疼,同样都是for Linux,但RPM.tar.gz.DEB包还是有很大区别的,这种区别很有可能使你的安装过程进行不下去.那我们应该下载什么格式的包呢 ...

  2. 菜鸟学Linux命令:find命令 查找文件

    find命令是Linux下最常用的命令之一,灵活的使用find命令,你会发现查找文件变得十分简单. 命令格式 find [指定查找目录]  [查找规则(选项)]  [查找完后执行的动作] 参数规则 - ...

  3. phpstorm 8 license key

    Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!XVcxFChJ2gTUR2lZtlLXrPLb ...

  4. Java Hour6

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为5 Hour,请各位不吝赐教. Hour6 Jav ...

  5. GridView块布局

    <GridView android:id="@+id/gridview" android:layout_width="match_parent" andr ...

  6. jq查找父类元素三个函数的区别

    parent是找当前元素的第一个父节点,parents是找当前元素的所有父节点 parent().parents()与closest()方法两两之间有类似又有不同,本篇简短的区分一下这三个方法.通过本 ...

  7. Eclipse 导入 Hadoop 源码

    1.准备工作 jdk: eclipse: Maven: libprotoc :https://developers.google.com/protocol-buffers/ hadoop:http:/ ...

  8. JVM常用参数配置

    Trace跟踪参数 -verbose:gc -XX:+printGC 打印GC的简要信息 -XX:+PrintGCDetails 打印GC详细信息 -XX:+PrintGCTimeStamps 打印C ...

  9. Visual Studio找不到iOS模拟器

    Visual Studio找不到iOS模拟器 Visual Studio可以正常连接Mac系统,但是在测试时候,提示以下错误信息:Failed to start iOS Simulator in th ...

  10. DFS HDOJ 2614 Beat

    题目传送门 /* 题意:处理完i问题后去处理j问题,要满足a[i][j] <= a[j][k],问最多能有多少问题可以解决 DFS简单题:以每次处理的问题作为过程(即行数),最多能解决n个问题, ...