一对一视频源码,Redis数据源还能这样隔离
通过ThreadLocal识别出来压测数据,自定义Redis的主键的序列化方式,如果是压测数据则在主键后面加上后缀,这样就可以通过不同主键将Redis数据进行隔离。
一、实现key序列化
public class KeyStringRedisSerializer extends StringRedisSerializer { @Resource private WormholeIsolationConfiguration isolationConfiguration; public byte[] serialize(@Nullable String redisKey) { WormholeContext wormholeContext = WormholeContextHolder.getContext(); if (null != wormholeContext) { redisKey = isolationConfiguration.generateIsolationKey(redisKey); } return super.serialize(redisKey); } }
二、配置序列化器
/** * Redis 配置类 */ @Configuration @ConditionalOnClass({RedisTemplate.class, RedisOperations.class, RedisConnectionFactory.class}) public class WormholeRedisAutoConfiguration { @Bean public KeyStringRedisSerializer keyStringRedisSerializer() { return new KeyStringRedisSerializer(); } @Bean("redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate(); //使用fastjson序列化 FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); // value值的序列化采用fastJsonRedisSerializer template.setValueSerializer(fastJsonRedisSerializer); template.setHashValueSerializer(fastJsonRedisSerializer); // key的序列化采用StringRedisSerializer template.setKeySerializer(keyStringRedisSerializer()); template.setHashKeySerializer(keyStringRedisSerializer()); template.setConnectionFactory(factory); return template; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setKeySerializer(keyStringRedisSerializer()); template.setHashKeySerializer(keyStringRedisSerializer()); template.setConnectionFactory(factory); return template; } }
以上就是一对一视频源码,Redis数据源还能这样隔离, 更多内容欢迎关注之后的文章
标签:数据源,Redis,public,源码,template,序列化,class,keyStringRedisSerializer From: https://www.cnblogs.com/yunbaomengnan/p/18199015