在分析应用程序时,我们发现 Redis 正在影响执行时间,因为线程中有很多睡眠。我需要实现两级缓存或者考虑解决这个问题。
我想要两级缓存:
我想出的解决方案是:
@Caching(cacheable = {
@Cacheable(cacheNames = CacheConfiguration.HELLO_WORLD),
@Cacheable(cacheNames = CacheConfiguration.HELLO_WORLD, cacheManager = "cacheManagerRedis")
})
public String generate(String name)
{
log.info(" Cached method call...");
return helloWorldService.generate(name);
}
类的结构类似于: CachedService(此处注释)-> NonCachedService
我面临的问题:
我想让它正常工作(是的 - 有效/n - 不工作):
将 @Caching 注释修改为 put={} 将值放入本地缓存会使整个缓存无法工作。
@Caching(cacheable = {
@Cacheable(cacheNames = CacheConfiguration.HELLO_WORLD),
@Cacheable(cacheNames = CacheConfiguration.HELLO_WORLD, cacheManager = "cacheManagerRedis")
}, put = {
@CachePut(cacheNames = CacheConfiguration.HELLO_WORLD),
})
public String generate(String name)
{
log.info(" Cached method call...");
return helloWorldService.generate(name);
}
对于其他正在寻找此功能的人,我能够实现 Ankit 建议的 CacheInterceptor。
示例:
public class RedisMultiCacheInterceptor extends CacheInterceptor {
@Autowired
private CacheManager caffeineCacheManager;
@Override
protected Cache.ValueWrapper doGet(Cache cache, Object key) {
//Get item from cache
var superGetResult = super.doGet(cache, key);
if (superGetResult == null) {
return superGetResult;
}
//If retrieved from Redis, check if it's missing from caffeine on local and add it
if (cache.getClass() == RedisCache.class) {
var caffeineCache = caffeineCacheManager.getCache(cache.getName());
if (caffeineCache != null) {
caffeineCache.putIfAbsent(key, superGetResult.get());
}
}
return superGetResult;
}
}
我尝试使用上面的拦截器。它不起作用,相反,我收到了错误“调用 init 方法失败”;嵌套异常是 java.lang.IllegalStateException:需要 'cacheOperationSources' 属性:如果没有可缓存的方法,则不要使用缓存方面。你能帮忙吗,小姐?如您所示创建类后,我是否需要在任何地方设置拦截器。