赞
踩
通过自定义redis key 序列话器
package com.example.demo.config; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * 项目redis 统一前缀 * * @author zhangqi * @date 2022/8/21 17:31 */ @Slf4j @Order @Configuration public class PrefixRedisSerializer extends StringRedisSerializer { /** * 演示这里就写死了 可以抽取到配置文件中 */ public static final String DEFUAL_PREFIX = "XXX##"; /** * 序列化 * * @param s key * @return 结果 */ @Override public byte[] serialize(String s) { if (s == null) { return new byte[0]; } // 这里加上你需要加上的key前缀 String realKey = DEFUAL_PREFIX + s; return super.serialize(realKey); } /** * 反序列化 * * @param bytes 数据 * @return 结果 */ @Override public String deserialize(byte[] bytes) { String s = bytes == null ? null : new String(bytes); if (StringUtils.isBlank(s)) { return s; } int index = s.indexOf(DEFUAL_PREFIX); if (index != -1) { return s.substring(DEFUAL_PREFIX.length()); } return s; } }
配置redis 序列话器
package com.example.demo.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.cache.annotation.CachingConfigurerSupport; 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.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import java.net.UnknownHostException; import java.util.Objects; /** * Redis配置 * * @author: Qz1997 * @date2020/5/14 0014 22:25 */ @SuppressWarnings("all") @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { /** * 设置redisTemplate 序列化器 * * @param redisConnectionFactory Redis链接工厂 * @return redisTemplate */ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); serializer.setObjectMapper(objectMapper); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.registerModule(new JavaTimeModule()); RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new PrefixRedisSerializer()); redisTemplate.setValueSerializer(serializer); redisTemplate.setHashKeySerializer(new PrefixRedisSerializer()); redisTemplate.setHashValueSerializer(serializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } /** * stringRedisTemplate 序列化器 * * @param redisConnectionFactory Redis链接工厂 * @return redisTemplate */ @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new PrefixRedisSerializer()); return template; } /** * 缓存管理器 * * @param redisTemplate redisTemplate * @return Redis缓存管理器 */ @Bean public RedisCacheManager redisCacheManager(RedisTemplate<?, ?> redisTemplate) { RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(Objects.requireNonNull(redisTemplate.getConnectionFactory())); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer())); return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration); } }
使用
@Resource private RedisTemplate<String, Object> redisTemplate; @Resource private StringRedisTemplate stringRedisTemplate; @GetMapping("testRedis") public String testRedis() { redisTemplate.opsForValue().set("test1", "test1"); Object o = redisTemplate.opsForValue().get("test1"); System.out.println("o = " + o); stringRedisTemplate.opsForValue().set("test2", "test2"); String test2 = stringRedisTemplate.opsForValue().get("test2"); System.out.println("test2 = " + test2); return "成功"; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。