美文网首页Java高级进阶
Ehcache缓存配置和基本使用

Ehcache缓存配置和基本使用

作者: 架构师springboot | 来源:发表于2018-09-13 16:21 被阅读5次

在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

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

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

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

相关文章

  • Hibernate Ehcache 配置

    hibernate 默认使用 ehcache 缓存策略ehcache 配置 hibernate 配置 Spring...

  • Ehcache缓存配置和基本使用

    在java项目广泛的使用。它是一个开源的、设计于提高在数据从RDBMS中取出来的高花费、高延迟采取的一种缓存方案。...

  • Ehcache缓存配置和基本使用

    Ehcache在java项目广泛的使用。它是一个开源的、设计于提高在数据从RDBMS中取出来的高花费、高延迟采取的...

  • Spring的缓存机制

    Spring的缓存机制启用Spring缓存Spring内置缓存实现的配置EhCache缓存实现的配置使用@Cach...

  • Mybatis缓存配置

    pom文件配置: spring加载ehcache配置文件 ehcache.xml: mybatis.xml开启缓存...

  • SpringBoot集成EhCache

    目标 网上的集成方案基本都是使用缓存注解来使用ehcache,但在实际应用中很不灵活。本文介绍了非注解方式无配置文...

  • SpringBoot2快速入门08--cache

    spring通过注解为我们提供了缓存功能,默认使用Ehcache,当然,也可以配置用其它的方式缓存,如redis,...

  • 集群间部署 Ehcache 实战

    本文参考网址: 《Spring+EhCache缓存实例》 《集群环境中使用 EhCache 缓存系统》 《Eh...

  • JAVA缓存-Redis入门级使用

    前言 Java缓存实现方案有很多,最基本的自己使用Map去构建缓存,再高级点的使用Ehcache或者Goolge的...

  • Ehcache结合Spring

    本章介绍Ehcache结合Spring实现服务层的缓存,其他还有数据层缓存和页面缓存。 添加依赖 spring配置...

网友评论

    本文标题:Ehcache缓存配置和基本使用

    本文链接:https://www.haomeiwen.com/subject/rbowgftx.html