zoukankan      html  css  js  c++  java
  • 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 对应 上面 xml name="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);
    	}
  • 相关阅读:
    【BZOJ2243】【SDOI2011】染色 (LCT)
    【BZOJ2631】tree (LCT)
    【BZOJ3626】【LNOI2014】LCA (树剖+离线)
    [BZOJ3244][NOI2013] 树的计数
    BZOJ2754 SCOI2012day1T2喵星球上的点名(后缀数组)
    BZOJ2753 SCOI2012day1T1滑雪与时间胶囊(bfs+kruskal)
    Swift
    Library not loaded: @rpath/libswiftCore.dylib
    PHP require include 区别
    Mac OS 下 eclipse中文乱码解决方法
  • 原文地址:https://www.cnblogs.com/teacher11/p/14929866.html
Copyright © 2011-2022 走看看