springboot ehcache 配置使用方法

深碍√TFBOYSˉ_ 2022-10-09 15:26 213阅读 0赞
  1. pom 引入依赖

    1. <dependency>
    2. <groupId>net.sf.ehcache</groupId>
    3. <artifactId>ehcache</artifactId>
    4. </dependency>

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

  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  3. updateCheck="false">
  4. <diskStore path="java.io.tmpdir"/>
  5. <!--defaultCache:echcache的默认缓存策略 -->
  6. <defaultCache
  7. maxElementsInMemory="10000"
  8. eternal="false"
  9. timeToIdleSeconds="120"
  10. timeToLiveSeconds="120"
  11. maxElementsOnDisk="10000000"
  12. diskExpiryThreadIntervalSeconds="120"
  13. memoryStoreEvictionPolicy="LRU">
  14. <persistence strategy="localTempSwap"/>
  15. </defaultCache>
  16. <!-- 菜单缓存策略 -->
  17. <cache name="menucache"
  18. maxElementsInMemory="10000"
  19. eternal="false"
  20. timeToIdleSeconds="120"
  21. timeToLiveSeconds="120"
  22. maxElementsOnDisk="10000000"
  23. diskExpiryThreadIntervalSeconds="120"
  24. memoryStoreEvictionPolicy="LRU">
  25. <persistence strategy="localTempSwap"/>
  26. </cache>
  27. </ehcache>

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

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

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

menucache 对应 上面 xml name=”menucache”

  1. /**删除菜单
  2. * @param MENU_ID
  3. * @www.fhadmin.org
  4. */
  5. @CacheEvict(value="menucache", allEntries=true)
  6. public void deleteMenuById(String MENU_ID) throws Exception{
  7. this.cleanRedis();
  8. menuMapper.deleteMenuById(MENU_ID);
  9. }
  10. /**
  11. * 通过ID获取其子一级菜单
  12. * @param parentId
  13. * @return
  14. * @www.fhadmin.org
  15. */
  16. @Cacheable(key="'menu-'+#parentId",value="menucache")
  17. public List<Menu> listSubMenuByParentId(String parentId) throws Exception {
  18. return menuMapper.listSubMenuByParentId(parentId);
  19. }

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springboot 配置Ehcache

    Ehcache的基本配置说明我就不说了.小编记录一下在springboot中使用Ehcache的使用方法. 第一步:在classpath下引入配置文件ehcache.xml