转自:http://www.coin163.com/it/490594393324999265/spring-ehcache

Ehcache在很多项目中都出现过,用法也比较简单。一般的加些配置就可以了,而且Ehcache可以对页面、对象、数据进行缓存,同时支持集群/分布式缓存。如果整合Spring、Hibernate也非常的简单,Spring对Ehcache的支持也非常好。EHCache支持内存和磁盘的缓存,支持

主会场地址https://www.aliyun.com/1111/2019/home

版本:Spring3.0.6 准备工作: 下载 ehcache-spring-annotations-1.2.0 http://code.google.com/p/ehcache-spring-annotations/downloads/list

下载完加压后里面的lib下的jar统统添加到classpath中 在资源文件夹下(通常是src/main/resources) 添加 ehcache.xml 内容如下 [html]

view plain

copy

print ? <?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

updateCheck="false">

<diskStore path="java.io.tmpdir" />

<defaultCache eternal="false"

maxElementsInMemory="1000"

overflowToDisk="false"

diskPersistent="false"

timeToIdleSeconds="0"

timeToLiveSeconds="600"

memoryStoreEvictionPolicy="LFU" />

<cache name="baseCache"

eternal="false"

maxElementsInMemory="500"

overflowToDisk="false"

diskPersistent="false"

timeToIdleSeconds="0"

timeToLiveSeconds="300"

memoryStoreEvictionPolicy="LFU" />

</ehcache>

这里是定义缓存策略 eternal="false"

// 元素是否永恒,如果是就永不过期(必须设置)

maxElementsInMemory="1000" // 缓存容量的内存最大值(必须设置)

overflowToDisk="false"

// 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)

diskPersistent="false"

// 磁盘缓存在VM重新启动时是否保持(默认为false)

timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0

timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期

memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU

然后在添加 cache-config.xml 内容如下: [html]

view plain

copy

print ? <beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:ehcache="http://ehcache-spr

ing-annotations.googlecode.com/svn/schema/ehcache-spring"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring

http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven />

<ehcache:config cache-manager="cacheManager">

<ehcache:evict-expired-elements

interval="60" />

</ehcache:config>

<bean id="cacheManager"

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

<property name="configLocation" value="classpath:ehcache.xml" />

</bean>

</beans>

然后在DAO层做如下配置: [java]

view plain

copy

print ?

@TriggersRemove(cacheName="baseCache",removeAll=true)

public Entity save(Entity entity) throws CrudException {

return entity;

}

@TriggersRemove(cacheName="baseCache",removeAll=true)

public Entity update(Entity entity) throws CrudException {

return entity;

}

@TriggersRemove(cacheName="baseCache",removeAll=true)

public void del(Entity entity) throws CrudException {

}

@Cacheable(cacheName="baseCache")

@SuppressWarnings("unchecked")

public List<Entity> findAll() throws SearchException {

return list;

}

@Cacheable(cacheName="baseCache") 这个注解就是做到缓存数据,cacheName对应ehcache.xml 中配置

@TriggersRemove(cacheName="baseCache",removeAll=true) 这个注解的作用就是当数据发生变化的时候清除缓存,做到数据同步

如何知道有没生效: 新建一个单元测试,在类中添加两个搜索的方法,然后执行这个单元测试,然后查看两个方法执行的时间(Eclipse的单元测试可以查看) 如果第二个搜索方法的运行时间为0那就说吗成功了,如图

参考: http://www.blogjava.net/zzzlyr/articles/343234.html http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/

(英文的,推荐) RE : http://blog.csdn.net/thc1987/article/details/7345816

原文

Spring MVC3 + Ehcache 缓存实现的更多相关文章

  1. 缓存插件 Spring支持EHCache缓存

    Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能.其中,spring对EHCache提供了很好的支持. 在介绍Spring的缓 ...

  2. 以Spring整合EhCache为例从根本上了解Spring缓存这件事(转)

    前两节"Spring缓存抽象"和"基于注解驱动的缓存"是为了更加清晰的了解Spring缓存机制,整合任何一个缓存实现或者叫缓存供应商都应该了解并清楚前两节,如果 ...

  3. spring+shiro+ehcache整合

    1.导入jar包(pom.xml文件) <!-- ehcache缓存框架 --> <dependency> <groupId>net.sf.ehcache</ ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

  6. Spring自定义缓存管理及配置Ehcache缓存

    spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...

  7. 使用Spring提供的缓存抽象机制整合EHCache为项目提供二级缓存

      Spring自身并没有实现缓存解决方案,但是对缓存管理功能提供了声明式的支持,能够与多种流行的缓存实现进行集成. Spring Cache是作用在方法上的(不能理解为只注解在方法上),其核心思想是 ...

  8. Spring 集成 Ehcache 开启缓存

    1.jar包 1.1.slf4j-api-1.6.1.jar 1.2.ehcache-2.7.0.jar 1.3.spring-core-3.2.0.RELEASE.jar 1.4.spring-co ...

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

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

随机推荐

  1. maven的核心概念——继承

    15.1 为什么需要继承机制 由于非compile范围的依赖信息是不能在“依赖链”中传递的,所以有需要的工程只能单独配置.例如: Hello <dependency> <groupI ...

  2. Spring MVC 定时任务注解说明

    一.注解说明. Spring 自带的定时任务执行@Scheduled注解,可以定时的.周期性的执行一些任务.查看@Scheduled的注解可以看到有以下三种: 1.1 String cron() de ...

  3. 解决问题:当redis服务端断开的时候`进程会崩溃(转载6哥笔记)

    package main import ( "fmt" "github.com/astaxie/beego/logs" "github.com/gar ...

  4. Selenium-浏览器兼容性测试自动化

    Selenium 使浏览器兼容性测试自动化成为可能,但是在不同的浏览器上依然有细微的差别 测试浏览器的兼容性--测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上. 测试系统功能--创建回 ...

  5. tensor维度变换

    维度变换是tensorflow中的重要模块之一,前面mnist实战模块我们使用了图片数据的压平操作,它就是维度变换的应用之一. 在详解维度变换的方法之前,这里先介绍一下View(视图)的概念.所谓Vi ...

  6. .net mvc 多文件上传

    1.input文件上传设置允许选择多个文件,设置属性 multiple即可 <input type="file" multiple="multiple" ...

  7. 基于STL的字典生成模块-模拟搜索引擎算法的尝试

    该课题来源于UVA中Searching the Web的题目:https://vjudge.net/problem/UVA-1597 按照题目的说法,我对按照特定格式输入的文章中的词语合成字典,以满足 ...

  8. docker 安装 jenkins touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

    拉取镜像 docker pull jenkins/jenkins:lts 官方下载 运行容器 docker run -it -v /home/jenkins:/var/jenkins_home -p ...

  9. Python之四:控制流

    1.If 逻辑判断: if a: b elif c: d else: e 先判断a语句块的值是否为真,如果为真,则执行b语句块,如果不为真则转到elif判断c语句块的值是否为真,如果为真执行d语句块, ...

  10. 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(5.21.1.10-加-6.4)Atomic_Operations

    5.21.1.10 Write Atomicity Normal 这个特性控制AWUN和NAWUN参数的操作.设置的属性值在set Feature命令的Dword 11中表明. 如果提交Get Fea ...