Springboot Redis設(shè)置key前綴的方法步驟
#redisredis.masterClusterNodes=10.40.57.197:7000;10.40.57.198:7002;10.40.57.199:7004redis.slaveClusterNodes=10.40.57.197:7001;10.40.57.198:7003;10.40.57.199:7005redis.maxTotal=50redis.maxIdle=10redis.minIdle=1redis.maxWaitMillis=1000redis.testOnBorrow=trueredis.testOnReturn=trueredis.timeout=10000redis.lockExpireSeconds=5redis.soTimeout=1000redis.maxAttempts=3redis.password=123456redis.clientName=clientName redis.keyPrefix=0000-->讀取配置文件內(nèi)容:
@Component@ConfigurationProperties(prefix = 'redis')@PropertySource('classpath:redis.properties')public class RedisProperties { /** * master 節(jié)點(diǎn)數(shù)據(jù) */ private String masterClusterNodes; /** * slave 節(jié)點(diǎn)數(shù)據(jù) */ private String slaveClusterNodes; /** * 連接超時(shí)時(shí)間 */ private int timeout; /** * 獲取數(shù)據(jù)超時(shí)時(shí)間 */ private int soTimeout; /** * 出現(xiàn)異常最大重試次數(shù) */ private int maxAttempts; /** * 連接時(shí)使用的密碼 */ private String password; private int maxTotal; private int maxIdle; private int minIdle; private int maxWaitMillis; private boolean testOnBorrow; private boolean testOnReturn; /** * key前綴 */ private String keyPrefix; sets,gets }自定義StringSerializer
這個(gè)還是需要優(yōu)化的
@Componentpublic 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@EnableCachingpublic 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; } /** * 配置cacheManage * 設(shè)置超時(shí)時(shí)間 1小時(shí) * * @param redisTemplate * @return */ @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; }}
到此這篇關(guān)于Springboot Redis設(shè)置key前綴的方法步驟的文章就介紹到這了,更多相關(guān)Springboot Redis key前綴內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式2. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))3. html中的form不提交(排除)某些input 原創(chuàng)4. 利用CSS制作3D動(dòng)畫(huà)5. XML解析錯(cuò)誤:未組織好 的解決辦法6. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單7. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器8. jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法
