RedisCacheManager不更新keyspace_misses

Alw*_*ing 16 java spring redis spring-data-redis spring-boot

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

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

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

编辑

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

RedisCacheManager从缓存中检索元素时使用的代码.通知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)));
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将通过MONITOR高速缓存未命中在redis 上执行这些命令.再次注意,EXISTS根据代码执行:

为缓存未命中执行Redis命令

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

在此输入图像描述

小智 1

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

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