简体   繁体   English

春天如何每30分钟更新一次缓存?

[英]How to update cache every 30 minutes in spring?

I have following declaration: 我有以下声明:

@Cacheable("books")
public Book findBook(ISBN isbn) {...}

But I want to update the cache every 30 minutes. 但是我想每30分钟更新一次缓存。 I understand that I can create @Scheduled job to invoke method annotated @CacheEvict("books") 我知道我可以创建@Scheduled作业来调用带注释的@CacheEvict("books")

Also, I suppose that in this case all books will be cleared but it is more desirable to update only stale data(which were put in cache > 30 minutes ago) 另外,我想在这种情况下,所有书籍都将被清除,但更可取的是仅更新陈旧的数据(已在30分钟前放入缓存)

Is there anything in spring that can facilitate implementation? 春天是否有什么可以促进实施?

Cache implementations provide a feature named expire after write or time to life for this task. 高速缓存实现为该任务提供了一个名为在写入后生存 期后到期的功能。 The different cache implementations have a lot variances. 不同的缓存实现有很多差异。 In Spring no effort was made to try to abstract or generalize the configuration part as well. 在Spring中,也没有进行任何尝试来抽象或概括配置部分。 Here is an example of programmatic configuration for your cache in Spring, if you like to use cache2k : 如果您想使用cache2k ,那么这是在Spring中对缓存进行编程配置的示例:

@Configuration
@EnableCaching
public class CachingConfig extends CachingConfigurerSupport  {

  @Bean
  public CacheManager cacheManager() {
    return new SpringCache2kCacheManager()
      .addCaches(
        b->b.name("books").keyType(ISBN.class).valueType(Book.class)
            .expireAfterWrite(30, TimeUnit.MINUTES)
            .entryCapacity(5000);
  }
}

More information about this is in cache2k User Guide - Spring Framework Support . 关于此的更多信息,请参见cache2k用户指南-Spring Framework支持 Other cache implementations like, EHCache or Caffeine support expiry as well, but the configuration is different. 其他缓存实现(例如EHCache或Caffeine)也支持过期,但是配置不同。

If you like to configure the cache expiry in a "vendor neutral" way, you can use a cache implementation that support the JCache/JSR107 standard. 如果您想以“供应商中立”的方式配置缓存过期,则可以使用支持JCache / JSR107标准的缓存实现。 The standard includes setting an expiry. 该标准包括设置有效期。 A way to do it, looks like this: 一种实现方式如下所示:

@Configuration
@EnableCaching
public class CacheConfiguration {

  @Bean
  public JCacheCacheManager cacheManager() {
    return new JCacheCacheManager() {
      @Override
      protected Collection<Cache> loadCaches() {
        Collection<Cache> caches = new ArrayList<>();
        caches.add(new JCacheCache(
          getCacheManager().createCache("books",
          new MutableConfiguration<ISBN,Book>()
            .setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 30)))),
          false));
        return caches;
      }
    };
  }
}

The in JCache is, that there are configuration options, that you need, which are not part of the standard. JCache中的是您需要的配置选项,它们不是标准的一部分。 One example is limiting the cache size. 一个示例是限制缓存大小。 For this, you always need to add a vendor specific configuration. 为此,您始终需要添加供应商特定的配置。 In case of cache2k (I am the author of cache2k), which supports JCache, the configurations are merged, which is described in detail at cache2k User Guide - JCache . 对于支持JCache的cache2k(我是cache2k的作者),将合并配置,这在cache2k用户指南-JCache中有详细描述。 This means on a programmatic level you do the "logic" part of the configuration, where as, the "operational" part like the cache size is configurable in an external configuration file. 这意味着在程序设计级别上,您需要进行配置的“逻辑”部分,而“操作”部分(例如缓存大小)可以在外部配置文件中进行配置。

Unfortunately, its not part of the standard how a vendor configuration and a programmatic configuration via the JCache API needs to interoperate. 不幸的是,通过JCache API进行供应商配置和编程配置如何互操作并不是标准的一部分。 So, even a 100% JCache compatible cache might refuse operation, and require that you only use one way of configuration. 因此,即使100%兼容JCache的缓存也可能拒绝操作,并要求您仅使用一种配置方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每隔 30 分钟执行一次 Spring cron 表达式 - Spring cron expression for every after 30 minutes 如何启用 logback 每 x 分钟/5 分钟/30 分钟轮换日志文件? - How to enable logback to rotate the log files every x minutes/5 minutes/30 minutes? 如何每30分钟自动执行一个vba宏? - how to execute a vba macro every 30 minutes automatically? 如何每30分钟在Java Web应用程序中调用一个类 - How to call a class in every 30 minutes in java web application 如何每隔X分钟清除一次应用缓存? - How do I clear app cache every X minutes? Java Robot:每30分钟重复一次 - Java Robot: Repeating every 30 minutes 每周有几天如何每20分钟从20.35到23.35运行一次工作? - How to run a job from 20.35 until 23.35 every 30 minutes, some days a week? Java - 如何在 for 循环中每 30 分钟下载文件、上传文件、删除文件 - Java - How to download file, upload file, delete file inside a for loop every 30 minutes 如何使用 Android 中的广播接收器每 2 分钟更新一次活动? - How to update activity every 2 minutes using broadcast receiver in Android? 在Android服务上每30分钟在线程中运行命令 - Run command every 30 minutes in thread on Android service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM