gpt4 book ai didi

java - RedisCacheManager 未更新 keyspace_misses

转载 作者:IT老高 更新时间:2023-10-28 13:53:09 28 4
gpt4 key购买 nike

我正在使用 spring-boot spring-data-redis 1.8.9.RELEASE RedisCacheManager CacheManager 用于缓存的实现。我想要了解的一个指标是缓存命中/未命中率。为此,我提取了 keyspace_hits and keyspace_misses通过 redis 服务器公开,也可以通过 redis_cli 使用 INFO STATS 查看。问题是 RedisCacheManager 从不注册缓存未命中,即即使存在缓存“未命中”,keyspace_misses 也永远不会增加。

调试代码,我看到spring-data-redis实际上在检索之前检查redis中的键EXISTS是否。我看到了这种方法的意义,但是当 EXISTS 对 redis 服务器执行时,它不会注册缓存未命中。

有没有办法使用 RedisCacheManager 并注册缓存未命中?我知道我可以使用其他 redis 对象来完成此操作,但我想知道是否可以使用标准 CacheManager 实现来完成?

编辑

理想的解决方案不会增加很多开销,我无法编辑 redis 服务器的配置。

RedisCacheManager 从缓存中检索元素时使用的代码。注意 Boolean 存在:

public RedisCacheElement get(final RedisCacheKey cacheKey) {
Assert.notNull(cacheKey, "CacheKey must not be null!");
Boolean exists = (Boolean)this.redisOperations.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(cacheKey.getKeyBytes());
}
});
return !exists ? null : new RedisCacheElement(cacheKey, this.fromStoreValue(this.lookup(cacheKey)));
}

以上代码将在缓存未命中时通过 MONITOR 在可查看的 redis 上执行这些命令。再次注意 EXISTS 是按照代码执行的:

Redis commands executed for a cache miss

执行上述命令后,即使发生缓存未命中,keyspace_misses 也不会递增:

enter image description here

最佳答案

问题中提到的代码是Spring提供的RedisCache的一部分。

  1. 扩展并创建 RedisCache 类的自定义实现,以覆盖“get”方法的行为以满足您的需要。
  2. 扩展 RedisCacheManager 以覆盖方法“createRedisCache”以使用您在第一步中创建的自定义 RedisCache 而不是默认缓存。

关于java - RedisCacheManager 未更新 keyspace_misses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50032435/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com