1、核心原理
二级缓存方案实现主要在于利用redis 做远程缓存,caffeine 做本地缓存,形成本地缓存为一级缓存,redis为二级缓存
2、准备
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</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.5-release</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.42</version>
</dependency>
- 添加配置信息
server:
port: 6650
nosql:
redis:
host: 192.168.43.135
port: 6379
password:
database: 0
j2cache:
open-spring-cache: true
cache-clean-mode: passive
allow-null-values: true
redis-client: lettuce
l2-cache-open: true
broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
L1:
provider_class: caffeine
L2:
provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider
config_section: lettuce
sync_ttl_to_redis: true
default_cache_null_object: false
serialization: fst
caffeine:
properties: /caffeine.properties
lettuce:
mode: single
namespace:
storage: generic
channel: j2cache
scheme: redis
hosts: ${nosql.redis.host}:${nosql.redis.port}
password: ${nosql.redis.password}
database: ${nosql.redis.database}
sentinelMasterId:
maxTotal: 100
maxIdle: 10
minIdle: 10
timeout: 10000
- 调用处理
private String key = "myKey";
private String region = "caffeineRegion";
@Resource
private CacheChannel cacheChannel;
@GetMapping("/getValue")
public String getValue() {
CacheObject cacheObject = cacheChannel.get(region, key);
if (cacheObject.getValue() == null) {
String dbString = "db data";
cacheChannel.set(region, key, dbString);
return dbString;
}
return cacheObject.getValue().toString();
}
@GetMapping("evict")
public String evict() {
cacheChannel.evict(region, key);
return "evict success";
}
@GetMapping("clear")
public String clear() {
cacheChannel.clear(region);
return "clear success";
}
@GetMapping("exists")
public String exists() {
boolean exists = cacheChannel.exists(region, key);
return key + "is exists :" + exists;
}