1

我正在寻找将新缓存动态添加到已经初始化的CaffeineCacheManager实例。

该课程似乎没有明显的方法来做到这一点。我可以通过添加一个新的缓存名称,CaffeineCacheManager.getCacheNames().add()但我不确定那是正确的方法。

我之所以这么想,是因为该方法的文档setCacheNames()基本上指出,在您更改了一次集合后,您将无法添加任何新的缓存。继承人的实际文档:

为此 CacheManager 的“静态”模式指定一组缓存名称。调用此方法后,缓存的数量及其名称将被固定,在运行时不会创建更多的缓存区域。使用 null 集合参数调用它会将模式重置为“动态”,从而允许再次进一步创建缓存

最后是registerCustomCache()可以使用的方法。但是这种方法的文档确实令人困惑。

使用此缓存管理器注册给定的本地 Caffeine Cache 实例,使其适应 Spring 的缓存 API 以通过 getCache(java.lang.String) 公开。可以并排注册任意数量的此类自定义缓存。这允许每个缓存的自定义设置(与所有缓存共享缓存管理器配置中的公共设置相反)并且通常与 Caffeine 构建器 API 一起使用:registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10)。 build()) 请注意,任何其他缓存,无论是通过 setCacheNames(java.util.Collection<java.lang.String>) 静态指定还是按需动态构建,仍然使用缓存管理器配置中的通用设置进行操作

最后一部分特别令人困惑:

Note that any other caches, whether statically specified through setCacheNames(java.util.Collection<java.lang.String>) or dynamically built on demand, still operate with the common settings in the cache manager's configuration.

当我registerCustomCache()用来添加缓存时,我基本上是在动态构建缓存并使用这种方法allows for custom settings per cache,但是文档下面的几行突然说它们都是still operate with the common settings in the cache manager's configuration

所以我现在有点困惑。请告诉我应该使用哪种方法将新缓存动态添加到缓存管理器。

谢谢

4

0 回答 0