这里举例使用spring3.1.4 + ehcache

注解的方式使用cache 是在spring3.1加入的

使用方法:

1.ehcache依赖+spring依赖

<!-- ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.7.2</version>
</dependency>
        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<!-- 这里有注解类需要的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>

      <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>2.2.2</version>
      </dependency>

 

2.spring+ehcache的简单配置

Ehcache的基本配置,具体参数意思看:http://www.ehcache.org/documentation/configuration/configuration

name

cache的唯一标识

maxElementsInMemory

最大创建条数

eternal

缓存是否是永久的,默认false

timeToLiveSeconds

缓存的存活时间

timeToIdleSeconds

多长时间不访问就清楚该缓存

overflowToDisk

内存不足是否写入磁盘,默认False

maxElementsOnDisk

持久化到硬盘最大条数

memoryStoreEvictionPolicy

缓存满了后,清除缓存的规则,

自带三种:FIFO(先进先出),LFU(最少使用),LRU(最近最少使用)

diskSpoolBufferSizeMB

磁盘缓存的缓存区大小,默认30M

diskExpiryThreadIntervalSeconds

磁盘失效线程时间间隔

2.1 首先建立一个ehcache.xml的配置文件

<ehcache>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/> <cache
name="onecache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"/>
</ehcache>

2.2在spring的apllication.xml 加入注入的cache

    <cache:annotation-driven cache-manager="cacheManager"/>

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="true"/>

这里还需要在配置文件头部引入

 xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
...
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
"

在mvc-servlet.xml 里加入

  <mvc:annotation-driven/>  <!-- 挺重要的,不加注解不会被扫描-->

3.注解使用

3.1 用于对象 class

@Cacheable(value = "onecache")
class A1{}

这种情况类中方法中返回值都会被缓存,

3.2用于方法method

 @Cacheable(value = "onecache", key = "#name",condition = "#age < 25")
public xx findEmployeeBySurname(String firstName, String name, int age) {
return xxx
}

这个就会将age小于25的值,按name为key缓存

3.3 清除

@CacheEvict(value="onecache",key="#name + 'haha'")
public void delete(String name) {
System.out.println("delete one name");
}

还可使用下面的清除全部

@CacheEvict(value="oneCache",allEntries=true)

4.代码调用

  @Autowired
private CacheManager cacheManager; //获取当前时间
public String getABCCache() {
cacheManager.getCache("ccc").put("hello", new Date().toString());
return (String) cacheManager.getCache("ccc").get("hello").get();
}

java spring 使用注解来实现缓存的更多相关文章

  1. java Spring 基于注解的配置(一)

    注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  2. Java 必须掌握的 20+ 种 Spring 常用注解

    Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @C ...

  3. Java 必须掌握的 12 种 Spring 常用注解!

    1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller ...

  4. spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除

    转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...

  5. spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...

  6. 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...

  7. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  8. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  9. 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)

    硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...

随机推荐

  1. util-判断当前年份所处的季度,并返回当前季度开始的月份

    ylbtech-funcation-util:  判断当前年份所处的季度,并返回当前季度开始的月份 判断当前年份所处的季度,并返回当前季度开始的月份. 1.A,Ylbtech.Model返回顶部 us ...

  2. webdriver(python)学习笔记四——定位一组元素

    webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. 定位一组对象一般用于以下场景: ...

  3. bzoj 1001: [BeiJing2006]狼抓兔子 平面图最小割

    平面图跑最大流 可以转换为其对偶图跑最短路 一个环对应一个割  找到最小环(即最短路)极为所求,注意辅助边的建立 加入读入优化  不过时间还是一般  估计是dij写的不好   大神勿喷~~~ /*** ...

  4. 浏览器URL传参最大长度问题

    这几天为解决一个BUG头疼了一段时间,BUG现象如下: 一个选择人员的选择控件,当选择多个人时(50多个的时候),返回没有错误现象,而再一次打开的时候就报404错误.看到这个错误非常纳闷,无法下手,只 ...

  5. container_of

    在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - cast ...

  6. 关于Tokenizer与TokenFilter的区别

    TokenStream是一个能在被调用后产生语汇单元流的类,但是 TokenStream 类有两个不同的类型:Tokenizer 类和 TokenFilter 类.这两个类都是从抽象类TokenStr ...

  7. Web开发人员需知的Web缓存知识

    最近的译文距今已有4年之久,原文有一定的更新.今天踩着前辈们的肩膀,再次把这篇文章翻译整理下.一来让自己对web缓存的理解更深刻些,二来让大家注意力稍稍转移下,不要整天HTML5, 面试题啊叨啊叨的~ ...

  8. 使用CocoaPods管理依赖库

    本篇内容将介绍Mac和iOS开发中必备的一个依赖库管理工具CocoaPods. CocoaPods是什么 在iOS开发中势必会用到一些第三方依赖库,比如大家都熟悉的ASIHttpRequest.AFN ...

  9. 用鼠标右键选择DataGridView单元格或行

    在datagirdview_cellmousedown事件中先将CurrentCell(或CurrentRow)的Selected属性设为false,然后将鼠标右键点击的单元格(或行)设为Curren ...

  10. Quality Center11初始化失败

    打开start_a.jsp页面总是闪退,原因如下: 初始化失败因为证书签名过期了.把IE选项里证书检查的三项勾掉就好了(检查发行商的证书是否吊销.检查服务器证书吊销.检查已下载的程序的签名)