我正在尝试使用新的 Lettuce 6.0
ClientSideCaching
功能。但我不确定如何配置它。基本上它需要StatefulRedisConnection
,我不知道如何得到它。这是我到目前为止的配置。
@Configuration
public class RedisConfiguration {
private final RedisConnectionFactory connectionFactory;
private final ObjectMapper objectMapper;
private final Map<String, SiteConfiguration> localCache = new ConcurrentHashMap<>();
@Autowired
public RedisConfiguration(RedisConnectionFactory connectionFactory, ObjectMapper objectMapper) {
this.connectionFactory = connectionFactory;
this.objectMapper = objectMapper;
}
@Bean
RedisOperations<String, Record> redisOperations() {
Jackson2JsonRedisSerializer<Record> serializer = new Jackson2JsonRedisSerializer<>(Record.class);
serializer.setObjectMapper(objectMapper);
var redisTemplate = new RedisTemplate<String, Recordn>();
redisTemplate.setDefaultSerializer(serializer);
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
@Bean
//----------------------------------------------------|
// This is not getting resolved v
CacheFrontend<String, Record> cacheFrontend(StatefulRedisConnection<String, Record> statefulRedisConnection) {
// Even this is not working connectionFactory.getConnection().getNativeConnection();
return ClientSideCaching.enable(CacheAccessor.forMap(localCache), statefulRedisConnection, TrackingArgs.Builder.enabled().noloop());
}
}
所以,想知道配置此功能的最佳方法是什么。还有什么方法可以链接这个 Caffeine 或其他东西来代替 ConcurrentHashMap ,这样客户端就不会存储太多数据。
老实说,当前的解决方案还有一些问题需要解决。
关于第 3 方缓存库的主题 - 您可以使用它们,只要它们可以遵循 CacheAccessor 的接口(您在上面使用的由 Map 支持的库,但您可以创建自己的实现,由其他东西支持) ).
对于访问 StatefulRedisConnection,它实际上是 RedisAsyncCommandsImpl 中的一个字段(connectionFactory.getConnection().getNativeConnection() 会生成 RedisAsyncCommandsImpl 的实例)。所以你需要以下内容:
@Bean
CacheFrontend<String, String> cacheFrontend(RedisConnectionFactory factory) {
RedisAsyncCommandsImpl<String,String> commands =
(RedisAsyncCommandsImpl<String, String>) factory.getConnection().getNativeConnection();
StatefulRedisConnection<String, String> connection =
(StatefulRedisConnection<String, String>) commands.getConnection();
return ClientSideCaching.enable(CacheAccessor.forMap(localCache),
connection, TrackingArgs.Builder.enabled().noloop());
}
但这绝对不是最漂亮的代码。