• springboot ehcache


    直接上代码

    一,POM

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
    <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>2.10.8</version>
            </dependency>

    二,配置xml

    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" />
        <!-- 默认缓存 -->
        <defaultCache
                maxElementsInMemory="10000"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                maxElementsOnDisk="10000000"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap" />
        </defaultCache>
     
        <!-- 测试 -->
        <cache name="testcache"
               timeToIdleSeconds="5"
               timeToLiveSeconds="5"
               maxEntriesLocalHeap="10000"
               maxEntriesLocalDisk="10000000"
               diskExpiryThreadIntervalSeconds="120"
               overflowToDisk="false"
               memoryStoreEvictionPolicy="LRU">
        </cache>
    </ehcache>

    application.yml:

    spring:
      cache:
        type: ehcache
        ehcache:
          config: classpath:config/ehcache.xml

    启动类:

    添加 @EnableCaching

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

    三,创建Service / Impl

    PS:必须有Service接口,不然缓存失效

    Service:

    public interface TestService {
     
        String testCache(String str);
    }

    Impl:

    @Service
    public class TestImpl implements TestService {
        @Cacheable(value = "testcache", key = "#str")
        @Override
        public String testCache(String str) {
            int a = new Random().nextInt();
            return a+"";
        }
    }

    四,调用

    @RestController
    @RequestMapping("api/test")
    public class TestPController {
     
        @Autowired
        TestService testService;
     
     
        @ApiOperation(value = "report test")
        @RequestMapping(value = "test", method = RequestMethod.POST)
        public ApiResponse<String> test(@RequestBody ApiRequest<String> req) throws Exception {
            String a=testService.testCache(req.getData());
            return new ApiResponse<>().Success(a);
        }
     
    }
  • 相关阅读:
    十分钟内学会:控制浏览器是否缓存网页状态
    编写 iPhone Friendly 的 Web 应用程序 (Part 7 多点触击)
    写个 JavaScript 异步调用框架 (Part 4 链式调用)
    拆分自然数:纯while实现 (Part 2 实现)
    诚聘 项目经理 & C++开发高手
    李天平:日常管理随笔一
    诚聘:.Net 软件工程师
    对 Enterprise Library 2.0 进一步封装: DbHelperSQL2
    李天平:无为而治&灯下黑
    累并快乐着!
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/15767006.html
Copyright © 2020-2023  润新知