ehcache-demo

搭建本地缓存ehcache的demo

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.ehcache.demo</groupId>
  <artifactId>ehcache-demo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>ehcache-demo</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring Boot 缓存支持启动器 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <!-- 使用 ehcache -->
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
</project>

2.application.yml

spring:
  application:
    name: demo
  cache:
    type: ehcache
    ehcache:
      config: classpath:ehcache.xml

server:
  port: 8080

3.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"
         updateCheck="false">

    <!--这里给一篇博客:https://www.cnblogs.com/fashflying/p/6908028.html-->
    <diskStore path="java.io.tmpdir/Tmp_EhCache"/>
    <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="259200"
            memoryStoreEvictionPolicy="LRU"/>

    <cache
            name="local"
            eternal="false"
            maxElementsInMemory="5000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="1800"
            memoryStoreEvictionPolicy="LRU"/>

</ehcache>

4.目录结构,其它类
在这里插入图片描述

package com.ehcache.demo.impl;

import com.ehcache.demo.DemoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

/**
 * @program: ehcache-demo
 * @description: 测试实现类
 * @author: sw
 * @create: 2020-06-28 22:08
 **/

@Service
@EnableCaching
public class DemoServiceImpl implements DemoService {
    private final static String CACHE_KEY = "local";
    private int count = 0;

    //第一次查询操作后缓存!,count请求多次count都++了一次
    @Cacheable(value = CACHE_KEY,key = "#id")
    public Integer getId(int id) {
        System.out.println(id);
        return ++count;
    }

    //每次请求移,都清除旧值,也就是每请求一次返回++的值
    @CacheEvict(value = CACHE_KEY,key = "#id")
    public Integer removeId(int id) {
        return ++count;
    }

    //每次请求都更新缓存中都值,count也会每次++,和上面不同都是这个是更新缓存,上面是删除缓存
    @CachePut(value = CACHE_KEY,key = "#id")
    public Integer updateId(int id) {
        return ++count;
    }
}

package com.ehcache.demo.controller;

import com.ehcache.demo.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: ehcache-demo
 * @description: test
 * @author: zxb
 * @create: 2020-06-28 22:26
 **/

@RestController
public class EhcacheController {

    @Autowired
    private DemoService demoService;

    @GetMapping("/hello")
    public String hello(){
        return "ok";
    }

    @GetMapping("/getId")
    public Integer getId(int id) {
        System.out.println(id);
        return demoService.getId(id);
    }

    @GetMapping("/removeId")
    public Integer removeId(int id) {
        return demoService.removeId(id);
    }

    @GetMapping("/updateId")
    public Integer updateId(int id) {
        return demoService.updateId(id);
    }
}


package com.ehcache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;



/**
 * Hello world!
 *
 */

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class App {
    public static void main( String[] args ) {
        SpringApplication.run(App.class,args);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值