Spring Caching集成Ehcache
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的更多相关文章
- (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...
- spring boot集成ehcache 2.x 用于hibernate二级缓存
https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...
- Spring Boot 集成 Ehcache 缓存,三步搞定!
作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...
- Spring Boot集成EHCache实现缓存机制
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- Spring中集成Ehcache缓存
1.导入依赖包 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...
- 详解Spring MVC 集成EHCache缓存_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 废话少说,直接上代码: ehcache.xml 文件 <?xml version="1.0" ...
- 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】
[原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...
- spring集成ehcache本地缓存
1.maven依赖 <!-- ehcache 相关依赖 --> <dependency> <groupId>net.sf.ehcache</groupId&g ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
随机推荐
- php 二维数组按照某个键排序
$date = array_column($arr, 'run_date'); //上面得到的结果:array(0=>'2017-11-21',1=>'2017-11-20',3=> ...
- 大数据入门到精通8-spark RDD 复合key 和复合value 的map reduce操作
一.做基础数据准备 这次使用fights得数据. scala> val flights= sc.textFile("/user/hdfs/data/Flights/flights.cs ...
- Linux 上pcntl安装步骤
一. 下载对应的PHP源码包 wget http://cn2.php.net/get/php-5.5.20.tar.gz/from/this/mirror 二. 解压下载的源码文件 tar -zxvf ...
- oracle 表空间创建和删除
oracle数据库:数据库对象以及表数据都存储在表空间中,创建用户时可以指定对应的表空间.这样用户可以在各自的表空间中操作数据,互不干扰. 1. 表空间创建 若不清楚表空间对应文件的路径,可以登录系统 ...
- 100道c++面试题(上)
1. new, delete, malloc, free关系 new/delete是c++的运算符,delete会调用对象的析构函数: malloc/free是c/c++的标准库函数,free只释放内 ...
- reentrantlocklock实现有界队列
今天找synchronize和reentrantlock区别的时候,发现有个使用reentrantlock中的condition实现有界队列,感觉挺有趣的,自己顺手敲了一遍 class Queue{ ...
- Vim中YouCompleteMe插件安装
背景 YouCompleteMe需要使用GCC进行编译,然而Centos 6.7默认的GCC版本太低,所以需要使用devtools-2,用来安装多个版本GCC手动编译安装GCC的坑简直不要太多(类似于 ...
- FastFDS安装及简单使用
1. FastDFS安装(ubuntu) 需要准备: nginx.fastdfs.libfastcommon.gcc.git apt-get update apt-get -y install mak ...
- str 转 md5
@interface NSString (MD5) + (NSString *)md5To32bit:(NSString *)str; @end @implementation NSString (M ...
- 《笨方法学Python》加分题15
本题本题开始涉及文件的操作,文件操作是一件危险的事情,需要仔细细心否则可能导致重要的文件损坏. 本题除了 ex15.py 这个脚本以外,还需要一个用来读取的文件 ex15_sample.txt 其内容 ...