springboot2.0系列十(整合Ehcache)

注解配置与EhCache使用

ehcache是一个纯Java实现的进程内缓存实现,具有快速精简等特点。有单机版本、分布式实现版本。主要针对基于java开发的项目使用。支持磁盘持久化及磁盘load到内存。

介绍

EhCache是基于标志的开源缓存,有很好的性能,可扩展。因为功能强大、经过测试的、功能全而广泛的应用与Java开发的系统中。支持进程内、混合进程内/进程外继承部署。

特点

1、单机版本的ehcache是jvm进程内缓存,不走网卡,速度快、效率高。
2、冷热数据单独处理不方便,正常情况下数据都是放在内存中,超过配置阈值后才会进行持久化磁盘处理。
3、数据的持久化需要在配置文件中配置才会进行,否则ehcache关闭后会删除掉缓存的磁盘文件。
4、如果项目中缓存分类比较多,分类又需要单独配置参数的情况,则配置文件就会比较大,比较麻烦。
5、默认依赖于ehcache.xml配置文件,如果名称不同则需要明确指明配置文件。

pom文件引入

<!--SpringBoot缓存支持启动器 -->

  <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-cache</artifactId>

  </dependency>

  

  <!-- Ehcache坐标 -->

  <dependency>

    <groupId>net.sf.ehcache</groupId>

    <artifactId>ehcache</artifactId>

  </dependency>

 

 

新建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">

      <diskStore path="java.io.tmpdir/Tmp_EhCache" />

 

      <!-- 默认配置 -->

      <defaultCache maxElementsInMemory="5000" eternal="false"

            timeToIdleSeconds="120" timeToLiveSeconds="120"

            memoryStoreEvictionPolicy="LRU" overflowToDisk="false" />

 

      <cache name="baseCache" maxElementsInMemory="10000"

            maxElementsOnDisk="100000" />

 

</ehcache>

配置信息介绍

  1.     <!--  
  2.        name:缓存名称。  
  3.        maxElementsInMemory:缓存最大个数。  
  4.        eternal:对象是否永久有效,一但设置了,timeout将不起作用。  
  5.        timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。  
  6.        timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。  
  7.        overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。  
  8.        diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。  
  9.        maxElementsOnDisk:硬盘最大缓存个数。  
  10.        diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.  
  11.        diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。  
  12.        memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。  
  13.        clearOnFlush:内存数量最大时是否清除。  
  14.     --> 

 

代码使用Cacheabl

package com.example.springbootdistributedtransaction.controller;

  

  

  

  

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.cache.CacheManager;

  import org.springframework.cache.annotation.Cacheable;

  import org.springframework.web.bind.annotation.GetMapping;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  

  import java.util.HashMap;

  import java.util.Map;

  

  @RestController

  public class userController {

   

    @RequestMapping("/getKey")

    @Cacheable(value = "baseCache")

    public Map getKey() {

        System.out.println("执行getkey方法");

        Map<String,String> map=new HashMap<>();

        map.put("name","zhangsan");

        map.put("age","33");

        return map;

    }

    //清除缓存

    @RequestMapping("/remoKey") 

    public void remoKey() {

        cacheManager.getCache("baseCache").clear();

    }

  

}

 

 

启动加入缓存

 

@SpringBootApplication

@EnableCaching // 开启缓存注解

public class SpringbootDistributedTransactionApplication {



    public static void main(String[] args) {

        SpringApplication.run(SpringbootDistributedTransactionApplication.class, args);

    }



}

效果

第一次访问localhost:8080/getKey  控制台打印       执行getkey方法

第二次访问 控制台不打印 说明走了缓存

调用remoKey后再次调用getKey  控制台打印  执行getkey方法

猜你喜欢

转载自blog.csdn.net/wota5037/article/details/111472969