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

【框架整合】利用j2cache实现二级缓存方案1 核心原理 二级缓存方案实现主要在于利用 redis 做远程缓存 caffeine 做本地缓存 形成本地缓存为一级缓存 redis 为二级缓存 2 准备 引入依赖 lt dependency gt lt groupId gt org springframew boot groupId

大家好,我是讯享网,很高兴认识大家。

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; } 
小讯
上一篇 2025-03-27 12:47
下一篇 2025-03-14 22:13

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/92524.html