springboot ehcache 配置使用方法代码详解
EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统,它提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,快速简单。
Springboot对ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也简易。
下面通过本文给大家介绍springboot ehcache 配置使用方法,具体内容如下所示:
1. pom 引入依赖
<!-- Ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2.resources 目录下直接放个文件ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache的默认缓存策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!-- 菜单缓存策略 --> <cache name="menucache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
3.在Service层 方法上加上注解
@CacheEvict(value="menucache", allEntries=true) ,更新缓存
@Cacheable(key="'menu-'+#parentId",value="menucache") 读取缓存,"'menu-'+#parentId" 通配符,也可以直接写死字符串
menucache 对应 上面 xmlname="menucache"
/**删除菜单 * @param MENU_ID * @www.fhadmin.org */ @CacheEvict(value="menucache", allEntries=true) public void deleteMenuById(String MENU_ID) throws Exception{ this.cleanRedis(); menuMapper.deleteMenuById(MENU_ID); } /** * 通过ID获取其子一级菜单 * @param parentId * @return * @www.fhadmin.org */ @Cacheable(key="'menu-'+#parentId",value="menucache") public List<Menu> listSubMenuByParentId(String parentId) throws Exception { return menuMapper.listSubMenuByParentId(parentId); }
到此这篇关于springboot ehcache 配置使用方法代码详解的文章就介绍到这了,更多相关springboot ehcache 配置使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Boot 整合持久层之Spring Data JPA
在介绍Spring Data JPA的时候,我们首先认识下Hibernate。Hibernate是数据访问解决技术的绝对霸主,使用O/R映射技术实现数据访问,O/R映射即将领域模型类和数据库的表进行映射,通过程序操作对象而实现表数据操作的能力,让数据访问操作无须关注数据库相关的技术2022-08-08java提取字符串中数字string以及获取字符串中的整数或小数
这篇文章主要给大家介绍了关于java提取字符串中数字string以及获取字符串中的整数或小数的相关资料,需要的朋友可以参考下2023-08-08关于shiro中部分SpringCache失效问题的解决方法
这篇文章主要给大家介绍了关于shiro中部分SpringCache失效问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2018-07-07Java请求调用参数格式为form-data类型的接口代码示例
这篇文章主要给大家介绍了关于Java请求调用参数格式为form-data类型的接口的相关资料,文中给出了详细的代码示例,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-08-08
最新评论