springboot基础(43):j2cache
j2cache是一个缓存整合框架,可以提供缓存的整套方案,使各种缓存搭配使用。本章介绍ehcache+redis整合。
·
前言
j2cache是一个缓存整合框架,可以提供缓存的整套方案,使各种缓存搭配使用。
本章介绍ehcache+ redis 整合
如何使用j2cache进行整合
- 导入依赖
<!--j2cache-->
<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>
<!--j2cache默引入了redis的依赖-->
<!--ehcache-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
- 配置application.yml等文件
application.yml
j2cache:
config-location: j2cache.properties
ehcache.xml
<?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>
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
- 使用CacheChannel
@RestController
@RequestMapping("/sms")
public class SmsCodeController {
@Autowired
private SmsUtils smsUtils;
@Autowired
private CacheChannel cacheChannel;
@GetMapping
public String getCode(String phone) {
String code= smsUtils.createCode(phone);
cacheChannel.set("sms",phone,code);
return code;
}
@PostMapping
public boolean checkCode(SmsCode smsCode) {
String code = smsCode.getCode();
// String code2 = smsUtils.getCode(smsCode.getPhone());
String code2 = cacheChannel.get("sms",smsCode.getPhone()).asString();
return code.equals(code2);
}
}
- 启动服务器测试
查看缓存,可以看到它的key的组成
设置模式
启动服务器,控制台会警告没有设置redis的模式,此时只要在j2cache.properties里配置模式即可。
# 设置模式
redis.mode = single
设置命名空间
在j2cache.properties里配置命名空间即可。
# redis缓存命名空间(可选值),默认空
redis.namespace = j2cache
关闭二级缓存
j2cache.properties不启动二级缓存,ehache是一级缓存,redis是二级缓存。j2cache可以通过配置关闭二级缓存。
# 设置是否启用2级缓存
j2cache.l2-cache-open=false
配置内容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
# 设置模式
redis.mode = single
# redis缓存命名空间(可选值),默认空
redis.namespace = j2cache
# 设置是否启用2级缓存
j2cache.l2-cache-open=false
关于报错
错误1. classpath包含了多个SLF4J
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/developsoft/maven/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/developsoft/maven/repository/org/slf4j/slf4j-simple/1.7.36/slf4j-simple-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
解决方法:
使用排除依赖,将slf4j-simple去掉
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>2.8.4-release</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)