Keyspaces
密钥空间定义了用于创建 Redis 哈希实际密钥的前缀。默认情况下,前缀设置为 getClass().getName()
。可以通过在聚合根级别设置 @RedisHash
或通过设置程序化配置来更改此默认设置。但是,带注释的密钥空间优先于任何其他配置。
Keyspaces define prefixes used to create the actual key for the Redis Hash.
By default, the prefix is set to getClass().getName()
.
You can alter this default by setting @RedisHash
on the aggregate root level or by setting up a programmatic configuration.
However, the annotated keyspace supersedes any other configuration.
以下示例演示如何使用 @EnableRedisRepositories
注释设置密钥空间配置:
The following example shows how to set the keyspace configuration with the @EnableRedisRepositories
annotation:
Example 1. Keyspace Setup via
@EnableRedisRepositories
@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"));
}
}
}
以下示例演示如何通过编程设置密钥空间:
The following example shows how to programmatically set the keyspace:
Example 2. Programmatic Keyspace setup
@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"));
}
}
}