j2cache入门使用
j2cache是OSChina目前正在使用的两级缓存框架。
j2cache的两级缓存结构:
L进程内缓存caffeine/ehcacheL集中式缓存Redis/Memcached
j2cache其实并不是在重复造轮子,而是作资源整合,即将Ehcache、Caffeine、redis、SpringCache等进行整合。
由于大量的缓存读取会导致L2的网络成为整个系统的瓶颈,因此L1的目标是降低对L2的读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的ehcache缓存数据丢失。
j2cache从0版本开始支持JGroups和RedisPub/Sub两种方式进行缓存事件的通知。
数据读取顺序->L1->L2->DB
使用j2cache需要导入的maven坐标:
<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>
j2cache入门案例
第一步:创建maven工程j2cache_demo并配置pom.xml文件
第二步:创建application.yml
server:
port: 9000
# redis 通用配置, 不同的环境,需要配置不同的链接信息,
# 只需要将这段信息复制到具体环境的配置文件中进行修改即可
# 如:复制到pd-auth-server-dev.yml中将数据库名和ip改掉
pinda:
redis:
ip: 127.0.0.1
port: 6379
password:
database: 0
spring:
cache:
type: GENERIC
redis:
host: ${pinda.redis.ip} # 引用配置文件中的信息
password: ${pinda.redis.password}
port: ${pinda.redis.port}
database: ${pinda.redis.database}
j2cache:
# config-location: /j2cache.properties
open-spring-cache: true
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
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
第三步:创建/resources/caffeine.properties文件
第一缓存区域,以及缓存时间例如:rx=50,2H
#########################################
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
#########################################
default=2000, 2h
rx=50, 2h
第四步:创建MyController
package com.zcl.controller;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 描述:使用j2cache提供的CacheChannel对象作为缓存数据
*
* @author zhong
* @date 2022-09-01 8:08
*/
@RestController
@RequestMapping("/user")
public class UserController {
// 存储密钥
private String key = "myKey";
// 缓存区域
private String region = "rx";
/**
* 注入j2cache对象
*/
@Resource
private CacheChannel cacheChannel;
/**
* 写入缓存
* @return
*/
@GetMapping("/getInfos")
public List<String> getInfos(){
CacheObject cacheObject = cacheChannel.get(region, key);
if(cacheObject.getValue() == null){
//缓存中没有找到,查询数据库获得
List<String> data = new ArrayList<String>();
data.add("info1");
data.add("info2");
//放入缓存
cacheChannel.set(region,key,data);
return data;
}
return (List<String>) cacheObject.getValue();
}
}
第五步:创建启动类
package com.zcl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 描述:启动类
*
* @author zhong
* @date 2022-09-01 8:16
*/
@SpringBootApplication
public class J2CacheApp {
public static void main(String[] args) {
SpringApplication.run(J2CacheApp.class,args);
}
}
注意:由于我们当前第二级缓存使用的是redis,所以需要启动redis服务才能正常运行入门案例。
可以发现redis中已经缓存了数据:
重启项目,再次访问
如果项目存储保存之后再次请求,获取的数据还是在一级缓存中获取重启项目,由于j2cache的一级缓存是进程级缓存,重启后一级缓存消失。但是二级缓存的数据还存在,再次访问上面地址,通过debug断点调试可以看到程序从redis中获取了缓存数据。【lervel=2】
第六步、完成控制器的开发
在原来的基础上进行添加控制器接口,测试时指定请求接口即可
package com.zcl.controller;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 描述:使用j2cache提供的CacheChannel对象作为缓存数据
*
* @author zhong
* @date 2022-09-01 8:08
*/
@RestController
@RequestMapping("/user")
public class UserController {
// 存储密钥
private String key = "myKey";
// 缓存区域
private String region = "rx";
/**
* 注入j2cache对象
*/
@Resource
private CacheChannel cacheChannel;
/**
* 写入缓存
* @return
*/
@GetMapping("/getInfos")
public List<String> getInfos(){
CacheObject cacheObject = cacheChannel.get(region, key);
if(cacheObject.getValue() == null){
//缓存中没有找到,查询数据库获得
List<String> data = new ArrayList<String>();
data.add("info1");
data.add("info2");
//放入缓存
cacheChannel.set(region,key,data);
return data;
}
return (List<String>) cacheObject.getValue();
}
/**
* 清理指定缓存【L1L2】
* @return
*/
@GetMapping("/evict")
public String evict(){
cacheChannel.evict(region,key);
return "evict success";
}
/**
* 检测存在那级缓存
* @return
*/
@GetMapping("/check")
public String check(){
int check = cacheChannel.check(region, key);
return "level:" + check;
}
/**
* 检测缓存数据是否存在
* @return
*/
@GetMapping("/exists")
public String exists(){
boolean exists = cacheChannel.exists(region, key);
return "exists:" + exists;
}
/**
* 清理指定区域的缓存
* @return
*/
@GetMapping("/clear")
public String clear(){
cacheChannel.clear(region);
return "clear success";
}
}
第七步、测试
文章为作者独立观点,不代表股票交易接口观点