如何使用Spring Data Redis在Redis上格式化键模式?

时间:2018-06-14 20:19:04

标签: java spring-boot redis spring-data spring-data-redis

我想在将对象保存到Redis服务器时更改redis密钥格式。

例如,目前我正在获取这样的密钥:

"人数:8637ab41-b411-41d9-ABEC-e0995d90f406"

但我想要这样的事情:

" {RA} 7ofa1 1U"

有没有办法用弹簧数据redis更改键的格式?

   @Configuration
    @ComponentScan("com.repository.redis")
    @EnableRedisRepositories(basePackages = "com.repository.redis")
    public class RedisConfig {

        @Bean
        public RedisConnectionFactory 

jedisConnectionFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(5);
        poolConfig.setMinIdle(1);
        poolConfig.setMaxTotal(128);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTestWhileIdle(true);
        return new JedisConnectionFactory(poolConfig);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        template.setValueSerializer(new Jackson2JsonRedisSerializer<Student>(Student.class));
        template.setHashKeySerializer(new StringRedisSerializer());

        return template;
    }
}


@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Student")
public class Student implements Serializable {

    @Id
    private String id;
}

提前致谢。

0 个答案:

没有答案