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

@Cacheable 注解的 @CacheManager 示例

pom.xml 依赖包:

        <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

application.properties

配置如下:

# ############## redis config##########redis.host= xx.xx.xx.xx
redis.port=6579
redis.password=xxxxredis.master=xx
redis.host.proxy=xxxx
redis.port.proxy=6579
redis.password.proxy=xxxx#redis cluster
redis.cache.clusterNodes=xxx:xxx,xxx:xxx,xxx:xxx
redis.cache.password=xxx
redis.cache.commandTimeout=1000
redis.cache.soTimeout=3000
redis.cache.maxAttempts=5
redis.cache.expireSeconds=120
redis.pool.maxActive=30
redis.pool.maxIdle=10
redis.pool.maxTotal=1024
redis.pool.maxWaitMillis=2000
redis.pool.testOnBorrow=false# ############## redis config end##########

代码如下:

@ConfigurationProperties(prefix = “redis”) 表示读取 以redis 开头的配置。

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.*;
import redis.clients.jedis.JedisPoolConfig;import java.time.Duration;@Configuration
@ConfigurationProperties(prefix = "redis")
@Slf4j
public class RedisProxyConfig {@Value("${redis.host.proxy}")private String host;@Value("${redis.port.proxy}")private int port;@Value("${redis.password.proxy}")private String password;@Value("${redis.pool.maxActive}")private int maxActive;@Value("${redis.pool.maxIdle}")private int maxIdle;@Value("${redis.pool.maxTotal}")private int maxTotal;@Value("${redis.pool.maxWaitMillis}")private int maxWaitMillis;@Value("${redis.pool.testOnBorrow}")private boolean testOnBorrow;@Beanpublic JedisPoolConfig jedisPoolConfig() {JedisPoolConfig jc = new JedisPoolConfig();jc.setMaxTotal(maxTotal);jc.setMaxIdle(maxIdle);jc.setMaxWaitMillis(maxWaitMillis);jc.setTestOnBorrow(testOnBorrow);return jc;}@Bean@Autowiredpublic JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {log.info("RedisConfig jedisConnectionFactory Proxy Bean!");JedisConnectionFactory factory = new JedisConnectionFactory();factory.setHostName(host);factory.setPort(port);if (StringUtils.isNotEmpty(password)) {factory.setPassword(password);}factory.setPoolConfig(jedisPoolConfig);factory.setUsePool(true);return factory;}@Bean@Autowiredpublic StringRedisTemplate stringRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {return new StringRedisTemplate(jedisConnectionFactory);}@Beanpublic RedisTemplate redisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate();redisTemplate.setConnectionFactory(jedisConnectionFactory);RedisSerializer<String> stringSerializer = new StringRedisSerializer();RedisSerializer<Object> jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();redisTemplate.setKeySerializer(stringSerializer);redisTemplate.setHashKeySerializer(stringSerializer);redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);return redisTemplate;}@Bean@PrimaryCacheManager cacheManager(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory) {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);RedisCacheManager redisCacheManager = RedisCacheManager.builder(jedisConnectionFactory).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofMinutes(10))).build();
//        redisCacheManager.setUsePrefix(true);
//        redisCacheManager.setDefaultExpiration(600);return redisCacheManager;}/*** 默认1小时有效期,在cacheable注解中未指明时默认使用此cacheManager** @param jedisConnectionFactory* @return*/@BeanCacheManager cacheManagerOneHour(@Autowired RedisConnectionFactory jedisConnectionFactory) {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);RedisCacheManager redisCacheManager = RedisCacheManager.builder(jedisConnectionFactory).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))//entryTtl 表示缓存的时间.entryTtl(Duration.ofMinutes(60))).build();return redisCacheManager;}}

@Cacheable 注解的使用

详情见:https://blog.csdn.net/sinat_32502451/article/details/134310654

相关文章:

@Cacheable 注解的 @CacheManager 示例

pom.xml 依赖包&#xff1a; <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jed…...

springboot二维码示例

pom.xml依赖 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.16</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>…...

nacos做服务配置和服务器发现

一、创建项目 1、创建一个spring-boot的项目 2、创建三个模块file、system、gateway模块 3、file和system分别配置启动信息,并且创建一个简单的控制器 server.port9000 spring.application.namefile server.servlet.context-path/file4、在根目录下引入依赖 <properties&g…...

KCC@广州与 TiDB 社区联手—广州开源盛宴

10月21日&#xff0c;KCC广州与 TiDB 社区联手&#xff0c;在海珠区保利中悦广场 29 楼召开了一次难忘的开源盛宴。这不仅仅是 KCC广州的又一次线下见面&#xff0c;更代表着与 TiDB 社区及广州技术社区的首次深度合作。 活动的策划与组织由 KCC广州负责人 - 惠世冀、PingCAP 的…...

CSS3 分页、框大小、弹性盒子

一、CSS3分页&#xff1a; 网站有很多个页面&#xff0c;需要使用分页来为每个页面做导航。示例&#xff1a; <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; f…...

GEE问题——GEE中循环的使用map()函数,以提取指定范围内的逐日的二氧化氮平均浓度为例

问题: 我有一个简单的代码,可以帮助计算德克萨斯州每个县的对流层二氧化氮平均浓度。目前,我可以将其导出为我指定的任何日期范围的 csv 表,但我想 1) 提取每天平均值,例如 3 个月(2020 年 3 月至 2020 年 5 月,约 90 天)--手动多次运行肯定不是办法,而且我的编码技…...

短信验证码实现(阿里云)

如果实现短信验证&#xff0c;上教程&#xff0c;这里用的阿里云短信服务 短信服务 (aliyun.com) 进入短信服务后开通就行&#xff0c;可以体验100条免费&#xff0c;刚好测试用 这里由自定义和专用&#xff0c;测试的话就选择专用吧&#xff0c;自定义要审核&#xff0c; Se…...

如何对element弹窗进行二次封装

方式一使用$refs 个人比较喜欢用这种的 通过$refs打开的同时 还能给弹窗组件传参 一些框架使用的也是这种方式 父组件 <template><div><el-button type"text" click"handleDialogOpen">打开嵌套表单的 Dialog</el-button><Dia…...

【微服务专题】手写模拟SpringBoot

目录 前言阅读对象阅读导航前置知识笔记正文一、工程项目准备1.1 新建项目1.1 pom.xml1.2 业务模拟 二、模拟SpringBoot启动&#xff1a;好戏开场2.1 启动配置类2.1.1 shen-base-springboot新增2.1.2 shen-example客户端新增启动类 三、run方法的实现3.1 步骤一&#xff1a;启动…...

七个优秀微服务跟踪工具

随着微服务架构复杂性的增加&#xff0c;在问题出现时确定问题的根本原因变得更具挑战性。日志和指标为我们提供了有用的信息&#xff0c;但并不能提供系统的完整概况。这就是跟踪的用武之地。通过跟踪&#xff0c;开发人员可以监控微服务之间的请求进度&#xff0c;从而使他们…...

redis 问题解决 1

1.1 常见考点 1、Redis 为何这么快? Redis 是一款基于内存的数据结构存储系统,它之所以能够提供非常快的读写性能,主要是因为以下几个方面的原因: 基于内存存储:Redis 所有的数据都存储在内存中,而内存的访问速度比磁盘要快得多。因此,Redis 可以提供非常快的读写性能…...

odoo16前端框架源码阅读——启动、菜单、动作

odoo16前端框架源码阅读——启动、菜单、动作 目录&#xff1a;addons/web/static/src 1、main.js odoo实际上是一个单页应用&#xff0c;从名字看&#xff0c;这是前端的入口文件&#xff0c;文件内容也很简单。 /** odoo-module **/import { startWebClient } from "…...

C/C++(a/b)*c的值 2021年6月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析

目录 C/C(a/b)*c的值 一、题目要求 1、编程实现 2、输入输出 二、算法分析 三、程序编写 四、程序说明 五、运行结果 六、考点分析 C/C(a/b)*c的值 2021年6月 C/C编程等级考试一级编程题 一、题目要求 1、编程实现 给定整数a、b、c&#xff0c;计算(a / b)*c的值&…...

CIFAR-100数据集的加载和预处理教程

一、CIFAR-100数据集介绍 CIFAR-100&#xff08;Canadian Institute for Advanced Research - 100 classes&#xff09;是一个经典的图像分类数据集&#xff0c;用于计算机视觉领域的研究和算法测试。它是CIFAR-10数据集的扩展版本&#xff0c;包含了更多的类别&#xff0c;用…...

C#,数值计算——函数计算,Eulsum的计算方法与源程序

1 文本格式 using System; namespace Legalsoft.Truffer { public class Eulsum { private double[] wksp { get; set; } private int n { get; set; } private int ncv { get; set; } public bool cnvgd { get; set; } pri…...

ChatGLM3 langchain_demo 代码解析

ChatGLM3 langchain_demo 代码解析 0. 背景1. 项目代码结构2. 代码解析2-1. utils.py2-2. ChatGLM3.py2-3. Tool/Calculator.py2-4. Tool/Weather.py2-5. main.py 0. 背景 学习 ChatGLM3 的项目内容&#xff0c;过程中使用 AI 代码工具&#xff0c;对代码进行解释&#xff0c;…...

asp.net学院网上报销系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio

一、源码特点 asp.net学院网上报销系统是一套完善的web设计管理系统&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为vs2010&#xff0c;数据库为sqlserver2008&#xff0c;使用c#语言 开发 asp.net学院网上报销系统 应用技术…...

ElasticSearch知识点

什么是ElasticSearch ElasticSearch: 智能搜索&#xff0c;分布式的搜索引擎&#xff0c;是ELK的一个非常完善的产品&#xff0c;ELK代表的是: E就是ElasticSearch&#xff0c;L就是Logstach&#xff0c;K就是kibana Elasticsearch是一个建立在全文搜索引擎 Apache Lucene基础…...

STM32 GPIO

STM32 GPIO GPIO简介 GPIO&#xff08;General Purpose Input Output&#xff09;通用输入输出口&#xff0c;也就是我们俗称的IO口 根据使用场景&#xff0c;可配置为8种输入输出模式 引脚电平&#xff1a;0V~3.3V&#xff0c;部分引脚可容忍5V 数据0就是低电平&#xff0c…...

Electron 开发页面应用

简介 Electron集成了包括chromium&#xff08;理解为具备chrom浏览器的工具&#xff09;&#xff0c;nodejs&#xff0c;native apis chromium&#xff1a;支持最新特性的浏览器。 nodejs&#xff1a;js运行时&#xff0c;可实现文件读写等。 native apis &#xff1a;提供…...

5分钟快速上手:使用Buzz实现高效离线音频转录与翻译的完整指南

5分钟快速上手&#xff1a;使用Buzz实现高效离线音频转录与翻译的完整指南 【免费下载链接】buzz Buzz transcribes and translates audio offline on your personal computer. Powered by OpenAIs Whisper. 项目地址: https://gitcode.com/GitHub_Trending/buz/buzz 你…...

3分钟上手Nebula Console:图数据库管理的终极命令行工具指南 [特殊字符]

3分钟上手Nebula Console&#xff1a;图数据库管理的终极命令行工具指南 &#x1f680; 【免费下载链接】nebula-console Command line interface for the Nebula Graph service 项目地址: https://gitcode.com/gh_mirrors/ne/nebula-console Nebula Console是NebulaGra…...

Windows服务器渗透日记:我是如何用MS17-010漏洞连穿三层内网的

Windows服务器渗透实战&#xff1a;从外网突破到内网横向移动的技术解析 那天下午&#xff0c;阳光透过百叶窗在键盘上投下斑驳的光影。我盯着屏幕上跳动的命令行界面&#xff0c;手指在键盘上快速敲击——这不是什么电影场景&#xff0c;而是一次真实的渗透测试任务。作为安全…...

移动端数据同步

移动端数据同步&#xff1a;数据无缝流转的奥秘 在移动互联网时代&#xff0c;智能手机已成为人们生活的核心工具。无论是工作文件、社交聊天记录&#xff0c;还是照片视频&#xff0c;数据分散在不同设备中&#xff0c;如何实现高效同步成为用户关注的焦点。移动端数据同步技…...

如何调试Dig依赖图:可视化工具和完整错误排查指南

如何调试Dig依赖图&#xff1a;可视化工具和完整错误排查指南 【免费下载链接】dig A reflection based dependency injection toolkit for Go. 项目地址: https://gitcode.com/gh_mirrors/di/dig Dig是Go语言中一款基于反射的依赖注入工具包&#xff0c;它能够帮助开发…...

Hybrid A*路径规划器:自动驾驶车辆运动规划的终极解决方案

Hybrid A*路径规划器&#xff1a;自动驾驶车辆运动规划的终极解决方案 【免费下载链接】path_planner Hybrid A* Path Planner for the KTH Research Concept Vehicle 项目地址: https://gitcode.com/gh_mirrors/pa/path_planner Hybrid A路径规划器是KTH Research Conc…...

3个实用技巧:掌握Chrome文本替换插件的终极指南

3个实用技巧&#xff1a;掌握Chrome文本替换插件的终极指南 【免费下载链接】chrome-extensions-searchReplace 项目地址: https://gitcode.com/gh_mirrors/ch/chrome-extensions-searchReplace 你是否曾在浏览网页时发现错别字却无法修改&#xff1f;是否需要对大量重…...

为什么选择顶级开源跨平台IPTV播放器:完整实战指南

为什么选择顶级开源跨平台IPTV播放器&#xff1a;完整实战指南 【免费下载链接】iptvnator :tv: Cross-platform IPTV player application with multiple features, such as support of m3u and m3u8 playlists, favorites, TV guide, TV archive/catchup and more. 项目地址…...

3步掌握APK Installer:如何在Windows上无缝运行安卓应用?

3步掌握APK Installer&#xff1a;如何在Windows上无缝运行安卓应用&#xff1f; 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 你是否厌倦了臃肿的安卓模拟器&#x…...

别再花钱买服务器了!用闲置云主机+frp 0.65.0搭建免费内网穿透服务(保姆级教程)

闲置云主机变身内网穿透神器&#xff1a;frp实战指南 手里有台快过期的学生优惠云服务器&#xff1f;别急着让它吃灰。今天我们就来聊聊如何把这类"鸡肋"资源变成实用的内网穿透工具。相比动辄几百元的商业穿透服务&#xff0c;用frp自建方案不仅零成本&#xff0c;还…...