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

1、核心原理

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

2、准备

  1. 引入依赖
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
    <version>3.1.5version>
dependency>
<dependency>
    <groupId>org.yamlgroupId>
    <artifactId>snakeyamlartifactId>
    <version>2.2version>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-aopartifactId>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
dependency>
<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-pool2artifactId>
dependency>
<dependency>
    <groupId>net.oschina.j2cachegroupId>
    <artifactId>j2cache-spring-boot2-starterartifactId>
    <version>2.8.0-releaseversion>
dependency>
<dependency>
    <groupId>net.oschina.j2cachegroupId>
    <artifactId>j2cache-coreartifactId>
    <version>2.8.5-releaseversion>
    <exclusions>
        <exclusion>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
        exclusion>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
        exclusion>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
        exclusion>
    exclusions>
dependency>
<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>2.0.42version>
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;
}

你可能感兴趣的:(框架整合,java,redis,缓存)