首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring整合Ehcache管理缓存

Spring整合Ehcache管理缓存

作者头像
静默虚空
发布于 2018-01-05 07:45:35
发布于 2018-01-05 07:45:35
1.8K00
代码可运行
举报
运行总次数:0
代码可运行

前言

Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存。 Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。 本文先通过Ehcache独立应用的范例来介绍它的基本使用方法,然后再介绍与Spring整合的方法。

概述

Ehcache是什么? EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点。它是Hibernate中的默认缓存框架。 Ehcache已经发布了3.1版本。但是本文的讲解基于2.10.2版本。 为什么不使用最新版呢?因为Spring4还不能直接整合Ehcache 3.x。虽然可以通过JCache间接整合,Ehcache也支持JCache,但是个人觉得不是很方便。

安装

Ehcache 如果你的项目使用maven管理,添加以下依赖到你的pom.xml中。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
  <version>2.10.2</version>
  <type>pom</type>
</dependency>

如果你的项目不使用maven管理,请在 Ehcache官网下载地址 下载jar包。

Spring 如果你的项目使用maven管理,添加以下依赖到你的pom.xml中。 spring-context-support这个jar包中含有Spring对于缓存功能的抽象封装接口。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.1.4.RELEASE</version>
</dependency>

Ehcache的使用

HelloWorld范例

接触一种技术最快最直接的途径总是一个Hello World例子,毕竟动手实践印象更深刻,不是吗?

(1) 在classpath下添加ehcache.xml 添加一个名为helloworld的缓存。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

  <!-- 磁盘缓存位置 -->
  <diskStore path="java.io.tmpdir/ehcache"/>

  <!-- 默认缓存 -->
  <defaultCache
          maxEntriesLocalHeap="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          maxEntriesLocalDisk="10000000"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU"/>

  <!-- helloworld缓存 -->
  <cache name="helloworld"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="5"
         timeToLiveSeconds="5"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>
</ehcache>

(2) EhcacheDemo.java Ehcache会自动加载classpath根目录下名为ehcache.xml文件。 EhcacheDemo的工作步骤如下: 在EhcacheDemo中,我们引用ehcache.xml声明的名为helloworld的缓存来创建Cache对象; 然后我们用一个键值对来实例化Element对象; 将Element对象添加到Cache; 然后用Cache的get方法获取Element对象。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class EhcacheDemo {
    public static void main(String[] args) throws Exception {
        // Create a cache manager
        final CacheManager cacheManager = new CacheManager();

        // create the cache called "helloworld"
        final Cache cache = cacheManager.getCache("helloworld");

        // create a key to map the data to
        final String key = "greeting";

        // Create a data element
        final Element putGreeting = new Element(key, "Hello, World!");

        // Put the element into the data store
        cache.put(putGreeting);

        // Retrieve the data element
        final Element getGreeting = cache.get(key);

        // Print the value
        System.out.println(getGreeting.getObjectValue());
    }
}

输出

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Hello, World!

Ehcache基本操作

ElementCacheCacheManager是Ehcache最重要的API

  • Element:缓存的元素,它维护着一个键值对。
  • Cache:它是Ehcache的核心类,它有多个Element,并被CacheManager管理。它实现了对缓存的逻辑行为。
  • CacheManager:Cache容器对象,并管理着Cache的生命周期。 创建CacheManager 下面的代码列举了创建CacheManager的五种方式。 使用静态方法create()会以默认配置来创建单例的CacheManager实例。 newInstance()方法是一个工厂方法,以默认配置创建一个新的CacheManager实例。 此外,newInstance()还有几个重载函数,分别可以通过传入StringURLInputStream参数来加载配置文件,然后创建CacheManager实例。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 使用Ehcache默认配置获取单例的CacheManager实例
CacheManager.create();
String[] cacheNames = CacheManager.getInstance().getCacheNames();

// 使用Ehcache默认配置新建一个CacheManager实例
CacheManager.newInstance();
String[] cacheNames = manager.getCacheNames();

// 使用不同的配置文件分别创建一个CacheManager实例
CacheManager manager1 = CacheManager.newInstance("src/config/ehcache1.xml");
CacheManager manager2 = CacheManager.newInstance("src/config/ehcache2.xml");
String[] cacheNamesForManager1 = manager1.getCacheNames();
String[] cacheNamesForManager2 = manager2.getCacheNames();

// 基于classpath下的配置文件创建CacheManager实例
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = CacheManager.newInstance(url);

// 基于文件流得到配置文件,并创建CacheManager实例
InputStream fis = new FileInputStream(new File
("src/config/ehcache.xml").getAbsolutePath());
try {
 CacheManager manager = CacheManager.newInstance(fis);
} finally {
 fis.close();
}
添加缓存

需要强调一点,Cache对象在用addCache方法添加到CacheManager之前,是无效的。 使用CacheManager的addCache方法可以根据缓存名将ehcache.xml中声明的cache添加到容器中;它也可以直接将Cache对象添加到缓存容器中。 Cache有多个构造函数,提供了不同方式去加载缓存的配置参数。 有时候,你可能需要使用API来动态的添加缓存,下面的例子就提供了这样的范例。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 除了可以使用xml文件中配置的缓存,你也可以使用API动态增删缓存
// 添加缓存
manager.addCache(cacheName);

// 使用默认配置添加缓存
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");

// 使用自定义配置添加缓存,注意缓存未添加进CacheManager之前并不可用
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
singletonManager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");

// 使用特定的配置添加缓存
CacheManager manager = CacheManager.create();
Cache testCache = new Cache(
 new CacheConfiguration("testCache", maxEntriesLocalHeap)
 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
 .eternal(false)
 .timeToLiveSeconds(60)
 .timeToIdleSeconds(30)
 .diskExpiryThreadIntervalSeconds(0)
 .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP)));
 manager.addCache(testCache);

删除缓存

删除缓存比较简单,你只需要将指定的缓存名传入removeCache方法即可。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("sampleCache1");

实现基本缓存操作

Cache最重要的两个方法就是put和get,分别用来添加Element和获取Element。 Cache还提供了一系列的get、set方法来设置或获取缓存参数,这里不一一列举,更多API操作可参考官方API开发手册

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * 测试:使用默认配置或使用指定配置来创建CacheManager
 *
 * @author Zhang Peng
 */
public class CacheOperationTest {
    private final Logger log = LoggerFactory.getLogger(CacheOperationTest.class);

    /**
     * 使用Ehcache默认配置(classpath下的ehcache.xml)获取单例的CacheManager实例
     */
    @Test
    public void operation() {
        CacheManager manager = CacheManager.newInstance("src/test/resources/ehcache/ehcache.xml");

        // 获得Cache的引用
        Cache cache = manager.getCache("userCache");

        // 将一个Element添加到Cache
        cache.put(new Element("key1", "value1"));

        // 获取Element,Element类支持序列化,所以下面两种方法都可以用
        Element element1 = cache.get("key1");
        // 获取非序列化的值
        log.debug("key:{}, value:{}", element1.getObjectKey(), element1.getObjectValue());
        // 获取序列化的值
        log.debug("key:{}, value:{}", element1.getKey(), element1.getValue());

        // 更新Cache中的Element
        cache.put(new Element("key1", "value2"));
        Element element2 = cache.get("key1");
        log.debug("key:{}, value:{}", element2.getObjectKey(), element2.getObjectValue());

        // 获取Cache的元素数
        log.debug("cache size:{}", cache.getSize());

        // 获取MemoryStore的元素数
        log.debug("MemoryStoreSize:{}", cache.getMemoryStoreSize());

        // 获取DiskStore的元素数
        log.debug("DiskStoreSize:{}", cache.getDiskStoreSize());

        // 移除Element
        cache.remove("key1");
        log.debug("cache size:{}", cache.getSize());

        // 关闭当前CacheManager对象
        manager.shutdown();

        // 关闭CacheManager单例实例
        CacheManager.getInstance().shutdown();
    }
}

缓存配置

Ehcache支持通过xml文件和API两种方式进行配置。

xml方式

Ehcache的CacheManager构造函数或工厂方法被调用时,会默认加载classpath下名为ehcache.xml的配置文件。如果加载失败,会加载Ehcache jar包中的ehcache-failsafe.xml文件,这个文件中含有简单的默认配置。 ehcache.xml配置参数说明:

  • name:缓存名称。
  • maxElementsInMemory:缓存最大个数。
  • eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
  • timeToIdleSeconds:置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  • timeToLiveSeconds:缓存数据的生存时间(TTL),也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
  • maxEntriesLocalDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
  • overflowToDisk:内存不足时,是否启用磁盘缓存。
  • diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
  • maxElementsOnDisk:硬盘最大缓存个数。
  • diskPersistent:是否在VM重启时存储硬盘的缓存数据。默认值是false。
  • diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
  • memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
  • clearOnFlush:内存数量最大时是否清除。

ehcache.xml的一个范例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

  <!-- 磁盘缓存位置 -->
  <diskStore path="java.io.tmpdir/ehcache"/>

  <!-- 默认缓存 -->
  <defaultCache
          maxEntriesLocalHeap="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          maxEntriesLocalDisk="10000000"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
  </defaultCache>

  <cache name="userCache"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="3"
         timeToLiveSeconds="3"
         maxEntriesLocalDisk="10000000"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>
</ehcache>
API方式

xml配置的参数也可以直接通过编程方式来动态的进行配置(dynamicConfig没有设为false)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Cache cache = manager.getCache("sampleCache"); 
CacheConfiguration config = cache.getCacheConfiguration(); 
config.setTimeToIdleSeconds(60); 
config.setTimeToLiveSeconds(120); 
config.setmaxEntriesLocalHeap(10000); 
config.setmaxEntriesLocalDisk(1000000);

也可以通过disableDynamicFeatures()方式关闭动态配置开关。配置以后你将无法再以编程方式配置参数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Cache cache = manager.getCache("sampleCache"); 
cache.disableDynamicFeatures();

Spring整合Ehcache

Spring3.1开始添加了对缓存的支持。和事务功能的支持方式类似,缓存抽象允许底层使用不同的缓存解决方案来进行整合。 Spring4.1开始支持JSR-107注解。 注:我本人使用的Spring版本为4.1.4.RELEASE,目前Spring版本仅支持Ehcache2.5以上版本,但不支持Ehcache3。

绑定Ehcache

org.springframework.cache.ehcache.EhCacheManagerFactoryBean这个类的作用是加载Ehcache配置文件。 org.springframework.cache.ehcache.EhCacheCacheManager这个类的作用是支持net.sf.ehcache.CacheManager。

spring-ehcache.xml的配置

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

  <description>ehcache缓存配置管理文件</description>

  <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache/ehcache.xml"/>
  </bean>

  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
  </bean>

  <!-- 启用缓存注解开关 -->
  <cache:annotation-driven cache-manager="cacheManager"/>
</beans>

使用Spring的缓存注解

开启注解

Spring为缓存功能提供了注解功能,但是你必须启动注解。 你有两个选择:

(1) 在xml中声明 像上一节spring-ehcache.xml中的做法一样,使用<cache:annotation-driven/>

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<cache:annotation-driven cache-manager="cacheManager"/>

(2) 使用标记注解 你也可以通过对一个类进行注解修饰的方式在这个类中使用缓存注解。 范例如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Configuration
@EnableCaching
public class AppConfig {
}

注解基本使用方法

Spring对缓存的支持类似于对事务的支持。 首先使用注解标记方法,相当于定义了切点,然后使用Aop技术在这个方法的调用前、调用后获取方法的入参和返回值,进而实现了缓存的逻辑。 下面三个注解都是方法级别:

@Cacheable

表明所修饰的方法是可以缓存的:当第一次调用这个方法时,它的结果会被缓存下来,在缓存的有效时间内,以后访问这个方法都直接返回缓存结果,不再执行方法中的代码段。 这个注解可以用condition属性来设置条件,如果不满足条件,就不使用缓存能力,直接执行方法。 可以使用key属性来指定key的生成规则。

@CachePut

@Cacheable不同,@CachePut不仅会缓存方法的结果,还会执行方法的代码段。 它支持的属性和用法都与@Cacheable一致。

@CacheEvict

@Cacheable功能相反,@CacheEvict表明所修饰的方法是用来删除失效或无用的缓存数据。 下面是@Cacheable@CacheEvict@CachePut基本使用方法的一个集中展示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Service
public class UserService {
    // @Cacheable可以设置多个缓存,形式如:@Cacheable({"books", "isbns"})
    @Cacheable({"users"})
    public User findUser(User user) {
        return findUserInDB(user.getId());
    }

    @Cacheable(value = "users", condition = "#user.getId() <= 2")
    public User findUserInLimit(User user) {
        return findUserInDB(user.getId());
    }

    @CachePut(value = "users", key = "#user.getId()")
    public void updateUser(User user) {
        updateUserInDB(user);
    }

    @CacheEvict(value = "users")
    public void removeUser(User user) {
        removeUserInDB(user.getId());
    }

    @CacheEvict(value = "users", allEntries = true)
    public void clear() {
        removeAllInDB();
    }
}
@Caching

如果需要使用同一个缓存注解(@Cacheable@CacheEvict@CachePut)多次修饰一个方法,就需要用到@Caching

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(cacheNames="secondary", key="#p0") })
public Book importBooks(String deposit, Date date)
@CacheConfig

与前面的缓存注解不同,这是一个类级别的注解。 如果类的所有操作都是缓存操作,你可以使用@CacheConfig来指定类,省去一些配置。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@CacheConfig("books")
public class BookRepositoryImpl implements BookRepository {
    @Cacheable
    public Book findBook(ISBN isbn) {...}
}

参考

如果想参考我的完整代码示例,请点击这里访问我的github。

下面是我在写作时参考的资料或文章。 Ehcache github Ehcache官方文档 Ehcache详细解读 注释驱动的 Spring cache 缓存介绍 Spring官方文档4.3.3.RELEASE 第36章缓存抽象

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-10-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
缓存之EHCache
    ehcache 是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,而且是hibernate 默认的缓存provider。ehcache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
allsmallpig
2021/02/12
6000
Ehcache介绍及整合Spring实现高速缓存
Ehcache介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。它使用的是JVM的堆内存,超过内存可以设置缓存到磁盘,企业版的可以使用JVM堆外的物理内存。 Spring整合Ehcache 首先加入最新的ehcache的maven依赖 <!-- ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>
Java技术栈
2018/03/30
1.6K0
Ehcache介绍及整合Spring实现高速缓存
另一种缓存,Spring Boot 整合 Ehcache
用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场景下,我们还是会用到 Ehcache。
江南一点雨
2019/06/18
5640
spring ehcache配置以及使用(afterPropertiesSet)
大家好,又见面了,我是你们的朋友全栈君。spring 配置ehcache例子:[url]http://blog.csdn.net/linfanhehe/article/details/7693091[/url] [color=red][b]主要特性[/b][/color] 1. 快速. 2. 简单. [b]3. 多种缓存策略[/b] 4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 5. 缓存数据会在虚拟机重启的过程中写入磁盘 [b]6. 可以通过RMI、可插入API等方式进行分布式缓存[/b] 7. 具有缓存和缓存管理器的侦听接口 8. 支持多缓存管理器实例,以及一个实例的多个缓存区域 [b]9. 提供Hibernate的缓存实现[/b]
全栈程序员站长
2022/07/23
5050
SpringBoot2 整合Ehcache组件,轻量级缓存管理
EhCache是一个纯Java的进程内缓存框架,具有快速、上手简单等特点,是Hibernate中默认的缓存提供方。
知了一笑
2020/08/13
6550
Redis应用—8.相关的缓存框架
整体上看,Ehcache的使用是相对简单便捷的,提供了完整的各类API接口。需要注意,虽然Ehcache支持磁盘的持久化,但是由于存在两级缓存介质,在一级内存中的缓存如果没有主动刷入磁盘持久化,则在应用异常宕机时,依然会出现缓存数据丢失,为此可以根据需要将缓存刷到磁盘。将缓存条目刷到磁盘的操作可以通过cache.flush()方法来执行。需要注意:将对象写入磁盘前,要先将对象进行序列化。
东阳马生架构
2025/03/07
2140
SpringBoot中Shiro缓存使用Redis、Ehcache
SpringBoot 中配置redis作为session 缓存器。 让shiro引用
云扬四海
2019/08/19
2.8K0
SpringBoot 2.0.4 使用Ehcache作为Hibernate的二级缓存和系统缓存
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
yingzi_code
2019/08/31
1.8K0
Spring接口缓存实现方案Caffeine和EhCache
1.引入jar包 compile("com.github.ben-manes.caffeine:caffeine:2.8.6") compile("org.springframework.boot:spring-boot-starter-cache") 2.application.properties ##配置ehcache spring.cache.ehcache.config = classpath:/ehcache.xml spring.cache.type = ehcache
oktokeep
2024/11/21
1770
SpringMVC+mybatis+maven+Ehcache缓存实现
所谓缓存,就是将程序或系统经常要调用的对象存在内存中,以便其使用时可以快速调用,不必再去创建新的重复的实例。这样做可以减少系统开销,提高系统效率。 缓存主要可分为二大类:  一、通过文件缓存,顾名思义文件缓存是指把数据存储在磁盘上,不管你是以XML格式,序列化文件DAT格式还是其它文件格式;   二、内存缓存,也就是实现一个类中静态Map,对这个Map进行常规的增删查. 一、EhCache缓存系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认
架构师专栏
2018/06/29
6580
EhCache缓存工具类 原
参考:https://blog.csdn.net/qq_34531925/article/details/79134903
wuweixiang
2018/08/14
1.1K0
Springboot整合ehcache缓存「建议收藏」
大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说Springboot整合ehcache缓存「建议收藏」,希望能够帮助大家进步!!!
Java架构师必看
2022/02/15
1.2K0
Springboot整合ehcache缓存「建议收藏」
(11)SpringBoot整合EhCache做缓存
EhCache 简介:EhCache 是一个纯Java的进程内缓存框架,是Hibernate中默认的CacheProvider;其缓存的数据可以存放在内存里面,也可以存放在硬盘上;其核心是CacheManager,一切Ehcache的应用都是从CacheManager开始的;它是用来管理Cache(缓存)的,一个应用可以有多个CacheManager,而一个CacheManager下又可以有多个Cache;Cache内部保存的是一个个的Element,而一个Element中保存的是一个key和value的配对,相当于Map里面的一个Entry。
IT云清
2022/05/07
1.4K0
Spring Boot---(14)Spring Boot 整合EhCache做缓存
EHCache是来自sourceforge(http://ehcache.sourceforge.net/) 的开源项目,也是纯Java实现的简单、快速的Cache组件。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用 Gzip压缩提高响应速度。
IT云清
2019/01/22
1.1K0
ehcache2.8.3入门示例:hello world
一、pom.xml 依赖项 1 <dependency> 2 <groupId>net.sf.ehcache</groupId> 3 <artifactId>ehcache</artifactId> 4 <version>2.8.3</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.slf4j<
菩提树下的杨过
2018/01/24
7890
ehcache2.8.3入门示例:hello world
springboot ehcache 配置使用方法
@CacheEvict(value="menucache", allEntries=true) ,更新缓存
FHAdmin
2021/06/25
1.1K0
Spring+EhCache缓存实例(详细讲解+源码下载)
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
执笔记忆的空白
2020/12/25
8050
springboot+mybatis集成自定义缓存ehcache用法笔记
今天小编给大家整理了springboot+mybatis集成自定义缓存ehcache用法笔记,希望对大家能有所办帮助!
小明互联网技术分享社区
2021/09/30
5530
springboot+mybatis集成自定义缓存ehcache用法笔记
SpringBoot配置EhCache缓存
在 src/main/resources 目录下创建 ehcache.xml 文件,内容如下:
崔笑颜
2020/06/08
1.5K0
Ehcache配置详解及CacheManager使用
timeToIdleSeconds 当缓存闲置n秒后销毁 timeToLiveSeconds 当缓存存活n秒后销毁 缓存配置 name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToL
小柒2012
2018/04/13
1.9K0
相关推荐
缓存之EHCache
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档