当前位置:   article > 正文

Springboot Redis设置key 前缀_spring boot redis key前缀

spring boot redis key前缀

springboot 连接redis 并设置key前缀

properties中配置

  1. #redis
  2. redis.masterClusterNodes=10.40.57.197:7000;10.40.57.198:7002;10.40.57.199:7004
  3. redis.slaveClusterNodes=10.40.57.197:7001;10.40.57.198:7003;10.40.57.199:7005
  4. redis.maxTotal=50
  5. redis.maxIdle=10
  6. redis.minIdle=1
  7. redis.maxWaitMillis=1000
  8. redis.testOnBorrow=true
  9. redis.testOnReturn=true
  10. redis.timeout=10000
  11. redis.lockExpireSeconds=5
  12. redis.soTimeout=1000
  13. redis.maxAttempts=3
  14. redis.password=123456
  15. redis.clientName=clientName
  16. redis.keyPrefix=0000-->

读取配置文件内容:

  1. @Component
  2. @ConfigurationProperties(prefix = "redis")
  3. @PropertySource("classpath:redis.properties")
  4. public class RedisProperties {
  5. /**
  6. * master 节点数据
  7. */
  8. private String masterClusterNodes;
  9. /**
  10. * slave 节点数据
  11. */
  12. private String slaveClusterNodes;
  13. /**
  14. * 连接超时时间
  15. */
  16. private int timeout;
  17. /**
  18. * 获取数据超时时间
  19. */
  20. private int soTimeout;
  21. /**
  22. * 出现异常最大重试次数
  23. */
  24. private int maxAttempts;
  25. /**
  26. * 连接时使用的密码
  27. */
  28. private String password;
  29. private int maxTotal;
  30. private int maxIdle;
  31. private int minIdle;
  32. private int maxWaitMillis;
  33. private boolean testOnBorrow;
  34. private boolean testOnReturn;
  35. /**
  36. * key前缀
  37. */
  38. private String keyPrefix;
  39. sets,gets
  40. }

自定义StringSerializer

这个还是需要优化的

  1. @Component
  2. public class MyStringSerializer implements RedisSerializer<String> {
  3. private final Logger logger = LoggerFactory.getLogger ( this.getClass () );
  4. @Autowired
  5. private RedisProperties redisProperties;
  6. private final Charset charset;
  7. public MyStringSerializer() {
  8. this ( Charset.forName ( "UTF8" ) );
  9. }
  10. public MyStringSerializer(Charset charset) {
  11. Assert.notNull ( charset, "Charset must not be null!" );
  12. this.charset = charset;
  13. }
  14. @Override
  15. public String deserialize(byte[] bytes) {
  16. String keyPrefix = redisProperties.getKeyPrefix ();
  17. String saveKey = new String ( bytes, charset );
  18. int indexOf = saveKey.indexOf ( keyPrefix );
  19. if (indexOf > 0) {
  20. logger.info ( "key缺少前缀" );
  21. } else {
  22. saveKey = saveKey.substring ( indexOf );
  23. }
  24. logger.info ( "saveKey:{}",saveKey);
  25. return (saveKey.getBytes () == null ? null : saveKey);
  26. }
  27. @Override
  28. public byte[] serialize(String string) {
  29. String keyPrefix = redisProperties.getKeyPrefix ();
  30. String key = keyPrefix + string;
  31. logger.info ( "key:{},getBytes:{}",key, key.getBytes ( charset ));
  32. return (key == null ? null : key.getBytes ( charset ));
  33. }
  34. }

redisConfig 配置

  1. @Configuration
  2. @EnableCaching
  3. public class RedisConfig extends CachingConfigurerSupport {
  4. private final Logger logger = LoggerFactory.getLogger ( this.getClass () );
  5. @Autowired
  6. private RedisProperties redisProperties;
  7. @Autowired
  8. private MyStringSerializer myStringSerializer;
  9. @Bean
  10. public JedisConnectionFactory jedisConnectionFactory() {
  11. JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory ( redisClusterConfiguration (),
  12. jedisPoolConfig () );
  13. jedisConnectionFactory.setPassword ( redisProperties.getPassword () );
  14. jedisConnectionFactory.setTimeout ( redisProperties.getTimeout () );
  15. return jedisConnectionFactory;
  16. }
  17. @Bean
  18. public RedisClusterConfiguration redisClusterConfiguration() {
  19. String[] ipPorts = redisProperties.getClusterNodes ().split ( ";" );
  20. RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration ( Arrays.asList ( ipPorts
  21. ) );
  22. return redisClusterConfiguration;
  23. }
  24. @Bean
  25. public JedisPoolConfig jedisPoolConfig() {
  26. JedisPoolConfig jedisPoolConfig = BeanMapperUtil.map ( redisProperties,
  27. JedisPoolConfig.class );
  28. return jedisPoolConfig;
  29. }
  30. /**
  31. * 配置cacheManage
  32. * 设置超时时间 1小时
  33. *
  34. * @param redisTemplate
  35. * @return
  36. */
  37. @Bean
  38. public CacheManager cacheManager(RedisTemplate redisTemplate) {
  39. RedisCacheManager redisCacheManager = new RedisCacheManager ( redisTemplate );
  40. redisCacheManager.setDefaultExpiration ( 60 * 60 );
  41. return redisCacheManager;
  42. }
  43. @Bean
  44. public RedisTemplate<String, String> redisTemplate() {
  45. StringRedisTemplate template = new StringRedisTemplate ( jedisConnectionFactory () );
  46. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer ( Object.class );
  47. ObjectMapper om = new ObjectMapper ();
  48. om.setVisibility ( PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY );
  49. om.enableDefaultTyping ( ObjectMapper.DefaultTyping.NON_FINAL );
  50. jackson2JsonRedisSerializer.setObjectMapper ( om );
  51. template.setKeySerializer ( myStringSerializer );
  52. template.setHashKeySerializer ( myStringSerializer );
  53. template.setValueSerializer ( jackson2JsonRedisSerializer );
  54. template.afterPropertiesSet ();
  55. return template;
  56. }
  57. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/389673
推荐阅读
相关标签
  

闽ICP备14008679号