j2chache
1.什么是j2cache?
J2cache是OSChina开发的一款二级缓存框架,但它并没有重复造*而是将资源进行了一个整合,即将Ehcache、Caffeine、redis、Spring Cache等进行整合。
J2cache分为2层缓存结构 第一层为进程式缓存caffeine 第二层为集中式缓存 redis
j2cache从1.3.0版本开始支持JGroups和Redis Pub/Sub两种方式进行缓存事件的通知。
2. 快速入门j2cache
1.导入maven坐标
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-spring-boot2-starter</artifactId>
<version>2.8.0-release</version>
</dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>2.8.0-release</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.yml配置文件
server:
port: 9000
pinda:
redis:
ip: 127.0.0.1
port: 6379
database: 0
spring:
redis:
host: ${pinda.redis.ip}
port: ${pinda.redis.port}
database: ${pinda.redis.database}
j2cache:
cache-clean-mode: passive
allow-null-values: true
redis-client: lettuce #指定redis客户端使用lettuce,也可以使用Jedis
l2-cache-open: true #开启二级缓存
broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
# broadcast: jgroups
L1: #指定一级缓存提供者为caffeine
provider_class: caffeine
L2: #指定二级缓存提供者为redis
provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider
config_section: lettuce
sync_ttl_to_redis: true
default_cache_null_object: false
serialization: fst #序列化方式:fst、kyro、Java
caffeine:
properties: /caffeine.properties # 这个配置文件需要放在项目中
lettuce:
mode: single
namespace:
storage: generic
channel: j2cache
scheme: redis
hosts: ${pinda.redis.ip}:${pinda.redis.port}
password: ${pinda.redis.password}
database: ${pinda.redis.database}
sentinelMasterId:
maxTotal: 100
maxIdle: 10
minIdle: 10
timeout: 10000
-----------------------------------------------------------------L1一级缓存配置-----------------------------------------------------------------------------
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
default=2000, 2h
rx=50, 2h
3.常用方法
@Autowired
private CacheChannel cacheChannel;
private String region = "abc";
private String key = "location";
/**
* 获取缓存
*
* @return
*/
@GetMapping("/getCacheData")
public List<String> getCacheData() {
CacheObject cacheObject = cacheChannel.get(region, key);
// 如果缓存中没有数据
if (Objects.isNull(cacheObject.getValue())) {
List<String> list = new ArrayList<>();
list.add("TOP");
list.add("JUG");
list.add("MID");
list.add("AD");
list.add("SUP");
// 设置缓存
cacheChannel.set(region, key, list);
}
return (List<String>) cacheObject.getValue();
}
//清理指定缓存
@GetMapping("/evict")
public String evict() {
cacheChannel.evict(region, key);
return "evict";
}
//检测存在那级缓存
@GetMapping("/check")
public String check() {
int check = cacheChannel.check(region, key);
return "level:" + check;
}
//检测缓存数据是否存在
@GetMapping("/exists")
public String exists() {
boolean exists = cacheChannel.exists(region, key);
return "exists:" + exists;
}
//清理指定区域的缓存
@GetMapping("/clear")
public String clear() {
cacheChannel.clear(region);
return "clear";
}