【框架整合】利用j2cache实现二级缓存方案

1、核心原理

二级缓存方案实现主要在于利用redis 做远程缓存,caffeine 做本地缓存,形成本地缓存为一级缓存,redis为二级缓存

2、准备

  1. 引入依赖
<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>
  1. 添加配置信息
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客户端使用lettuce,也可以使用Jedis
  redis-client: lettuce
  #开启二级缓存,false则表示只使用一级缓存
  l2-cache-open: true
  # 事件通知的机制,j2cache从1.3.0版本开始支持JGroups和Redis Pub/Sub两种方式进行缓存事件的通知。
  # 此处我们使用基于redis的发布订阅模式来通知缓存的各个节点来进行缓存数据的同步(由j2cache进行实现,我们写上配置即可)
  broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
  # broadcast: jgroups
  #指定一级缓存提供者为caffeine
  L1:
    provider_class: caffeine
  #指定二级缓存提供者为redis
  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是redis的一个客户端,也可以使用jedis,都是用来操作redis的java客户端
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
  1. 调用处理
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;
}
  • 13
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxtx-0510

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值