这里举例使用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. Source Insight中文乱码

    搜索都是c++的代码,本来想下一个Vc,想了下,决定下个eclipse for C++,anyway,n次n久时间下载失败后,我接受了推荐,先下了个Source Insight来看代码.然后问题就出现 ...

  2. Casperjs/PhantomJs 中文网站截图乱码

    使用CasperJs进行自动化测试中文网站的时候发现中文网站截图会出现乱码的现象,中文汉字被一个个小方框代替 查找了一些资料发现是因为Linux服务器上没有安装中文字体导致的,Linux如何安装中文字 ...

  3. ASP.NET常用加密解密方法

    ASP.NET常用加密解密方法 一.MD5加密解密 1.加密 C# 代码           public static string ToMd5(string clearString)        ...

  4. Javascript原理

    1.javascript创建对象 创建新对象有两种不同的方法: 定义并创建对象的实例 person=new Object(); person.firstname="Bill"; p ...

  5. 在Windows上,如何卸载RabbitMQ服务

    打开运行->CMD->sc delete RabbitMQ 如果报错..... 打开运行->regedit 找到RabbitMQ节点,删掉即可.(右侧看到的都是启动服务时,需要的配置 ...

  6. usb mass storage device

    Problem adding USB host device to KVM Windows guest machine. Status: CLOSED CURRENTRELEASE   Aliases ...

  7. IMAQ Flatten Image to String VI的参数设置对比

    无压缩 jpeg压缩 无损二元包装 仅JPEG压缩时有效 平化类型(指定字符串中存储什么类型的数据)   None JPEG PACKED BINARY Quality Image Image and ...

  8. zoj 1610 Count the Colors

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610  Count the Colors Time Limit:2000MS   ...

  9. jquery easyui鼠标右击显示自定义的菜单

    1.datagrid表格中,对某一行鼠标右击,显示出如下的自定义的菜单: 在html页面中写: <div id="menu" class="easyui-menu& ...

  10. 当心回车符破坏你的JSON数据

    今天发现系统中一个地方获取JSON数据时,时而失败,时而成功,最后发现是回车符搞的鬼. 当你的JSON中有回车符时,会致使你的JSON出现格式错误:解决办法是在保存数据,或整理数据向客户端输出时将回车 ...