redis key的前缀统一设置

开发过程中经常会遇到设置redis key前缀的场景,尤其是有多条业务线,op都会给各条业务线分配特定的前缀,例如:baidu:baijiayun:xxxxx ,开发者每次拼接key的时候都要去拼接该前缀,很是麻烦,这里提供一种思路,java数据传输之前要通过,某种方式(各种序列化框架)序列化,此时可以在业务完成之后,在序列化的时候将前缀加进去

@Configuration
public class AlyRedisConfigration {

    @Value("${redis.commonPrefix}")
    private String prefix;

    @Bean
    @ConfigurationProperties(prefix = "redis")
    public SingleRedisConfig alyRedisConfiguration() {
        return new SingleRedisConfig();
    }

    @Bean("alyConnectionFactory")
    public LettuceConnectionFactory alyConnectionFactory(SingleRedisConfig alyRedisConfiguration) {
        return alyRedisConfiguration.getConnectionFactory();
    }

    @Bean
    public AlyRedisTemplate alyRedisTemplate(
        @Qualifier("alyConnectionFactory") LettuceConnectionFactory alyConnectionFactory) {
        AlyRedisTemplate al
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值