【问题标题】:Using @Cacheable Spring annotation and manually add to Infinispan Cache使用@Cacheable Spring注解并手动添加到Infinispan Cache
【发布时间】:2019-11-26 17:23:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: spring-boot caching infinispan


    【解决方案1】:

    您可以使用 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);
    

    【讨论】:

      【解决方案2】:

      我能够通过将值存储为字符串键和对象值来解决这个问题——这与 Spring @Cacheable 注释非常有效。如果在缓存中找到对象,则 Spring 会将它们转换为返回类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 2013-11-25
        • 2021-08-22
        • 2018-07-03
        • 1970-01-01
        相关资源
        最近更新 更多