首页 文章

RedisCacheManager不更新keyspace_misses

提问于
浏览
16

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

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

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

Edit

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

Code that RedisCacheManager uses when retrieving an element from cache. Notice Boolean exists

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)));
}

The above code will execute these commands on redis viewable via MONITOR on a cache miss. Notice again that EXISTS is executed as per the code:

Redis commands executed for a cache miss

After the above commands are executed, keyspace_misses is not incremented even though there was a cache miss:

enter image description here

1 回答

  • 1

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

    • 扩展并创建RedisCache类的自定义实现,以覆盖"get"方法的行为以满足您的需要 .

    • 扩展RedisCacheManager以覆盖方法"createRedisCache",以使用您在第一步中创建的自定义RedisCache而不是默认缓存 .

相关问题