Ehcache可以对页面、对象、数据进行缓存,同时支持集群/分布式缓存。在应用中用于常常需要读取的数据交换,而不是通过DB DAO数据交换(cache不占用DB宝贵的NIO,直接交换堆内存)。

整合Spring、Hibernate也非常的简单,Spring对Ehcache的支持也非常好。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache。

从Spring3.1开始添加了对缓存的支持。

Maven的依赖:

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.0</version>
</dependency> <!-- Optional, to log stuff -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency> <!-- Spring caching framework inside this -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Support for Ehcache and others -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</project>

Gradle的依赖:

apply plugin: 'java'
apply plugin: 'eclipse-wtp' version = '1.0' // Uses JDK 7
sourceCompatibility = 1.7
targetCompatibility = 1.7 // Get dependencies from Maven central repository
repositories {
mavenCentral()
} //Project dependencies
dependencies {
compile 'org.springframework:spring-context:4.1.4.RELEASE'
compile 'org.springframework:spring-context-support:4.1.4.RELEASE'
compile 'net.sf.ehcache:ehcache:2.9.0'
compile 'ch.qos.logback:logback-classic:1.0.13'
}

spring-config-cache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean> <!--ehcache-->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache/ehcache.xml"/>
</bean> </beans>

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="es"> <diskStore path="java.io.tmpdir"/> <cache name="code-cache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache> </ehcache>

参数说明:

    name:
cache的名称,必填。 maxEntriesLocalHeap:
在内存中的最大保存对象数量,0则不限,如果填必须小于Integer.MAX_SIZE (2147483647)。 maxEntriesLocalDisk:
磁盘中保存的数量,0则不限,一般填0。 eternal:
true:对象永不过期。false:对象有过期时限。 以下为选填: maxEntriesInCache:
只在分布式环境使用,集群节点中的最大数量,0则不限 overflowToOffHeap:
企业版Ehcache才有的功能,用于java off-heap(off-heap允许Java直接操作内存空间),这样的目的是为了节省宝贵的jvm堆,又避免磁盘存储的低速。 maxBytesLocalHeap:
定义缓存可以从VM的堆中使用多少字节,如果定义这个则不能再定义maxEntriesLocalHeap。 maxBytesLocalOffHeap:
定义缓存可以从OffHeap中使用多少字节 maxBytesLocalDisk:
定义缓存可以从磁盘中使用多少OffHeap字节 timeToIdleSeconds:
对象空闲过期时间,以秒为单位,如果是0则不过期。只对eternal为false的有效。 timeToLiveSeconds:
对象生存时间,一般为0,为永久生存。只对eternal为false的有效。 上边的两个配置容易混淆,区别:
timeToLiveSeconds=x:缓存自创建日期起至失效时的间隔时间x;对象空闲时间,指对象在多长时间没有被访问就会失效。
timeToIdleSeconds=y:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔y; diskExpiryThreadIntervalSeconds:
对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。 diskSpoolBufferSizeMB:
DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。 memoryStoreEvictionPolicy:
如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。 clearOnFlush:
flush()的时候清除内存。 persistence sub-element. 持久化策略,这是子属性,一般不用设置。 * localRestartable - 可以重用的缓存,持久化在磁盘,只有企业版才有这个功能. * localTempSwap - 当(on-heap and/or off-heap)满的时候保存到磁盘,但并不重用持久化,即进程结束则缓存全部清除。 * none - 不持久化

使用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service; ...... private Cache cache; ...... public void addObjToCache(Object key, Object value) {
cache.put(key, value);
} public String getValueByCache(String key) {
return (String)cache.get(key).get();
} ......

Spring Caching集成Ehcache的更多相关文章

  1. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  2. spring boot集成ehcache 2.x 用于hibernate二级缓存

    https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...

  3. Spring Boot 集成 Ehcache 缓存,三步搞定!

    作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...

  4. Spring Boot集成EHCache实现缓存机制

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  5. Spring中集成Ehcache缓存

    1.导入依赖包 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...

  6. 详解Spring MVC 集成EHCache缓存_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 废话少说,直接上代码: ehcache.xml 文件 <?xml version="1.0" ...

  7. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  8. spring集成ehcache本地缓存

    1.maven依赖 <!-- ehcache 相关依赖 --> <dependency> <groupId>net.sf.ehcache</groupId&g ...

  9. SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

随机推荐

  1. Element-UI使用指南

    原网址:https://blog.csdn.net/u012285967/article/details/53023825 Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌 ...

  2. hbase--知识点总结2

    --用java操作hbase 1.配置jar包环境 创建hbase项目 --> 新建folder文件夹 --> 将hbase相关jar包全部导入到java项目之中 --> add b ...

  3. 原子性: Interlocked 类

    public class CounterNoLock:CountBase { private int _count; public int Count { get { return _count; } ...

  4. Mark,20180127,技术博客之路开启!

    不怎么会说话,自己本身少说话的撸码loner,少说多做.毕业从事手游一年多,之前主要从事Cocos2dx,刚转投Unity不到一个月,后面主要总结下自己在这两方面开发过程中的一些历程,希望自己能有所积 ...

  5. spring mvc重定向

    spring mvc重定向有三种方法. 1.return new ModelAndView("redirect:/toUrl"); 其中/toUrlt是你要重定向的url. 2.r ...

  6. Add custom field in Material Master

    1.Add fields in the Append Structure of table MARA. 2.Configure SPRO IMG -> Logistics General -&g ...

  7. sublime text3 使用问题积累

    1.安装完后,注册码:注意!要把下列内容完全拷贝过去,包含"-------BEGIN LICENSE------和------END LICENSE--------" ----- ...

  8. echo 转义字符的使用

    man echo 查看 echo 的使用文档 -n 不尾随换行符 -e 启用解释反斜杠的转义功能 -E 禁用解释反斜杠的转义功能(默认) --help 显示此帮助信息并退出 --version 显示版 ...

  9. 谷歌开源的一个BTREE实现 Go语言

    // Copyright 2014 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "Licens ...

  10. 第四次spring会议

    昨天:对TXT的字体颜色和背景进行了代码编写. 出现的问题:在网上找到如何编写代码后,自己打进去了,输出不出来.少打了一个空格在EventArgs e之间. 今天将做之事: 我设置上换肤和透明度等功能 ...