Ehcache缓存配置和基本使用

在java项目广泛的使用。它是一个开源的、设计于提高在数据从RDBMS中取出来的高花费、高延迟采取的一种缓存方案。正因为Ehcache具有健壮性(基于java开发)、被认证(具有apache 2.0 license)、充满特色(稍后会详细介绍),所以被用于大型复杂分布式web application的各个节点中。


够快

Ehcache的发行有一段时长了,经过几年的努力和不计其数的性能测试,Ehcache终被设计于large, high concurrency systems.

够简单

开发者提供的接口非常简单明了,从Ehcache的搭建到运用运行仅仅需要的是你宝贵的几分钟。其实很多开发者都不知道自己用在用Ehcache,Ehcache被广泛的运用于其他的开源项目

比如:hibernate

够袖珍

关于这点的特性,官方给了一个很可爱的名字small foot print ,一般Ehcache的发布版本不会到2M,V 2.2.3 才 668KB。

够轻量

核心程序仅仅依赖slf4j这一个包,没有之一!

好扩展

Ehcache提供了对大数据的内存和硬盘的存储,最近版本允许多实例、保存对象高灵活性、提供LRU、LFU、FIFO淘汰算法,基础属性支持热配置、支持的插件多

监听器

缓存管理器监听器 (CacheManagerListener)和 缓存监听器(CacheEvenListener),做一些统计或数据一致性广播挺好用的

如何使用?

POM文件

<!--加入缓存--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.6</version> </dependency>

配置文件

在resources资源目录下创建一个ehcache-config.xml文件,内容如下:

<?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"> <!-- EhCache在每次启动的时候都要连接到 ehcache 网站上去检查新版本 使用如上的 updateCheck="false" 来禁止这个检查新版本 --> <!-- name:cache唯一标识 eternal:缓存是否永久有效 maxElementsInMemory:内存中最大缓存对象数 overflowToDisk(true,false):缓存对象达到最大数后,将缓存写到硬盘中 diskPersistent:硬盘持久化 timeToIdleSeconds:缓存清除时间 timeToLiveSeconds:缓存存活时间 diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔 memoryStoreEvictionPolicy:缓存清空策略 1.FIFO:first in first out 先讲先出 2.LFU: Less Frequently Used 一直以来最少被使用的 3.LRU:Least Recently Used 最近最少使用的 --> <diskStore path="java.io.tmpdir/ehcache" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="FIFO" /> <!-- 缓存 --> <cache name="cache_mall_keys" maxElementsInMemory="200" eternal="false" timeToIdleSeconds="7200" timeToLiveSeconds="7200" overflowToDisk="true" maxElementsOnDisk="1000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="FIFO" /> <!-- 缓存 --> <cache name="cache_pos_codes" maxElementsInMemory="200" eternal="false" timeToIdleSeconds="43200" timeToLiveSeconds="43200" overflowToDisk="true" maxElementsOnDisk="1000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="FIFO" /> </ehcache>

Spirng整合配置

注意一下内容必须注册在Spring的主配置文件中

<!--缓存配置文件接口--> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache-config.xml"></property> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"></property> </bean>

使用方法

这里可以使用注解的方式 @Cacheable(value = “cache_pos_codes”) 其中value的是设置的配置文件ehcache-config.xml的配置名称。需要注意的是 import org.springframework.cache.annotation.Cacheable;

@RequestMapping(value = "/date",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE + CHARSET) @ResponseBody @Cacheable(value = "cache_pos_codes") public String getDate(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(new Date()); }

欢迎工作一到五年的Java工程师朋友们加入Java架构开发:855801563

本群提供免费的学习指导 架构资料 以及免费的解答

不懂得问题都可以在本群提出来 之后还会有职业生涯规划以及面试指导

同时大家可以多多关注一下小编 纯干货 大家一起学习进步

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容