Springboot统一给redisKey加前缀

直接上源码:

@Slf4j
@Order
@Configuration
public class PreKeyRedisConfig {
    public static final String DEFUAL_PREFIX = "XXX##";
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    class PrefixRedisSerializer extends StringRedisSerializer {
        @Override
        public byte[] serialize(String s) {
            if (s == null) {
                return null;
            }
            // 这里加上你需要加上的key前缀
            String realKey = DEFUAL_PREFIX + s;
            return super.serialize(realKey);
        }
        @Override
        public String deserialize(byte[] bytes) {
            String s = bytes == null ? null : new String(bytes);
            int index = s.indexOf(DEFUAL_PREFIX);
            if (index != -1) {
                return s.substring(index + 2);
            }
            return s;
        }
    }

    @Bean
    public StringRedisSerializer stringRedisSerializer() {
        StringRedisSerializer serializer = new PrefixRedisSerializer();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.afterPropertiesSet();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.afterPropertiesSet();
        return serializer;
    }
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring Boot给Redis中的key加前缀可以通过配置文件来实现。以下是具体的步骤: 1. 在配置文件(application.properties或application.yml)中添加Redis配置信息。例如, ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 2. 创建一个Redis连接池配置类,并使用@Configuration注解进行标记。如下所示: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Bean public RedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); return new LettuceConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); return template; } } ``` 3. 在项目中使用@Autowired注解来注入RedisTemplate,并使用它来操作Redis数据库。例如,用于添加前缀的方法如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { private static final String PREFIX = "YOUR_PREFIX:"; @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(PREFIX + key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(PREFIX + key); } } ``` 在上面的例子中,通过在key前添加"YOUR_PREFIX:",实现了给Rediskey加前缀。您可以根据需要自定义前缀的内容。 需要注意的是,Redis中的key应该遵循某种命名规范以保持整体的清晰和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值