Chinaunix首页 | 论坛 | 博客
  • 博客访问: 551482
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-06-25 10:41:28

1. pom 引入依赖


点击(此处)折叠或打开

  1. <!-- Ehcache -->
  2. <dependency>
  3.              <groupId>net.sf.ehcache</groupId>
  4.              <artifactId>ehcache</artifactId>
  5. </dependency>
2.resources 目录下直接放个文件 ehcache.xml

点击(此处)折叠或打开

  1. <ehcache xmlns:xsi=""
  2.          xsi:noNamespaceSchemaLocation=""
  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.     <!-- 菜单缓存策略 -->
  18.     <cache name="menucache"
  19.             maxElementsInMemory="10000"
  20.             eternal="false"
  21.             timeToIdleSeconds="120"
  22.             timeToLiveSeconds="120"
  23.             maxElementsOnDisk="10000000"
  24.             diskExpiryThreadIntervalSeconds="120"
  25.             memoryStoreEvictionPolicy="LRU">
  26.         <persistence strategy="localTempSwap"/>
  27.     </cache>
  28.     
  29. </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.      * @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.      * @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.     }


 /**删除菜单
	 * @param MENU_ID
	 * @
	 */
	@CacheEvict(value="menucache", allEntries=true)
	public void deleteMenuById(String MENU_ID) throws Exception{
		this.cleanRedis();
		menuMapper.deleteMenuById(MENU_ID);
	}

	/**
	 * 通过ID获取其子一级菜单
	 * @param parentId
	 * @return
	 * @
	 */
	@Cacheable(key="'menu-'+#parentId",value="menucache")
	public List listSubMenuByParentId(String parentId) throws Exception {
		return menuMapper.listSubMenuByParentId(parentId);
	}
阅读(8774) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~