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

 

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