springboot ehcache 配置使用方法代码详解

网友投稿 787 2023-01-04

springboot ehcache 配置使用方法代码详解

springboot ehcache 配置使用方法代码详解

EhCache是一个比较成熟的java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统,它提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,快速简单。

Springboot对ehcache的使用非常支持,所以在SphnqdlRelDringboot中只需做些配置就可使用,且使用方式也简易。

下面通过本文给大家介绍springboot ehcache 配置使用方法,具体内容如下所示:

1. pom 引入依赖

net.sf.ehcache

ehcache

2.resources 目录下直接放个文件ehcache.xml

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

updateCheck="false">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

updateCheck="false">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU">

3.在Service层 方法上加上注解

@CacheEvict(value="menucache", allEntries=true) ,更新缓存

@Cacheable(key="'menu-'+#parentId",value="menucache") 读取缓存,"'menu-'+#parentId" 通配符,也可以直接写死字符串

menucache 对应 上面 xmlname="menucache"

/**删除菜单

* @param MENU_ID

* @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

* @fhadmin.org

*/

@Cacheable(key="'menu-'+#parentId",value="menucache")

public List

return menuMapper.listSubMenuByParentId(parentId);

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:hybrid(hybrid child)
下一篇:自媒体与小程序生态圈(小程序公众号文章朋友圈)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~