这里举例使用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. C语言内存地址基础

    来源:http://blog.jobbole.com/44845/ 从计算机内存的角度思考C语言中的一切东东,是挺有帮助的.我们可以把计算机内存想象成一个字节数组,内存中每一个地址表示 1 字节.比方 ...

  2. HNU 13108-Just Another Knapsack Problem (ac自动机上的dp)

    题意: 给你一个母串,多个模式串及其价值,求用模式串拼接成母串(不重叠不遗漏),能获得的最大价值. 分析: ac自动机中,在字典树上查找时,用dp,dp[i]拼成母串以i为结尾的子串,获得的最大价值, ...

  3. 写给Python初学者的设计模式入门

    有没有想过设计模式到底是什么?通过本文可以看到设计模式为什么这么重要,通过几个Python的示例展示为什么需要设计模式,以及如何使用. 设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的 ...

  4. (转载)OC学习篇之---第一个程序HelloWorld

    之前的一片文章简单的介绍了OC的相关概述,从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这 ...

  5. Winxp下搭建SVN服务器

    本文介绍一种在winxp下搭建SVN服务器的方法. (1) 需要下载Slik-Subversion和TortoiseSVN两个软件.我使用的版本是Slik-Subversion-1.8.3-1-win ...

  6. Distributed Sentence Similarity Base on Word Mover's Distance

    Algorithm: Refrence from one ICML15 paper: Word Mover's Distance. 1. First use Google's word2vec too ...

  7. leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)

    https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...

  8. Spark生态之Spark Core

  9. homework-03

    1.分工准备 这次的工作是结对编程,在第二次作业中我是使用python完成的作业,而小明是使用C完成的作业.因为打算使用动态链接库的方式将第二次的代码嵌入到本次的作业中,而python生成动态链接库不 ...

  10. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...