zoukankan      html  css  js  c++  java
  • SpringDataRedis的Keyspaces设置

    前言

    原文:https://docs.spring.io/spring-data/redis/docs/2.3.2.RELEASE/reference/html/#redis.repositories.keyspaces

    翻译章节:13.4. Keyspaces

    正文

    Keyspaces 定义用于为 Redis Hash 创建实际Key的前缀。默认情况下,前缀设置为 getClass().getName()。 您可以通过在聚合根(aggregate root)级别上设置@RedisHash或设置程序配置来更改此默认设置。但是,带注解的keyspace将取代任何其他配置

    下面的示例显示如何使用@EnableRedisRepositories注解设置keyspace配置:

    Example:通过@EnableRedisRepositories设置Keyspace

    @Configuration
    @EnableRedisRepositories(keyspaceConfiguration = MyKeyspaceConfiguration.class)
    public class ApplicationConfig {
    
      //... RedisConnectionFactory and RedisTemplate Bean definitions omitted
    
      public static class MyKeyspaceConfiguration extends KeyspaceConfiguration {
    
        @Override
        protected Iterable<KeyspaceSettings> initialConfiguration() {
          return Collections.singleton(new KeyspaceSettings(Person.class, "people"));
        }
      }
    }
    

    下面的示例演示如何以编程方式设置键空间:

    Example:程序化Keyspace设置

    @Configuration
    @EnableRedisRepositories
    public class ApplicationConfig {
    
      //... RedisConnectionFactory and RedisTemplate Bean definitions omitted
    
      @Bean
      public RedisMappingContext keyValueMappingContext() {
        return new RedisMappingContext(
          new MappingConfiguration(new IndexConfiguration(), new MyKeyspaceConfiguration()));
      }
    
      public static class MyKeyspaceConfiguration extends KeyspaceConfiguration {
    
        @Override
        protected Iterable<KeyspaceSettings> initialConfiguration() {
          return Collections.singleton(new KeyspaceSettings(Person.class, "people"));
        }
      }
    }
    

    一些问题

    当实体类使用 @RedisHash(value=“键前缀”)的时候,配置的键空间(Keyspace)为什么没有生效。

    注解的keyspace将取代任何其他配置

    这句话是关键的一句,当你使用注解,则注解声明的优先级最高,而配置的键空间相当于是给一个默认值处理;解决方法是直接使用 @RedisHash 注解就可以了。

    复制请注明出处,在世界中挣扎的灰太狼
  • 相关阅读:
    【机器学习】浅谈协方差
    python {}.format
    【机器学习】准确率、精确率、召回率
    【声纹识别】 EER
    【机器学习】 最形象的入门
    逻辑卷-LVM
    RAID及软RAID的实现
    输入数字or 字符串,统计重复次数---字典统计练习
    Python-数据结构之dict(字典*****)
    POJ 3204 网络流的必须边
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/13516066.html
Copyright © 2011-2022 走看看