springboot框架中增加使用ehcache

1、
Application.java中增加注解
//增加ehcache缓存
@EnableCaching
public class Application {

2、配置文件ehcache.xml 存放到

springboot框架中增加使用ehcache

<?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/Tmp_EhCache"/>
    <!-- 默认缓存 -->
     <defaultCache
            eternal="false"
            maxElementsInMemory="10000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="1800"
            timeToLiveSeconds="259200"
            memoryStoreEvictionPolicy="LRU"/>
    <!-- helloworld缓存 -->
       <cache name="cacheTestBean"
           maxElementsInMemory="1000"
           eternal="true"
           timeToIdleSeconds="50000"
           timeToLiveSeconds="50000"
           overflowToDisk="true"
           memoryStoreEvictionPolicy="LRU"/>

    <!--ehcache.xml配置参数说明:
    name:缓存名称。
    maxElementsInMemory:缓存最大个数。
    eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
    timeToIdleSeconds:置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
    timeToLiveSeconds:缓存数据的生存时间(TTL),也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
    maxEntriesLocalDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
    overflowToDisk:内存不足时,是否启用磁盘缓存。
    diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
    maxElementsOnDisk:硬盘最大缓存个数。
    diskPersistent:是否在VM重启时存储硬盘的缓存数据。默认值是false。
    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
    memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
    clearOnFlush:内存数量最大时是否清除。-->
</ehcache>

3、service调用

package com.unidt.norter.serviceImpl.normal;

import com.qcloud.cos.utils.StringUtils;
import com.unidt.norter.bean.UserInfo;
import com.unidt.norter.bean.testcache.CacheTestBean;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * @Description: 缓存测试服务
 * @author songchun.yang
 * @Description:
 * @Date Created in 2020/4/1 16:36
 */
@Service
@CacheConfig(cacheNames="cacheTestBean")
public class CacheTestService {

    /**模拟数据库进行存储**/
    public  Map<String, CacheTestBean> list = new HashMap<String,CacheTestBean>();

    @Cacheable(key="#bean.getKey()")
    public CacheTestBean save(CacheTestBean bean) {
        System.out.println("save()被执行了....");
        return bean;
    }
    /**
     *@Description: 添加到map
     *@Author: ysc 2020/4/1 16:36
     */
    @CachePut(value = "cacheTestBean", key="#cacheTestBean.key",condition = "#cacheTestBean.name != null ")
    public CacheTestBean add(CacheTestBean cacheTestBean){
        if(null!=cacheTestBean
                &&!StringUtils.isNullOrEmpty(cacheTestBean.getKey())
                &&!list.containsKey(cacheTestBean.getKey())){
            /**设置创建时间**/
            cacheTestBean.setCreatTime(new Date());
            System.out.println("添加到内存:"+cacheTestBean);
            return list.put(cacheTestBean.getKey(),cacheTestBean);
        }
        return new CacheTestBean();
    }

    /**
     *@Description: 获取map
     *@Author: ysc 2020/4/1 16:36
     */
    @Cacheable(value = "cacheTestBean",key="#key", condition = "#key != null ")
    public CacheTestBean get(String key){
        String a = "wqw";
        CacheTestBean current=list.get(key);
        /**设置更新时间**/
        if(current!=null){
            current.setUpdateTime(new Date());
            list.put(key,current);
            System.out.println("读取内存:"+current);
        }else{
            System.out.println("内存中不存在:"+current);
        }
        return current;
       /* if(!StringUtils.isNullOrEmpty(key)&&list.containsKey(key)){
            CacheTestBean current=list.get(key);
            *//**设置更新时间**//*
            current.setUpdateTime(new Date());
            list.put(key,current);
            System.out.println("读取内存:"+current);
            return current;
        }*/
        // return new CacheTestBean();
    }

    /**
     *@Description: 删除map
     *@Author: ysc 2020/4/1 16:36
     */
    @CacheEvict(value = "cacheTestBean",key="#key",condition = "#key != null ")
    public CacheTestBean del(String key){
        if(!StringUtils.isNullOrEmpty(key)&&list.containsKey(key)){
            System.out.println("删除内存:"+key);
            return list.remove(key);
        }
        return new CacheTestBean();
    }
}

4、调用测试类

package com.unidt.norter.bean.testcache;
import java.io.Serializable;
import java.util.Date;
/**
 *
 * @Description: 缓存测试实体
 * @author songchun.yang
 * @Description:
 * @Date Created in 2020/4/1 16:37
 */
public class CacheTestBean implements Serializable{

    /**键**/
    String key;
    /**值**/
    String name;
    /**创建时间**/
    Date creatTime;
    /**更新时间**/
    Date updateTime;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreatTime() {
        return creatTime;
    }

    public void setCreatTime(Date creatTime) {
        this.creatTime = creatTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }


    @Override
    public String toString() {
        return "CacheTestBean{" +
                "key='" + key + '\'' +
                ", name='" + name + '\'' +
                ", creatTime=" + creatTime +
                ", updateTime=" + updateTime +
                '}';
    }
}

5、缓存路由

package com.unidt.api;


import com.unidt.norter.bean.testcache.CacheTestBean;
import com.unidt.norter.serviceImpl.normal.CacheTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author songchun.yang
 * @Description:
 * @Date Created in 2020/4/1 17:09
 */
@RestController
@RequestMapping("/Cache")
public class CacheTestController {
    @Autowired
    CacheTestService cacheTestService;


    /**
     *@Description: 测试缓存
     *@param cacheTestBean
     *@return com.zyj.bean.CacheTestBean
     *@Author: zyj 2018/5/26 22:47
     */
    @RequestMapping("/add")
    public CacheTestBean testAdd(CacheTestBean cacheTestBean){
        return cacheTestService.add(cacheTestBean);
    }


    /**
     *@Description: 测试获取缓存
     *@param key
     *@return com.zyj.bean.CacheTestBean
     *@Author: zyj 2018/5/26 22:47
     */
    @RequestMapping("/get")
    public CacheTestBean testGet(String key){
        CacheTestBean current=cacheTestService.get(key);
        System.out.println("获得数据"+current);
        return current;
    }


    /**
     *@Description: 测试删除缓存
     *@param key
     *@return com.zyj.bean.CacheTestBean
     *@Author: zyj 2018/5/26 22:47
     */
    @RequestMapping("/del")
    public CacheTestBean testDel(String key){
        return cacheTestService.del(key);
    }
}