日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用注解實現Redis緩存功能_Redis

作者:遠走與夢游 ? 更新時間: 2022-09-21 編程語言

本文實例為大家分享了使用注解實現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

欄目分類
最近更新