小编siw*_*wel的帖子

两级缓存(Redis + Caffeine)

在对应用程序进行分析时,我们发现 Redis 正在影响执行时间,因为线程中有很多睡眠。我需要实现两级缓存或考虑解决这个问题。

我想要两级缓存

  • L1 - 每个部署实例的本地,
  • L2 - 同一部署的所有实例的全局缓存,

我想出的解决方案是

  • 创建两个CacheManager(CaffeineCacheManager和RedisCacheManager),
  • 为每个缓存管理器初始化相同的缓存,
  • 使用注解@Caching和cacheable={}来使用两个缓存,

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

Run Code Online (Sandbox Code Playgroud)

类的结构类似于:CachedService(此处注释)-> NonCachedService

我面临的问题

我想让它正常工作(是的 - 有效/n - 不工作):

  • [ y ] 数据被获取,然后缓存到 Redis 和本地缓存 - 这有效
  • [ y ] 如果数据存在于本地缓存中,则不要将其移动到 redis - 这有效
  • [ y ] 如果任何缓存包含数据,将从缓存中获取数据
  • [ n ] 如果数据存在于 Redis 中,则将其移动到本地 - 这不起作用

修改 @Caching …

java spring caching redis spring-boot

5
推荐指数
1
解决办法
5708
查看次数

标签 统计

caching ×1

java ×1

redis ×1

spring ×1

spring-boot ×1