網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
redis 配置類(lèi)
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
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.StringRedisSerializer;
@SpringBootConfiguration
public class RedisConfig extends CachingConfigurerSupport {
? ? /**
? ? ?* 注入 RedisConnectionFactory
? ? ?*/
? ? @Autowired
? ? private RedisConnectionFactory redisConnectionFactory;
? ? @Bean
? ? public CacheManager cacheManager(RedisConnectionFactory factory) {
? ? ? ? return RedisCacheManager.builder(factory).build();
? ? }
? ? @Bean
? ? public RedisTemplate<String, Object> functionDomainRedisTemplate() {
? ? ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
? ? ? ? redisTemplate.setConnectionFactory(redisConnectionFactory);
? ? ? ? // 使用Jackson2JsonRedisSerialize 替換默認(rèn)序列化
? ? ? ? Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
? ? ? ? ObjectMapper objectMapper = new ObjectMapper();
? ? ? ? objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
? ? ? ? objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
? ? ? ? jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
? ? ? ? // 設(shè)置value的序列化規(guī)則和 key的序列化規(guī)則
? ? ? ? StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
? ? ? ? redisTemplate.setKeySerializer(stringRedisSerializer);
? ? ? ? redisTemplate.setHashKeySerializer(stringRedisSerializer);
? ? ? ? redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
? ? ? ? redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
? ? ? ? redisTemplate.afterPropertiesSet();
? ? ? ? return redisTemplate;
? ? }
}
當(dāng)使用opsForValue() 存取String類(lèi)型key,value情形
@Autowired
private StringRedisTemplate redisTemplate;
當(dāng)使用opsForValue() 存取String類(lèi)型key,自定義對(duì)象value情形
@Autowired
private RedisTemplate<String, Object> redisTemplate;
當(dāng)使用hash結(jié)構(gòu)時(shí)
@Autowired
private RedisTemplate<String, Object> redisTemplate;
BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps("key1");
ops.put("key2",obj);
原文鏈接:https://blog.csdn.net/weixin_51681634/article/details/125078072
相關(guān)推薦
- 2022-11-05 關(guān)于Linux下動(dòng)態(tài)查看實(shí)時(shí)日志的命令_linux shell
- 2022-04-14 Python中創(chuàng)建表格詳細(xì)過(guò)程_python
- 2022-11-22 Redis?BloomFilter布隆過(guò)濾器原理與實(shí)現(xiàn)_Golang
- 2024-07-15 SpringBoot使用EasyExcel導(dǎo)出Excel(含設(shè)置下拉框、表頭凍結(jié))
- 2022-12-31 一文初探Go語(yǔ)言中的reflect反射包_Golang
- 2022-07-18 BeanUtils工具類(lèi)
- 2022-06-30 React-hooks中的useEffect使用步驟_React
- 2022-10-06 QT獲取顯示當(dāng)前時(shí)間和日期的方法(用QTime,QDate和QDateTime)_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支