使用注解實現(xiàn)Redis緩存功能
本文實例為大家分享了使用注解實現(xiàn)Redis緩存功能的具體代碼,供大家參考,具體內(nèi)容如下
非關(guān)系型內(nèi)存數(shù)據(jù)庫,有持久化操作,
c語言編寫的key,value存儲系統(tǒng)(區(qū)別于MySQL的二維表格的形式存儲。)
rdb:周期性的持久化
aof:以日志形式追加
默認rdb開啟,同時開啟使用aof
數(shù)據(jù)類型:string、list、set、zset、hash、
bitMaps 字節(jié)形式存儲、geospatial 經(jīng)緯度類型...
單線程:采用多路io復(fù)用實現(xiàn)高并發(fā)
使用:
添加依賴
<!-- 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>
創(chuàng)建配置類 固定寫法
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 { /** ?* 設(shè)置RedisTemplate規(guī)則 ?* @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); ? ? //解決查詢緩存轉(zhuǎn)換異常的問題 ? ? 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; } ? ? /** ? ? ?* 設(shè)置CacheManager緩存規(guī)則 ? ? ?* @param factory ? ? ?* @return ? ? ?*/ ? ? @Bean ? ? public CacheManager cacheManager(RedisConnectionFactory factory) { ? ? ? ? RedisSerializer<String> redisSerializer = new StringRedisSerializer(); ? ? ? ? Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ? ? ? ? //解決查詢緩存轉(zhuǎn)換異常的問題 ? ? ? ? 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分鐘 ---- 業(yè)務(wù)需求。 ? ? ? ? ? ? ? ? .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//設(shè)置key的序列化方式 ? ? ? ? ? ? ? ? .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //設(shè)置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 #最大阻塞等待時間(負數(shù)表示沒限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0
使用注解實現(xiàn)功能
緩存@Cacheable
根據(jù)方法對其返回結(jié)果進行緩存,下次請求時,如果緩存存在,則直接讀取緩存數(shù)據(jù)返 回;如果緩存不存在,則執(zhí)行方法,并把返回的結(jié)果存入緩存中。一般用在查詢方法 上。
緩存@CachePut
使用該注解標志的方法,每次都會執(zhí)行,并將結(jié)果存入指定的緩存中。其他方法可以直 接從響應(yīng)的緩存中讀取緩存數(shù)據(jù),而不需要再去查詢數(shù)據(jù)庫。一般用在新增方法上。
緩存@CacheEvict
使用該注解標志的方法,會清空指定的緩存。一般用在更新或者刪除方法上
在返回serviceimpl中標注注解 沒設(shè)置key時會自動加上參數(shù)作為key
@Cacheable(value = "dict", key = "'selectIndexList'+#id")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
阿里云服務(wù)器安裝配置redis的方法并且加入到開機啟動(推薦)
這篇文章主要介紹了阿里云服務(wù)器安裝配置redis并且加入到開機啟動,需要的朋友可以參考下2017-12-12淺談一下Redis的數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了淺談一下Redis的數(shù)據(jù)結(jié)構(gòu),簡單字符串結(jié)構(gòu)被用于存儲redis的key對象和String類型的value對象,其中的free和len字段可以輕松的使得在該字符串被修改時判斷是否需要擴容,需要的朋友可以參考下2023-08-08redis哈希類型_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了redis哈希類型的常用方法及原理淺析,感興趣的朋友一起看看吧2017-08-08Redis做數(shù)據(jù)持久化的解決方案及底層原理
Redis有兩種方式來實現(xiàn)數(shù)據(jù)的持久化,分別是RDB(Redis Database)和AOF(Append Only File),今天通過本文給大家聊一聊Redis做數(shù)據(jù)持久化的解決方案及底層原理,感興趣的朋友一起看看吧2021-07-07