使用@Cacheable Spring注解并手动添加到Infinispan Cache

Using @Cacheable Spring annotation and manually add to Infinispan Cache

我正在尝试在应用程序启动之前从冷启动加载我的缓存。这样做是为了让值在用户访问服务器时立即可用,而不必访问我的数据库。

来自Spring的@Cacheable 功能都很好,问题是我如何手动将对象存储在缓存中,以便在执行函数时可以读取它们。

Spring 以某种方式以字节为单位存储这些对象——我需要在手动加载缓存时模仿这一点。我只是想弄清楚他们如何在函数中处理 return 对象,以键值对的形式存储到缓存中。

您可以使用 Spring 的 CacheManager 以编程方式访问任何缓存。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/CacheManager.html

var cache = cacheManager.getCache("foo");

cache.put(key, value);

我能够通过将值存储为字符串键和对象值来解决这个问题——这与 Spring @Cacheable 注释配合得很好。如果在缓存中找到对象,则 Spring 将其转换为 return 类型。