EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统,它提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,快速简单。
Springboot对ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也简易。
下面通过本文给大家介绍springboot ehcache 配置使用方法,具体内容如下所示:
1. pom 引入依赖
|
1
2
3
4
5
|
<!-- Ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
|
2.resources 目录下直接放个文件ehcache.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<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"
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/**删除菜单
* @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 配置使用内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.cnblogs.com/teacher11/archive/2021/06/25/14929866.html
相关文章
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
iOS 泛型中nullable、null resettable、null kindof 用法详解
2025-05-29 100 -
2025-06-05 41
-
2025-06-04 84
-
2025-05-29 62
-
2025-05-25 49

