下面代码是一个使用java和redisson实现的三级缓存服务,主要功能包括:
1.缓存结构:
- 本地缓存:使用caffeine实现,最大容量10,000,写入后10分钟过期
- 分布式缓存:使用redisson的rmap结构操作redis
- 数据库:作为最终数据源
2.数据读取流程:
- 先查本地缓存,命中则返回
- 未命中则查redis,命中则更新本地缓存并返回
- 仍未命中则获取锁,再次检查两级缓存(双重检查)
- 最后从数据库读取,更新两级缓存后返回
3.数据更新流程:
- 使用分布式锁保证写操作原子性
- 先更新数据库
- 删除本地缓存和redis缓存
- 通过redis pub/sub发布缓存清除消息给集群内其他节点
- 执行延迟双删(100毫秒后再次删除redis缓存)
4.并发控制:
- 读取时使用本地锁(reentrantlock)防止缓存击穿
- 更新时使用redisson分布式锁(rlock)保证跨节点原子性
- 锁使用完成后从concurrenthashmap中移除
5.集群同步:
- 使用redis的rtopic实现消息发布订阅
- 接收到清除消息时自动删除本地缓存
- 确保集群内各节点缓存一致性
该实现综合运用了延迟双删、发布订阅、锁机制和ttl等多种策略,保障了高并发场景下三级缓存的数据一致性,尤其适合分布式微服务架构。
实战代码:
import java.util.concurrent.*; import java.util.concurrent.locks.reentrantlock; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.redisson.api.*; import com.github.benmanes.caffeine.cache.cache; import com.github.benmanes.caffeine.cache.cachebuilder; @service public class cacheservice { // 本地一级缓存(caffeine) private final cache<string, object> localcache; // redisson客户端,用于分布式操作 private final redissonclient redissonclient; // 锁缓存,用于控制并发 private final concurrenthashmap<string, reentrantlock> lockmap = new concurrenthashmap<>(); // 延迟任务执行器 private final scheduledexecutorservice scheduledexecutorservice; // 主题订阅,用于接收集群消息 private final rtopic cachecleartopic; @autowired public cacheservice(redissonclient redissonclient) { this.redissonclient = redissonclient; this.localcache = cachebuilder.newbuilder() .maximumsize(10_000) .expireafterwrite(10, timeunit.minutes) .build(); this.scheduledexecutorservice = executors.newscheduledthreadpool(5); this.cachecleartopic = redissonclient.gettopic("cache:clear"); // 注册消息监听器 cachecleartopic.addlistener(string.class, (channel, key) -> { localcache.invalidate(key); }); } // 读取缓存 public object get(string key) { // 1. 先查本地缓存 object value = localcache.getifpresent(key); if (value != null) { return value; } // 2. 本地缓存未命中,查redis rmap<string, object> redismap = redissonclient.getmap("cache"); value = redismap.get(key); if (value != null) { localcache.put(key, value); return value; } // 3. redis未命中,查数据库 reentrantlock lock = lockmap.computeifabsent(key, k -> new reentrantlock()); lock.lock(); try { // 双重检查 value = localcache.getifpresent(key); if (value != null) { return value; } value = redismap.get(key); if (value != null) { localcache.put(key, value); return value; } // 从数据库读取 value = readfromdatabase(key); if (value != null) { // 放入redis并设置ttl redismap.put(key, value, 300, timeunit.seconds); // 放入本地缓存 localcache.put(key, value); } return value; } finally { lock.unlock(); lockmap.remove(key); } } // 更新数据 public void update(string key, object value) { // 使用分布式锁保证写操作的原子性 rlock lock = redissonclient.getlock("writelock:" + key); lock.lock(); try { // 1. 更新数据库 boolean success = updatedatabase(key, value); if (success) { // 2. 先删除本地缓存 localcache.invalidate(key); // 3. 删除redis缓存 rmap<string, object> redismap = redissonclient.getmap("cache"); redismap.remove(key); // 4. 发布清除缓存的消息到集群 cachecleartopic.publish(key); // 5. 延迟双删 scheduledexecutorservice.schedule(() -> { redismap.remove(key); }, 100, timeunit.milliseconds); } } finally { lock.unlock(); } } // 从数据库读取数据(示例方法) private object readfromdatabase(string key) { // 实际实现中会查询数据库 return "data_from_db_" + key; } // 更新数据库(示例方法) private boolean updatedatabase(string key, object value) { // 实际实现中会更新数据库 return true; } }
redisson配置
import org.redisson.redisson; import org.redisson.api.redissonclient; import org.redisson.config.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @configuration public class redissonconfig { @bean public redissonclient redissonclient() { config config = new config(); // 单机模式配置 config.usesingleserver() .setaddress("redis://localhost:6379") .setconnectionminimumidlesize(5) .setconnectionpoolsize(50); // 集群模式配置示例 /* config.useclusterservers() .addnodeaddress("redis://node1:6379", "redis://node2:6379") .setscaninterval(2000) .setmasterconnectionminimumidlesize(10) .setmasterconnectionpoolsize(64) .setslaveconnectionminimumidlesize(10) .setslaveconnectionpoolsize(64); */ return redisson.create(config); } }
到此这篇关于java如何实现高并发场景下三级缓存的数据一致性的文章就介绍到这了,更多相关java三级缓存内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论