網站首頁 編程語言 正文
本文實例為大家分享了使用注解實現Redis緩存功能的具體代碼,供大家參考,具體內容如下
非關系型內存數據庫,有持久化操作,
c語言編寫的key,value存儲系統(區別于MySQL的二維表格的形式存儲。)
rdb:周期性的持久化
aof:以日志形式追加
默認rdb開啟,同時開啟使用aof
數據類型:string、list、set、zset、hash、
bitMaps 字節形式存儲、geospatial 經緯度類型...
單線程:采用多路io復用實現高并發
使用:
添加依賴
<!-- redis --> <dependency> ?<groupId>org.springframework.boot</groupId> ?<artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X集成redis所需common-pool2--> <dependency> ?<groupId>org.apache.commons</groupId> ?<artifactId>commons-pool2</artifactId> ?<version>2.6.0</version> </dependency>
創建配置類 固定寫法
package com.lzq.yygh.common; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; 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.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; ? import java.net.UnknownHostException; import java.time.Duration; @Configuration @EnableCaching ?//開啟緩存功能 public class RedisConfig { /** ?* 設置RedisTemplate規則 ?* @param redisConnectionFactory ?* @return ?*/ @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException { ? ? RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); ? ? redisTemplate.setConnectionFactory(redisConnectionFactory); ? ? Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ? ? //解決查詢緩存轉換異常的問題 ? ? ObjectMapper om = new ObjectMapper(); ? ? // 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public ? ? ?om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); ? ? // 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如String,Integer等 ? ? ? ? ? ? ? ? om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); ? ? ?jackson2JsonRedisSerializer.setObjectMapper(om); ? ? //序列號key value ? ? ?redisTemplate.setKeySerializer(new StringRedisSerializer()); ? ? ?redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); ? ? ?redisTemplate.setHashKeySerializer(new StringRedisSerializer()); ? ? ?redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); ? ? ?redisTemplate.afterPropertiesSet(); ? ? ?return redisTemplate; } ? ? /** ? ? ?* 設置CacheManager緩存規則 ? ? ?* @param factory ? ? ?* @return ? ? ?*/ ? ? @Bean ? ? public CacheManager cacheManager(RedisConnectionFactory factory) { ? ? ? ? RedisSerializer<String> redisSerializer = new StringRedisSerializer(); ? ? ? ? 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); ? ? ? ? // 配置序列化(解決亂碼的問題),過期時間600秒 ? ? ? ? RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() ? ? ? ? ? ? ? ? .entryTtl(Duration.ofSeconds(600)) //緩存過期10分鐘 ---- 業務需求。 ? ? ? ? ? ? ? ? .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//設置key的序列化方式 ? ? ? ? ? ? ? ? .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //設置value的序列化 ? ? ? ? ? ? ? ? .disableCachingNullValues(); ? ? ? ? RedisCacheManager cacheManager = RedisCacheManager.builder(factory) ? ? ? ? ? ? ? ? .cacheDefaults(config) ? ? ? ? ? ? ? ? .build(); ? ? ? ? return cacheManager; ? ? } }
添加配置信息
spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database= 0 spring.redis.timeout=1800000 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待時間(負數表示沒限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0
使用注解實現功能
緩存@Cacheable
根據方法對其返回結果進行緩存,下次請求時,如果緩存存在,則直接讀取緩存數據返 回;如果緩存不存在,則執行方法,并把返回的結果存入緩存中。一般用在查詢方法 上。
緩存@CachePut
使用該注解標志的方法,每次都會執行,并將結果存入指定的緩存中。其他方法可以直 接從響應的緩存中讀取緩存數據,而不需要再去查詢數據庫。一般用在新增方法上。
緩存@CacheEvict
使用該注解標志的方法,會清空指定的緩存。一般用在更新或者刪除方法上
在返回serviceimpl中標注注解 ?沒設置key時會自動加上參數作為key
@Cacheable(value = "dict", key = "'selectIndexList'+#id")
原文鏈接:https://blog.csdn.net/weixin_52210557/article/details/122645131
相關推薦
- 2022-11-17 C++11中異常處理機制詳解_C 語言
- 2023-02-25 Golang合并yaml文件過程逐步講解_Golang
- 2023-02-25 React中常用的Hook有哪些_React
- 2023-02-02 C++調用matlab引擎實現三維圖的繪制_C 語言
- 2022-12-21 Kotlin掛起函數應用介紹_Android
- 2022-06-14 Python?torch.fft.rfft()函數用法示例代碼_python
- 2022-10-21 一文帶你入門Go語言中定時任務庫Cron的使用_Golang
- 2022-06-18 C語言圖文并茂詳解鏈接過程_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支