跳到主要内容

Keyspaces(键空间)

DeepSeek V3 中英对照 Keyspaces

Keyspaces 定义了用于创建 Redis 哈希实际键的前缀。默认情况下,前缀设置为 getClass().getName()。你可以通过在聚合根级别设置 @RedisHash 或通过编程配置来更改此默认值。然而,注解的 keyspace 会覆盖任何其他配置。

以下示例展示了如何使用 @EnableRedisRepositories 注解来设置 keyspace 配置:

示例 1:通过 @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"));
}
}
}
java

以下示例展示了如何以编程方式设置键空间:

示例 2. 编程式 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"));
}
}
}
java