当前位置: 首页 > news >正文

spring cache 支持多结构的 Redis 缓存管理器

需求背景:spring cache 使用多级缓存,缓存实现方式用redis时,默认存储的结构是string,结果,但项目中需要使用其他结果,于是想到自己定义一个实现类,然后根据类型进行切换不同的实现方式,于是乎有了下面多结构缓存管理器,核心方法是createRedisCache

import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import java.util.Map;
import java.util.Objects;/*** 支持多结构的 Redis 缓存管理器* 可根据缓存名前缀自动选择缓存结构(Hash/ZSet/普通字符串)*/
public class StructRedisCacheManager extends RedisCacheManager {// 缓存结构类型枚举public enum CacheStructType {HASH,   // Hash 结构ZSET,   // ZSet 结构STRING  // 普通字符串结构(默认)}// 结构前缀(用于标识缓存类型,例如 "hash:userCache" 表示 Hash 结构)private static final String HASH_PREFIX = "hash:";private static final String ZSET_PREFIX = "zset:";private final RedisConnectionFactory connectionFactory;// 各结构的默认配置(是否允许覆盖/重复)private final boolean hashAllowOverwrite;  // Hash 是否允许覆盖private final boolean zsetAllowDuplicate; // ZSet 是否允许重复元素public StructRedisCacheManager(RedisCacheWriter cacheWriter,RedisCacheConfiguration defaultCacheConfig,RedisConnectionFactory connectionFactory,boolean hashAllowOverwrite,boolean zsetAllowDuplicate) {super(cacheWriter, defaultCacheConfig);this.connectionFactory = connectionFactory;this.hashAllowOverwrite = hashAllowOverwrite;this.zsetAllowDuplicate = zsetAllowDuplicate;}/*** 重载构造器(支持自定义缓存配置)*/public StructRedisCacheManager(RedisCacheWriter cacheWriter,RedisCacheConfiguration defaultCacheConfig,Map<String, RedisCacheConfiguration> initialCacheConfigurations,RedisConnectionFactory connectionFactory,boolean hashAllowOverwrite,boolean zsetAllowDuplicate) {super(cacheWriter, defaultCacheConfig, initialCacheConfigurations);this.connectionFactory = connectionFactory;this.hashAllowOverwrite = hashAllowOverwrite;this.zsetAllowDuplicate = zsetAllowDuplicate;}/*** 核心:根据缓存名选择对应的缓存结构实现*/@Overrideprotected RedisCache createRedisCache(String cacheName, RedisCacheConfiguration config) {// 解析缓存结构类型CacheStructType structType = parseStructType(cacheName);// 提取业务缓存名(去除结构前缀)String bizName = extractBizName(cacheName, structType);// 根据类型创建对应缓存实例switch (structType) {case HASH:// 创建 Hash 结构缓存return new HashRedisCache(bizName,getCacheWriter(),config != null ? config : getDefaultCacheConfiguration(),connectionFactory,hashAllowOverwrite);case ZSET:// 创建 ZSet 结构缓存return new ZSetRedisCache(bizName,getCacheWriter(),config != null ? config : getDefaultCacheConfiguration(),connectionFactory,zsetAllowDuplicate);default:// 默认使用普通字符串缓存(Spring 原生实现)return super.createRedisCache(bizName, config);}}/*** 解析缓存名对应的结构类型(通过前缀判断)*/private CacheStructType parseStructType(String cacheName) {if (cacheName.startsWith(HASH_PREFIX)) {return CacheStructType.HASH;} else if (cacheName.startsWith(ZSET_PREFIX)) {return CacheStructType.ZSET;} else {return CacheStructType.STRING;}}/*** 提取业务缓存名(去除结构前缀)*/private String extractBizName(String cacheName, CacheStructType structType) {switch (structType) {case HASH:return cacheName.substring(HASH_PREFIX.length());case ZSET:return cacheName.substring(ZSET_PREFIX.length());default:return cacheName;}}
}

http://www.dtcms.com/a/544262.html

相关文章:

  • CPU 架构(CPU Architecture)
  • 国产固态硬盘推荐:天硕工业级SSDDRAM缓存与HMB技术详解
  • arcgis中建立渔网,fragtats计算景观指数(纯小白-详细教程)
  • SpringGateway处理跨域
  • 网站被收录后又被提出了免费咨询平台
  • 【printpdf】readme.md文件详解
  • 环形缓冲区实现共享内存
  • Spring AI 搭建 RAG 个人知识库
  • 网站地址是什么用淘宝域名做网站什么效果
  • SPSSAU「质量控制」模块:从可视化监控到过程优化,一站式搞定质量难题
  • 基于健康指标的自动驾驶全系统运行时安全分析方法
  • 从 0 到 1 开发 Rust 分布式日志服务:高吞吐设计 + 存储优化,支撑千万级日志采集
  • 如何做好网站的推广工作成都百度爱采购
  • [无人机sdk] Open Protocol | 协议包构造验证
  • 【Vscode】解决ssh远程开发时Formatter失效的问题
  • TCP 如何保证传输的可靠性?
  • 亲子娱乐升级!Docker 电视盒子 ADB 安装助手,儿童 APP 一键装满电视
  • Microsoft 365 Copilot 扩展至应用和工作流构建功能
  • 【Latex】本地部署latex+vscode
  • 注册中心(环境隔离、分级模型、Eureka)、远程调用负载均衡、服务保护原理分析
  • 有没有专门做建筑造价的私单网站网站开发风险
  • LSTM模型做二分类(PyTorch实现)
  • Linux 文件变动监控工具:原理、设计与实用指南(C/C++代码实现)
  • 建站之星怎么用做视频解析网站犯法吗
  • LibreTV无广告观影实测:聚合全网资源,远程访问家庭影院新方案!
  • 仓颉中的 UTF-8 编码处理:从 DFA 解码、错误策略到流式与字素迭代的工程实战
  • 【React】打卡笔记,入门学习02:react-router
  • Latex 转 word 在线
  • 【OD刷题笔记】- 可以组成网络的服务器
  • 《算法闯关指南:优选算法--前缀和》--27.寻找数组的中心下标,28.除自身以外数组的乘积