spring boot 整合缓存的内容呢 已经学了好久了
那么 今天 我们开始学习 j2cache

这个技术 并不是一个缓存 而是一个框架 我们可以将其他缓存配到这套框架上来

那么 我们就还是弄最熟悉的 ehcache + redis进行整合

首先 我们启动 redis

springboot shiro 无操作一段时间出现per_后端


然后 我们打开项目 pom.xml 注入依赖

<dependency>
	<groupId>net.oschina.j2cache</groupId>
	<artifactId>j2cache-core</artifactId>
	<version>2.8.4-release</version>
</dependency>

<dependency>
	<groupId>net.oschina.j2cache</groupId>
	<artifactId>j2cache-spring-boot2-starter</artifactId>
	<version>2.8.0-release</version>
</dependency>
<dependency>
	<groupId>net.sf.ehcache</groupId>
	<artifactId>ehcache</artifactId>
</dependency>

第一个 是 j2cache 的核心包 第二个是 j2cache与spring bott整合的工具
然后 最后是 ehcache的包 因为我们还需要用 ehcache

配置文件 application.yml 加入如下内容

j2cache:
  config-location: j2cache.properties

springboot shiro 无操作一段时间出现per_spring boot_02


然后 我们在 resources 目录下创建一个文件 如下图

springboot shiro 无操作一段时间出现per_redis_03


叫 j2cache.properties

这就是 j2cache 配置文件了

然后 同目录 我们再创建一个 ehcache.xml
编写 ehcache的配置
编写代码如下

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://www.ehcache.org/ehcache.xsd"
         updateCheck="false">
    <diskStore path="D:\ehcache" />

    <!--默认缓存策略 -->
    <!-- external:是否永久存在,设置为true则不会被清除,此时与timeout冲突,通常设置为false-->
    <!-- diskPersistent:是否启用磁盘持久化-->
    <!-- maxElementsInMemory:最大缓存数量-->
    <!-- overflowToDisk:超过最大缓存数量是否持久化到磁盘-->
    <!-- timeToIdleSeconds:最大不活动间隔,设置过长缓存容易溢出,设置过短无效果,可用于记录时效性数据,例如验证码-->
    <!-- timeToLiveSeconds:最大存活时间-->
    <!-- memoryStoreEvictionPolicy:缓存清除策略-->
    <defaultCache
            eternal="false"
            diskPersistent="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            timeToIdleSeconds="60"
            timeToLiveSeconds="60"
            memoryStoreEvictionPolicy="LRU" />

    <cache
            name="smscode"
            eternal="false"
            diskPersistent="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            timeToIdleSeconds="10"
            timeToLiveSeconds="10"
            memoryStoreEvictionPolicy="LRU" />

</ehcache>

springboot shiro 无操作一段时间出现per_后端_04


j2cache.properties 编写代码如下

# 1级缓存
j2cache.L1.provider_class = ehcache
ehcache.configXml = ehcache.xml

# 2级缓存
j2cache.L2.provider_class =net.oschina.j2cache.cache.support.redis.SpringRedisProvider
j2cache.L2.config_section = redis
redis.hosts = localhost:6379

# 1级缓存中的数据如何到达2级缓存
j2cache.broadcast =net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy

简单说 就是 告诉它 你第一个用什么 我们用 ehcache 然后 第二级缓存 用 redis

然后 我们要在实现这个逻辑的 地方 条件装配一个 CacheChannel

springboot shiro 无操作一段时间出现per_redis_05

@GetMapping("/get")
public String getMin(){
    String code2 = cacheChannel.get("book", "1").asString();
    System.out.println(code2);
    return code2;
}

@RequestMapping(value = "/set", method = RequestMethod.POST)
@ResponseBody
public String setBook(@RequestBody book bindata) {
    cacheChannel.set("book","1",bindata.getName());
    return bindata.getName();
}

简单说 拿着我们的 cacheChannel 对象 调用 set方法

set接受三个参数 名称 key表示 数据

get 接受 名称 key表示 然后返回的 我们可以通过 asString 转为字符串类型

然后读出来

springboot shiro 无操作一段时间出现per_spring boot_06


然后 我们启动项目

springboot shiro 无操作一段时间出现per_缓存_07


然后 我们先执行set

springboot shiro 无操作一段时间出现per_缓存_08


再来get

springboot shiro 无操作一段时间出现per_redis_09


然后 我们到 redis 中 keys *

执行

springboot shiro 无操作一段时间出现per_后端_10


这边 也明显查到了