我正在使用spring-boot spring-data-redis 1.8.9.RELEASE的RedisCacheManager实现缓存,我想要知道一个指标:缓存命中/未命中比率。为了得到它,我提取了通过redis服务器暴露的keyspace_hits和keyspace_misses,也可以通过redis_cli使用
调试代码后,我发现spring-data-redis在检索数据之前实际上会检查redis中是否存在该键
是否有任何方法可以使用RedisCacheManager并注册缓存未命中?我知道可以使用其他redis对象来完成此操作,但我想知道是否可以使用标准的CacheManager实现来完成。 编辑 理想的解决方案不会增加很大的开销,而且我无法编辑redis服务器的配置。
RedisCacheManager从缓存中检索元素时使用的代码。请注意
INFO STATS
查看。问题在于RedisCacheManager从不注册缓存未命中,即使有缓存“未命中”,keyspace_misses也不会增加。调试代码后,我发现spring-data-redis在检索数据之前实际上会检查redis中是否存在该键
EXISTS
。我理解这种方法的意义,但是当执行EXISTS
时,它并不会记录缓存未命中。是否有任何方法可以使用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)));
}
上述代码将在redis上执行这些命令,可以通过MONITOR
在缓存未命中时查看。请注意,EXISTS
将按照代码执行: