Springboot Redis设置key前缀的方法步骤
短信预约 -IT技能 免费直播动态提醒
properties中配置
#redis
redis.masterClusterNodes=10.40.57.197:7000;10.40.57.198:7002;10.40.57.199:7004
redis.slaveClusterNodes=10.40.57.197:7001;10.40.57.198:7003;10.40.57.199:7005
redis.maxTotal=50
redis.maxIdle=10
redis.minIdle=1
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.testOnReturn=true
redis.timeout=10000
redis.lockExpireSeconds=5
redis.soTimeout=1000
redis.maxAttempts=3
redis.password=123456
redis.clientName=clientName
redis.keyPrefix=0000-->
读取配置文件内容:
@Component
@ConfigurationProperties(prefix = "redis")
@PropertySource("classpath:redis.properties")
public class RedisProperties {
private String masterClusterNodes;
private String slaveClusterNodes;
private int timeout;
private int soTimeout;
private int maxAttempts;
private String password;
private int maxTotal;
private int maxIdle;
private int minIdle;
private int maxWaitMillis;
private boolean testOnBorrow;
private boolean testOnReturn;
private String keyPrefix;
sets,gets
}
自定义StringSerializer
这个还是需要优化的
@Component
public class MyStringSerializer implements RedisSerializer<String> {
private final Logger logger = LoggerFactory.getLogger ( this.getClass () );
@Autowired
private RedisProperties redisProperties;
private final Charset charset;
public MyStringSerializer() {
this ( Charset.forName ( "UTF8" ) );
}
public MyStringSerializer(Charset charset) {
Assert.notNull ( charset, "Charset must not be null!" );
this.charset = charset;
}
@Override
public String deserialize(byte[] bytes) {
String keyPrefix = redisProperties.getKeyPrefix ();
String saveKey = new String ( bytes, charset );
int indexOf = saveKey.indexOf ( keyPrefix );
if (indexOf > 0) {
logger.info ( "key缺少前缀" );
} else {
saveKey = saveKey.substring ( indexOf );
}
logger.info ( "saveKey:{}",saveKey);
return (saveKey.getBytes () == null ? null : saveKey);
}
@Override
public byte[] serialize(String string) {
String keyPrefix = redisProperties.getKeyPrefix ();
String key = keyPrefix + string;
logger.info ( "key:{},getBytes:{}",key, key.getBytes ( charset ));
return (key == null ? null : key.getBytes ( charset ));
}
}
redisConfig 配置
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
private final Logger logger = LoggerFactory.getLogger ( this.getClass () );
@Autowired
private RedisProperties redisProperties;
@Autowired
private MyStringSerializer myStringSerializer;
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory ( redisClusterConfiguration (),
jedisPoolConfig () );
jedisConnectionFactory.setPassword ( redisProperties.getPassword () );
jedisConnectionFactory.setTimeout ( redisProperties.getTimeout () );
return jedisConnectionFactory;
}
@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
String[] ipPorts = redisProperties.getClusterNodes ().split ( ";" );
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration ( Arrays.asList ( ipPorts
) );
return redisClusterConfiguration;
}
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = BeanMapperUtil.map ( redisProperties,
JedisPoolConfig.class );
return jedisPoolConfig;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager redisCacheManager = new RedisCacheManager ( redisTemplate );
redisCacheManager.setDefaultExpiration ( 60 * 60 );
return redisCacheManager;
}
@Bean
public RedisTemplate<String, String> redisTemplate() {
StringRedisTemplate template = new StringRedisTemplate ( jedisConnectionFactory () );
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer ( Object.class );
ObjectMapper om = new ObjectMapper ();
om.setVisibility ( PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY );
om.enableDefaultTyping ( ObjectMapper.DefaultTyping.NON_FINAL );
jackson2JsonRedisSerializer.setObjectMapper ( om );
template.setKeySerializer ( myStringSerializer );
template.setHashKeySerializer ( myStringSerializer );
template.setValueSerializer ( jackson2JsonRedisSerializer );
template.afterPropertiesSet ();
return template;
}
}
到此这篇关于Springboot Redis设置key前缀的方法步骤的文章就介绍到这了,更多相关Springboot Redis key前缀内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341