網站首頁 編程語言 正文
redis 配置類
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 替換默認序列化
? ? ? ? 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);
? ? ? ? // 設置value的序列化規則和 key的序列化規則
? ? ? ? StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
? ? ? ? redisTemplate.setKeySerializer(stringRedisSerializer);
? ? ? ? redisTemplate.setHashKeySerializer(stringRedisSerializer);
? ? ? ? redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
? ? ? ? redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
? ? ? ? redisTemplate.afterPropertiesSet();
? ? ? ? return redisTemplate;
? ? }
}
當使用opsForValue() 存取String類型key,value情形
@Autowired
private StringRedisTemplate redisTemplate;
當使用opsForValue() 存取String類型key,自定義對象value情形
@Autowired
private RedisTemplate<String, Object> redisTemplate;
當使用hash結構時
@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
相關推薦
- 2022-04-11 css左側 div給固定寬 右側div自適應
- 2022-06-01 詳解C語言的void*空指針_C 語言
- 2022-11-25 PostgreSQL自增主鍵用法及在mybatis中的使用教程_PostgreSQL
- 2022-05-14 一起來學習React元素的創建和渲染_React
- 2022-07-04 Python三數之和的實現方式_python
- 2022-04-10 Blazor路由與頁面導航開發介紹_基礎應用
- 2022-04-04 前端小技巧:ELement UI時間控件el-date-picker誤差8小時
- 2022-06-02 python獲取字符串中的email_python
- 最近更新
-
- 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同步修改后的遠程分支