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

springboot Redis 支持星号(*) 包括注解@Cache

通过自定义CacheManager Bean来实现

bean

   @Autowiredprivate RedisConnectionFactory redisConnectionFactory;/*** 管理缓存** @return*///缓存管理器@Primary@Bean@Overridepublic CacheManager cacheManager() {// 使用自定义的缓存配置初始化一个cacheManagerreturn new CustomizedRedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), redisCacheConfiguration());}

CustomizedRedisCacheManager

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.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;/*** @author kittlen* @version 1.0* @date 2021/11/17 0017*/
public class CustomizedRedisCacheManager extends RedisCacheManager {private final RedisCacheWriter cacheWriter;private final RedisCacheConfiguration defaultCacheConfig;private final Map<String, RedisCacheConfiguration> initialCaches = new LinkedHashMap<>();private boolean enableTransactions;public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {super(cacheWriter, defaultCacheConfiguration);this.cacheWriter = cacheWriter;this.defaultCacheConfig = defaultCacheConfiguration;}public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) {super(cacheWriter, defaultCacheConfiguration, initialCacheNames);this.cacheWriter = cacheWriter;this.defaultCacheConfig = defaultCacheConfiguration;}public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowInFlightCacheCreation, String... initialCacheNames) {super(cacheWriter, defaultCacheConfiguration, allowInFlightCacheCreation, initialCacheNames);this.cacheWriter = cacheWriter;this.defaultCacheConfig = defaultCacheConfiguration;}public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations) {super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations);this.cacheWriter = cacheWriter;this.defaultCacheConfig = defaultCacheConfiguration;}public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations, boolean allowInFlightCacheCreation) {super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations, allowInFlightCacheCreation);this.cacheWriter = cacheWriter;this.defaultCacheConfig = defaultCacheConfiguration;}public CustomizedRedisCacheManager(RedisConnectionFactory redisConnectionFactory, RedisCacheConfiguration cacheConfiguration) {this(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),cacheConfiguration);}/*** 覆盖父类创建RedisCache*/@Overrideprotected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {return new CustomizedRedisCache(name, cacheWriter, cacheConfig != null ? cacheConfig : defaultCacheConfig);}@Overridepublic Map<String, RedisCacheConfiguration> getCacheConfigurations() {Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>(getCacheNames().size());getCacheNames().forEach(it -> {RedisCache cache = CustomizedRedisCache.class.cast(lookupCache(it));configurationMap.put(it, cache != null ? cache.getCacheConfiguration() : null);});return Collections.unmodifiableMap(configurationMap);}
}

CustomizedRedisCache

import org.springframework.core.convert.ConversionService;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;/*** @author kittlen* @version 1.0* @date 2021/11/17 0017*/
public class CustomizedRedisCache extends RedisCache {private static final String WILD_CARD = "*";private final String name;private final RedisCacheWriter cacheWriter;private final ConversionService conversionService;protected CustomizedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) {super(name, cacheWriter, cacheConfig);this.name = name;this.cacheWriter = cacheWriter;this.conversionService = cacheConfig.getConversionService();}@Overridepublic void evict(Object key) {if (key instanceof String) {String keyString = key.toString();if (keyString.endsWith(WILD_CARD)) {evictLikeSuffix(keyString);return;}if (keyString.startsWith(WILD_CARD)) {evictLikePrefix(keyString);return;}}super.evict(key);}/*** 前缀匹配** @param key*/public void evictLikePrefix(String key) {byte[] pattern = this.conversionService.convert(this.createCacheKey(key), byte[].class);this.cacheWriter.clean(this.name, pattern);}/*** 后缀匹配** @param key*/public void evictLikeSuffix(String key) {byte[] pattern = this.conversionService.convert(this.createCacheKey(key), byte[].class);this.cacheWriter.clean(this.name, pattern);}}

相关文章:

springboot Redis 支持星号(*) 包括注解@Cache

通过自定义CacheManager Bean来实现 bean Autowiredprivate RedisConnectionFactory redisConnectionFactory;/*** 管理缓存** return*///缓存管理器PrimaryBeanOverridepublic CacheManager cacheManager() {// 使用自定义的缓存配置初始化一个cacheManagerreturn new Custom…...

2023.5.12 第43周周报

学习时间&#xff1a;2023.5.5-2023.5.12 学习内容&#xff1a; 1、answer question: img&#xff1a; 看到有论文说应该让图像和文本的潜在嵌入具有相似和合理的数值范围【-2&#xff0c;2】 调试发现模型的文本图像的潜在嵌入虽然符合&#xff0c;但相差较大。 在将文本和…...

JavaEE 多线程详细讲解(2)

1.线程不安全分析 &#xff08;1&#xff09;线程不安全的主要原因就是&#xff0c;系统的抢占式执行&#xff0c;对于内核设计者来说&#xff0c;这是非常方便的一个执行方式&#xff0c;但是这却却导致线程不安全的问题&#xff0c;也有不抢占执行的系统&#xff0c;但是这种…...

Flask-HTTP请求、响应、上下文、进阶实验

本节主要目录如下&#xff1a; 一、请求响应循环 二、HTTP请求 2.1、请求报文 2.2、Request对象 2.3、在Flask中处理请求 2.4、请求钩子 三、HTTP响应 3.1、响应报文 3.2、在Flask中生成响应 3.3、响应格式 3.4、Cookie 3.5、session&#xff1a;安全的Cookie 四、…...

springboot 设置response和request的默认格式 驼峰或者SNAKE_CASE

springboot 设置response和request的默认格式 驼峰或者SNAKE_CASE。 我们使用默认配置的情况下&#xff0c;response和request是由jackson jason序列化和解析的&#xff0c;因此&#xff0c;我们只需要配置好jackson json的默认格式就可以。 要设置 jackson json默认的更多格式…...

VR全景技术在养老院的应用优势浅析

随着时代的快速发展&#xff0c;人口老龄化越来越严重&#xff0c;如何利用VR技术提升养老服务的质量&#xff0c;成为了社会各界关注的焦点。为养老院拍摄制作VR全景&#xff0c;不仅能够为养老院的老人子女们跨越空间限制&#xff0c;实现与家人的情感连接&#xff0c;还可以…...

[Spring Cloud] (6)gateway整体加解密

文章目录 简述整体效果后端增加配置nacos增加配置GlobalConfig 添加请求整体解密拦截器DecryptionFilter添加响应整体解密拦截器EncryptionFilter 前端请求拦截器添加整体加密逻辑请求头中添加sessionId 响应拦截器添加整体解密逻辑 简述 本文网关gateway&#xff0c;微服务&a…...

RUST编程语言入门基础2024

庄晓立&#xff0c;2024年3月。 Rust简介 A language empowering everyone to build reliable and efficient software. Rust编程语言赋能所有人开发高可靠且高性能的软件。 性能 Rust is blazingly fast and memory-efficient: with no runtime or garbage collector, it can…...

Linux进程控制——Linux进程终止

前言&#xff1a;前面了解完前面的Linux进程基础概念后&#xff0c;我们算是解决了Linux进程中的一大麻烦&#xff0c;现在我们准备更深入的了解Linux进程——Linux进程控制&#xff01; 我们主要介绍的Linux进程控制内容包括&#xff1a;进程终止&#xff0c;进程等待与替换&a…...

利用IP地址查询解决被“薅羊毛”的方法

在互联网时代&#xff0c;随着各种网络诈骗手段的不断更新和演变&#xff0c;“薅羊毛”成为了一种常见的网络犯罪行为。其中&#xff0c;利用查询IP地址进行欺诈活动已经成为一种普遍的手段。当个人或组织的IP地址被不法分子查询后&#xff0c;可能会面临虚假注册、盗取个人信…...

Tomcat7+ 弱口令 后台getshell漏洞

1 漏洞背景 Tomcat 是一个流行的开源Web应用服务器&#xff0c;用于部署和运行Java Web应用程序。Tomcat 7 版本中存在一个安全隐患&#xff0c;即默认的管理员密码可能较弱或者未被修改&#xff0c;攻击者可以利用这一漏洞登录到Tomcat的管理后台&#xff0c;并上传恶意的WAR…...

香港虚拟主机哪里可以试用?用于企业建站的

香港虚拟主机适合个人、企业建站&#xff0c;包括外贸企业网站、个人博客网站、中小企业官网等&#xff0c;那么作为新手不知道哪家香港虚拟主机好用的时候&#xff0c;该如何找到可以试用的香港虚拟主机呢&#xff1f; 香港虚拟主机也称作香港空间、香港虚拟空间&#xff0c;…...

C# 集合(四) —— Set类

总目录 C# 语法总目录 集合四 Set 1. Set 1. Set 有 HashSet 和 SortedSet&#xff0c; 它们都不包含重复元素忽略添加重复值的请求无法根据位置访问元素使用Contains方法均使用散列查找&#xff0c;所以速度快 SortedSet 按照一定顺序保存元素&#xff0c;使用红黑树实现&a…...

C#实现多线程的几种方式

前言 多线程是C#中一个重要的概念&#xff0c;多线程指的是在同一进程中同时运行多个线程的机制。多线程适用于需要提高系统并发性、吞吐量和响应速度的场景&#xff0c;可以充分利用多核处理器和系统资源&#xff0c;提高应用程序的性能和效率。 多线程常用场景 CPU 密集型任务…...

C语言—控制语句

控制语句就是用来实现对流程的选择、循环、转向和返回等控制行为。 分支语句 if语句 基本结构 if(表达式) { 语句块1&#xff1b; } else { 语句块2&#xff1b; } 执行顺序&#xff1a; 如果表达式判断成立&#xff08;即表达式为真&#xff09;&#xff0c;则执行语句块…...

三. TensorRT基础入门-ONNX注册算子的方法

目录 前言0. 简述1. 执行一下我们的python程序2.转换swin-tiny时候出现的不兼容op的例子3. 当出现导出onnx不成功的时候&#xff0c;我们需要考虑的事情4. unsupported asinh算子5. unsupported deformable conv算子总结参考 前言 自动驾驶之心推出的 《CUDA与TensorRT部署实战…...

01、什么是ip、协议、端口号知道吗?计算机网络通信的组成是什么?

声明&#xff1a;本教程不收取任何费用&#xff0c;欢迎转载&#xff0c;尊重作者劳动成果&#xff0c;不得用于商业用途&#xff0c;侵权必究&#xff01;&#xff01;&#xff01; 目录 前言 计算机网络 网络ip地址 网络协议 网络端口号 前言 最近有个项目要用到相关文章…...

答题套路2 阅读理解 说明文某个词是否能去掉

观点 回答&#xff1a;不能 解词 某个词什么意思需要解释一下 反证法 如果去掉了&#xff0c;会怎么样 总结 使用这个词体现了说明文的科学性&#xff0c;严谨性...

Pytorch图像分类模型模型实时在线验证代码

1.训练并保存自己的模型 保存的模型格式为&#xff1a;XXX.pth torch.save(model, "./weight/last.pth")if best_acc <(validation_acc / len_val):torch.save(model, "./weight/best.pth")2.转化为ONNX格式 2.1环境安装&#xff08;window10&#x…...

Java高并发场景(银行转账问题)

最近面试问到了银行转账的高并发问题&#xff0c;回答的不是很理想&#xff0c;小编整理了下&#xff0c;题目大概如下&#xff1a; 有一张银行账号表&#xff08;银行账号字段、金额字段&#xff09;&#xff0c;A账号要给B账号转账&#xff0c;A扣款&#xff0c;B收款&#x…...

如何快速解决AMD Ryzen系统调试问题:SMUDebugTool完整使用指南

如何快速解决AMD Ryzen系统调试问题&#xff1a;SMUDebugTool完整使用指南 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: ht…...

短视频 SEO 如何提高网站的搜索排名

为什么短视频 SEO 是提高网站搜索排名的关键 在当今数字化时代&#xff0c;短视频平台已经成为人们获取信息和娱乐的主要渠道。短视频的流行不仅改变了人们的观看习惯&#xff0c;还深刻影响了网络营销的方式。如何利用短视频 SEO&#xff08;搜索引擎优化&#xff09;来提高网…...

当AI学会“越狱“与“签名“:大模型 安全的攻与防

当AI学会"越狱"与"签名"&#xff1a;大模型安全的攻与防引言2023年以来&#xff0c;以ChatGPT、GPT-4、LLaMA、Qwen为代表的大语言模型&#xff08;Large Language Models, LLMs&#xff09;席卷了几乎所有行业。然而&#xff0c;能力越大&#xff0c;风险…...

InfluxDB新手必看:从安装到基本操作的完整指南(Windows版)

InfluxDB Windows实战指南&#xff1a;从零搭建时序数据库系统 时序数据正成为物联网、DevOps和业务监控领域的核心资产。想象一下&#xff0c;您需要每秒处理数千台设备的温度读数&#xff0c;或者分析应用程序每分钟的性能指标——传统关系型数据库在这种高频写入场景下往往…...

ESP32开发环境:VS Code与ESP-IDF插件高效配置指南

1. 为什么选择VS Code开发ESP32&#xff1f; 第一次接触ESP32开发时&#xff0c;我尝试过各种开发工具&#xff1a;Arduino IDE、PlatformIO、Eclipse...最后发现VS Code配合ESP-IDF插件才是最佳组合。这个方案不仅免费开源&#xff0c;更重要的是能充分发挥ESP32的全部性能特…...

2026年了,为什么很多企业做了智慧气象,结果还是没把风险降下来?

上个月&#xff0c;和一位新能源集团的运营负责人聊天&#xff0c;他抛出一个百思不得其解的问题&#xff1a;“我们花了300多万上了智慧气象系统&#xff0c;接了精细化预报&#xff0c;预警信息每天推送到手机、电脑、大屏&#xff0c;三个渠道同步。结果上个月一场雷暴&…...

ED-最优设计实战:如何用Python实现鲁棒实验设计(附完整代码)

ED-最优设计实战&#xff1a;如何用Python实现鲁棒实验设计&#xff08;附完整代码&#xff09; 在数据科学和工程领域&#xff0c;实验设计是优化参数估计和模型性能的关键环节。传统D-最优设计虽然经典&#xff0c;但在面对参数不确定性时往往表现不佳。本文将带你深入理解ED…...

华三中小型企业二层组网配置案例一(单ISP+单链路)

1. 组网拓扑某企业内部共划分 4 个业务部门&#xff0c;为实现部门间网络隔离与安全访问控制&#xff0c;分别规划独立网段&#xff1a;192.168.10.0/24、192.168.20.0/24、192.168.30.0/24、192.168.40.0/24。核心交换机作为三层网关&#xff0c;配置各网段 VLANIF 接口地址&a…...

影墨·今颜GPU算力适配:RTX 4090单卡实测每秒1.8张1024x1536图

影墨今颜GPU算力适配&#xff1a;RTX 4090单卡实测每秒1.8张1024x1536图 1. 引言&#xff1a;当顶级AI影像遇上顶级显卡 如果你是一位内容创作者&#xff0c;或者对AI生成人像有浓厚兴趣&#xff0c;那么“影墨今颜”这个名字最近可能已经进入了你的视野。它被描述为一款融合…...

别再只会用‘Let‘s think step by step’了:DeepSeek-R1原生CoT机制详解与实战调优

解锁DeepSeek-R1推理潜能&#xff1a;原生思维链技术深度解析与高阶应用指南 当我们在数学考试中遇到复杂题目时&#xff0c;老师总会强调"把解题过程写清楚"。这种分步思考的方式&#xff0c;正是人类解决复杂问题的核心方法。如今&#xff0c;大语言模型也掌握了这…...