zoukankan      html  css  js  c++  java
  • spring boot 使用Ehcache

    1-引入maven依赖;

    2-增加ehcache.xml

    3-bootstrap.yml配置ehcache.xml的路径

    4-启动类加注解@EnableCaching

    5-使用处加注解@Cacheable(value = "testCache", key = "#user.name")

      Cacheable的value选择配置,默认是默认配置;key是缓存的key值,默认是入参,也可自定义,取其中的一个属性值;

      @CacheEvict(value = "testCache", allEntries = true)

       清空缓存

    1-引入maven依赖

            <!--spring boot 缓存支持启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
            <!--Ehcahe-->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>2.10.1</version>
            </dependency>

    2-增加ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    
        <!-- 磁盘缓存位置 -->
        <diskStore path="java.io.tmpdir/ehcache"/>
    
        <!-- 默认缓存 -->
        <defaultCache
                maxEntriesLocalHeap="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                maxEntriesLocalDisk="10000000"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap"/>
        </defaultCache>
    
        <cache name="testCache"
               maxElementsInMemory="1000"
               eternal="false"
               timeToIdleSeconds="3"
               timeToLiveSeconds="3"
               maxEntriesLocalDisk="10000000"
               overflowToDisk="false"
               memoryStoreEvictionPolicy="LRU"/>
    </ehcache>

    3-bootstrap.yml配置ehcache.xml的路径

    #ehcache
    spring.cache.ehcache.config: ehcache.xml

    4-启动类加注解@EnableCaching

    @SpringBootApplication
    @EnableCaching
    public class UserServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication.class, args);
        }
    }

    5-使用处加注解@Cacheable("testCache")

        @GetMapping(value = "/testEhcache")
        @ResponseBody
        @Cacheable("testCache")
        public int testEhcache(int num) {
            logger.info("入参:" + num);
            logger.info("执行:" + num);
            for (int i = 0, n = num; i < n; i++) {
                num += i;
            }
            logger.info("结果:" + num);
            return num;
        }
    
        @GetMapping(value = "/testEhcache2")
        @ResponseBody
        @Cacheable("testCache")
        public int testEhcache2(int num) {
            logger.info("入参:" + num);
            logger.info("执行:" + num);
            for (int i = 0, n = num; i < n; i++) {
                num += Math.random() * 10;
            }
            logger.info("结果:" + num);
            return num;
        }
  • 相关阅读:
    VS2015调试ArcMap Add-in插件提示尝试运行项目时出错,无法启动程序“路径arcmap.exe”
    c#重命名文件,报错“System.NotSupportedException”类型的未经处理的异常在 mscorlib.dll 中发生”
    C# string contains 不区分大小写
    CSS div 高度满屏
    ArcGIS Server SOE开发之奇怪异常:
    C# 读取XML注释
    .Net程序员之不学Java做安卓开发:奇怪的Java语法
    .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口
    JS去遍历Table的所有单元格中的内容
    判断 checkbox 是否选中以及 设置checkbox选中
  • 原文地址:https://www.cnblogs.com/chenglc/p/10715417.html
Copyright © 2011-2022 走看看