spring-boot-cache整合redis

引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置

spring:cache:type: redisredis:host: localhostport: 6379username:password:database: 1

配置类

package com.qiangesoft.cache.config;import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;/*** 缓存配置** @author qiangesoft* @date 2024-05-15*/
@EnableCaching
@Configuration
public class RedisCacheConfig extends CachingConfigurerSupport {private static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();private static final GenericJackson2JsonRedisSerializer JACKSON__SERIALIZER = new GenericJackson2JsonRedisSerializer();@Beanpublic CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()// 三分钟有效期.entryTtl(Duration.ofMinutes(3)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(STRING_SERIALIZER)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(JACKSON__SERIALIZER));return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)).cacheDefaults(cacheConfiguration).build();}@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {// 配置redisTemplateRedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);// key序列化redisTemplate.setKeySerializer(STRING_SERIALIZER);// value序列化redisTemplate.setValueSerializer(JACKSON__SERIALIZER);// Hash key序列化redisTemplate.setHashKeySerializer(STRING_SERIALIZER);// Hash value序列化redisTemplate.setHashValueSerializer(JACKSON__SERIALIZER);redisTemplate.afterPropertiesSet();return redisTemplate;}}

服务类

package com.qiangesoft.cache.service;import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;/*** 用户服务类** @author qiangesoft* @date 2024-05-15*/
@Slf4j
@Service
public class UserInfoService {/*** 模拟数据库*/private final Map<Long, UserInfo> userInfoMap = new ConcurrentHashMap<>();private final String CACHE_KEY = "user";@CachePut(cacheNames = CACHE_KEY, key = "#userInfo.id")public UserInfo add(UserInfo userInfo) {log.info("UserInfoService.add() execute db !");userInfoMap.put(userInfo.getId(), userInfo);return userInfo;}@CacheEvict(cacheNames = CACHE_KEY, key = "#id")public void removeById(Long id) {log.info("UserInfoService.removeById() execute db !");userInfoMap.remove(id);}@CacheEvict(cacheNames = CACHE_KEY, allEntries = true)public void remove() {log.info("UserInfoService.remove() execute db !");userInfoMap.clear();}@CacheEvict(cacheNames = CACHE_KEY, key = "#userInfo.id")public void updateById(UserInfo userInfo) {log.info("UserInfoService.updateById() execute db !");userInfoMap.put(userInfo.getId(), userInfo);}@Cacheable(cacheNames = CACHE_KEY + ":list")public List<UserInfo> listAll() {log.info("UserInfoService.listAll() execute db !");return new ArrayList<>(userInfoMap.values());}@Cacheable(cacheNames = CACHE_KEY, key = "#id", unless = "#result == null")public UserInfo getById(Long id) {log.info("UserInfoService.getById() execute db !");UserInfo userInfo = userInfoMap.get(id);return userInfo;}}

测试

package com.qiangesoft.cache.controller;import com.qiangesoft.cache.service.UserInfo;
import com.qiangesoft.cache.service.UserInfoService;
import com.qiangesoft.cache.utils.CacheManagerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;/*** 用户测试** @author qiangesoft* @date 2024-05-15*/
@RequestMapping("/user")
@RestController
public class UserInfoController {@Autowiredprivate UserInfoService userInfoService;@Autowiredprivate CacheManagerUtil cacheManagerUtil;@PostMappingpublic void add(@RequestBody UserInfo userInfo) {userInfoService.add(userInfo);}@DeleteMapping("/{id}")public void removeById(@PathVariable Long id) {userInfoService.removeById(id);}@DeleteMapping("/all")public void remove() {userInfoService.remove();}@PutMappingpublic void updateById(@RequestBody UserInfo userInfo) {userInfoService.updateById(userInfo);}@GetMappingpublic List<UserInfo> listAll() {return userInfoService.listAll();}@GetMapping("/{id}")public UserInfo getById(@PathVariable Long id) {return userInfoService.getById(id);}@GetMapping("/current")public Object current(String id) {Object user = cacheManagerUtil.getCache("user", id);return user;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/12746.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python 机器学习 基础 之 监督学习/分类问题/回归任务/泛化、过拟合和欠拟合 基础概念说明

Python 机器学习 基础 之 监督学习/分类问题/回归任务/泛化、过拟合和欠拟合 基础概念说明 目录 Python 机器学习 基础 之 监督学习/分类问题/回归任务/泛化、过拟合和欠拟合 基础概念说明 一、简单介绍 二、监督学习 三、分类问题 四、回归任务 五、泛化、过拟合和欠拟合…

分治算法(Divide-and-Conquer Algorithm)

分治算法&#xff08;Divide-and-Conquer Algorithm&#xff09;是一种重要的计算机科学和数学领域的通用问题解决策略。其基本思想是将一个复杂的大规模问题分割成若干个规模较小、结构与原问题相似但相对简单的子问题来处理。这些子问题相互独立&#xff0c;分别求解后再通过…

Nginx进程结构

Nginx 是一个高性能的 Web 服务器和反向代理服务器&#xff0c;它使用多进程模型来优化并发处理和稳定性。Nginx 的进程结构主要由以下几部分组成&#xff1a; 主进程&#xff08;Master Process&#xff09; - 主进程的职责是管理其他所有进程&#xff08;工作进程和辅助进程…

全面提升数据采集效率:IP代理产品的应用与评估详解

全面提升数据采集效率&#xff1a;IP代理产品的应用与评估详解 文章目录 全面提升数据采集效率&#xff1a;IP代理产品的应用与评估详解背景应用场景&#xff1a;平台首页信息抓取准备评测素材详细的产品使用和评测流程产品介绍亮数据的IP代理服务亮数据的爬虫工具及采集技术 注…

uni-app 路由跳转方式

文章目录 navigator组件跳转API跳转1. uni.navigateTo&#xff08;打开新页面&#xff09;2. uni.redirectTo&#xff08;页面重定向&#xff09;3. uni.reLaunch&#xff08;重加载&#xff09;4. uni.switchTab&#xff08;Tab 切换&#xff09;5. uni.navigateBack&#xff…

2024网上可申请离婚,无需对方同意!

&#x1f383;很多客户决定离婚之后却因为不了解离婚流程没准备好所需材料&#xff0c;导致离婚失败&#xff0c;或者无故被对方e意拖延&#xff0c;无计可施&#xff0c;无可奈何&#xff01; &#x1f383;别怕&#xff0c;2024年离婚新规定已发布&#xff0c;离婚变的简单了…

OpenAI新模型GPT-4o“炸裂登场” 响应速度堪比真人 关键还免费!

GPT-4o模型基于来自互联网的大量数据进行训练&#xff0c;更擅长处理文本和音频&#xff0c;并且支持50种语言。更值得一提的是&#xff0c;GPT-4o最快可以在232毫秒的时间内响应音频输入&#xff0c;几乎达到了人类的响应水平。 GPT-4o有多“炸裂”&#xff1f;核心能力有三 G…

点量云流3D应用线上展厅云推流方案分享

展厅是企业对外展示宣传的窗口&#xff0c;不论企业还是政fu单位、博物馆、科技馆&#xff0c;展厅都可以给用户一个更直观的感受。而随着技术的发展&#xff0c;展厅展示的内容也从最初的图文、视频&#xff0c;扩展更多文件类型&#xff0c;比如PPT\PDF文件以及3D应用数字孪生…

sass详解与使用

Sass&#xff08;Syntactically Awesome Stylesheets&#xff09;是一个层叠样式表&#xff08;CSS&#xff09;的扩展语言&#xff0c;旨在帮助开发者更有效地编写和维护样式表。Sass最初由Hampton Catlin设计&#xff0c;并由Natalie Weizenbaum开发&#xff0c;后来通过Sass…

【文末附gpt升级方案】腾讯混元文生图大模型开源:中文原生Sora同款DiT架构引领新潮流

在人工智能与计算机视觉技术迅猛发展的今天&#xff0c;腾讯再次引领行业潮流&#xff0c;宣布其旗下的混元文生图大模型全面升级并对外开源。这次开源的模型不仅具备强大的文生图能力&#xff0c;更采用了业内首个中文原生的Sora同款DiT架构&#xff0c;为中文世界的视觉生成领…

uniapp使用地图开发app, renderjs使用方法及注意事项

上次提到uniapp开发地图app时得一些问题&#xff0c;最后提到使用renderjs实现app中使用任何地图&#xff08;下面将以腾讯地图为例&#xff0c;uniapp中写app时推荐使用得是高德地图&#xff0c;无法使用腾讯地图&#xff08;renderjs方式除外&#xff09;&#xff09;。 1、…

泰盈科技IPO终止:客户集中度高,业绩未达目标,高管薪酬较高

近日&#xff0c;上海证券交易所披露的信息显示&#xff0c;泰盈科技集团股份有限公司&#xff08;下称“泰盈科技”&#xff09;及其保荐人中金公司撤回上市申请文件。因此&#xff0c;上海证券交易所决定终止对该公司首次公开发行股票并在主板上市的审核。 据贝多财经了解&am…

企智汇项目管理软件有哪些优势?

一款非常好用、高效的软件——企智汇软件有哪些优势呢&#xff1f; 首先&#xff0c;我们来看看它的界面设计。企智汇软件界面简洁直观&#xff0c;用户可以轻松地使用各种功能&#xff0c;不需要学习复杂的操作流程。而且&#xff0c;软件还提供了多种配色方案和主题&#xf…

嵌入式学习72-复习(字符设备驱动框架)

编辑 drivers/char/Kconfig 为了在make menuconfig是能够显示出我们写的驱动程序 make menuconfig 编辑 drivers/char/Makefile 才是真正把编写好的源文件加入到编译中去 make modules cp drivers/char/first_driver.ko ~/nfs/rootfs/

Vue3的Options与Composition

OptionsAPI选项式配置项 Options类型的 API&#xff0c;数据、方法、计算属性等&#xff0c;是分落在&#xff1a;data、methods、computed中的&#xff0c;要是想新增或者修改一个需求&#xff0c;就必须需要分别修改&#xff1a;data、methods、computed&#xff0c;不易于维…

快手25届实习内推

快手25届实习内推 ①快手 【岗位】算法、工程、游戏&#xff0c;产品运营、市场、职能等 【一键内推】https://campus.kuaishou.cn/recruit/campus/e/h5/#/campus/jobs?codecampuswQrLOMvHE 【内推码】campuswQrLOMvHE

什么是ARP攻击,怎么做好主机安全,受到ARP攻击有哪些解决方案

在数字化日益深入的今天&#xff0c;网络安全问题愈发凸显其重要性。其中&#xff0c;ARP攻击作为一种常见的网络攻击方式之一&#xff0c;往往给企业和个人用户带来不小的困扰。ARP协议是TCP/IP协议族中的一个重要协议&#xff0c;负责把网络层(IP层)的IP地址解析为数据链路层…

Spring Boot集成activiti快速入门Demo

1.什么事activiti&#xff1f; Activiti是一个工作流引擎,可以将业务系统中复杂的业务流程抽取出来,使用专门的建模语言BPMN2.0进行定义,业务流程按照预先定义的流程进行执行,实现了系统的流程流activiti进行管理,减少业务系统由于流程变更进行系统升级改造的工作量,从而提高系…

做抖店的门槛高吗?一个月的时间能入门吗?基础问题解答如下

我是王路飞。 抖店&#xff0c;依旧是普通人做抖音最好的渠道&#xff0c;没有之一&#xff0c;依旧值得我们all in。 这是我对2024年抖音小店的看法和态度&#xff0c; 那么做抖店的门槛高吗&#xff1f;新手用一个月的时间能做到入门吗&#xff1f;投入和回报的数据是多少…

OpenAI 推出革命性新模型 GPT-4o:全能AI的新纪元

GPT-4o 模型的推出预示着人工智能领域的又一次飞跃&#xff0c;它将如何改变我们的世界&#xff1f; 在人工智能的快速发展浪潮中&#xff0c;OpenAI 再次站在了技术革新的前沿。2024年5月14日&#xff0c;OpenAI 宣布了其最新旗舰模型 GPT-4o&#xff0c;这不仅是一个简单的版…