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 注解就可以了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章