Spring定时缓存

huangapple go评论103阅读模式
英文:

Spring Scheduled Cache

问题

我有一个在我的Spring Boot项目中返回列表的方法。这个列表是通过DTO映射创建的,我不希望我的代码在每次页面刷新时执行SQL查询。有时候数据库可能会有新的更新。所以,例如,我希望它每隔5小时刷新一次缓存。@Cacheable正在运行,但不接收新的数据。我查看了@CacheEvict的示例,但我应用它时我的代码并不会创建任何缓存。还有一件事,是否可以在同一个方法中缓存多个列表?例如,就像下面的代码一样,但它不起作用:

  1. @Override
  2. @Cacheable(value = "apps")
  3. @Scheduled(cron = "0 0/30 * * * ?")
  4. @CacheEvict(value = "apps", allEntries=true)
  5. public List<Apps> executeLatestApplications(){
  6. ...
  7. List<Apps> apps = em.createNamedQuery("Apps", Apps.class).getResultList();
  8. ...
  9. }

这是应用于apps SQL 结果映射的部分:

  1. @NamedNativeQuery(
  2. name = "AppsQuery",
  3. query = "SELECT id, name, address, repo, version FROM apps",
  4. resultSetMapping = "Mapping"
  5. )
  6. @SqlResultSetMapping(
  7. name = "Mapping",
  8. classes = @ConstructorResult(targetClass= com.meursault.apps.model.Apps.class,
  9. columns = {
  10. @ColumnResult(name = "id", type = Long.class),
  11. @ColumnResult(name = "name", type = String.class),
  12. @ColumnResult(name = "address", type = String.class),
  13. @ColumnResult(name = "repo", type = String.class),
  14. @ColumnResult(name = "version", type = String.class)
  15. }
  16. )

如果你需要进一步的帮助,请告诉我。

英文:

I have a method which returns list in my spring-boot project. This list is created with DTO mapping and i don't want my code execute sql query in every refresh of page. Sometimes new updates can come to the database. So for example I want it to refresh cache every 5 hours. Cacheable is running but not receiving new data. I looked at the Cache Evict examples, but my code doesn't make any cache when I apply it. And one more thing, is it possible to cache more than one list in same method? For example like in the code below but it does not work:

  1. @Override
  2. @Cacheable(value = &quot;apps&quot;)
  3. @Scheduled(cron = &quot;0 0/30 * * * ?&quot;)
  4. @CacheEvict(value = &quot;apps&quot;, allEntries=true)
  5. public List&lt;Apps&gt; executeLatestApplications(){
  6. ...
  7. List&lt;Apps&gt; apps = em.createNamedQuery(&quot;Apps&quot;, Apps.class).getResultList();
  8. ...
  9. }

This is where apps SQL Result Mapping:

  1. @NamedNativeQuery(
  2. name = &quot;AppsQuery&quot;,
  3. query = &quot;SELECT id, name, address, repo, version FROM apps&quot;,
  4. resultSetMapping = &quot;Mapping&quot;
  5. )
  6. @SqlResultSetMapping(
  7. name = &quot;Mapping&quot;,
  8. classes = @ConstructorResult(targetClass= com.meursault.apps.model.Apps.class,
  9. columns = {
  10. @ColumnResult(name = &quot;id&quot;, type = Long.class),
  11. @ColumnResult(name = &quot;name&quot;, type = String.class),
  12. @ColumnResult(name = &quot;address&quot;, type = String.class),
  13. @ColumnResult(name = &quot;repo&quot;, type = String.class),
  14. @ColumnResult(name = &quot;version&quot;, type = String.class))

答案1

得分: 1

"@Cacheable正在运行,但不接收新数据。我查看了缓存清除的示例,但我应用它时我的代码不生成任何缓存。"

"@Cacheable通过将返回类型与方法参数进行缓存,可以将其视为具有方法参数作为键和返回值作为返回值的简单键值存储。"

"您可以使用单独的@CacheEvict方法来清除所有条目。"

  1. @CacheEvict(allEntries = true)
  2. public void save(App app) {
  3. em.insert(app);
  4. }

"查看这个帖子"

"您可以在Spring文档中查找更多信息。"

英文:

I'm not sure what you mean by 'Cacheable is running but not receiving new data. I looked at the Cache Evict examples, but my code doesn't make any cache when I apply it.

'@Cacheable works by caching return type against method argument, think of this as simple key/value store with key as method argument and value as the returned value.

You can have a separate @CacheEvict method which evicts allentries.

  1. @CacheEvict(allEntries = true)
  2. public void save(App app) {
  3. em.insert(app);
  4. }

Check this post

You can check more information in spring documentation.

答案2

得分: 1

I guess the problem is you are using @Cacheable(value = "apps") and @CacheEvict(value = "apps", allEntries=true) in the same method.

Use @CachePut to update all the entries.

  1. @Override
  2. @CachePut(value = "apps")
  3. @Scheduled(cron = "0 0/30 * * * ?")
  4. public List<Apps> executeLatestApplications(){
  5. ...
  6. List<Apps> apps = em.createNamedQuery("Apps", Apps.class).getResultList();
  7. ...
  8. }
英文:

I guess the problem is you are using @Cacheable(value = &quot;apps&quot;) and @CacheEvict(value = &quot;apps&quot;, allEntries=true) in the same method.

Use @CachePut to update all the entries.

  1. @Override
  2. @CachePut(value = &quot;apps&quot;)
  3. @Scheduled(cron = &quot;0 0/30 * * * ?&quot;)
  4. public List&lt;Apps&gt; executeLatestApplications(){
  5. ...
  6. List&lt;Apps&gt; apps = em.createNamedQuery(&quot;Apps&quot;, Apps.class).getResultList();
  7. ...
  8. }

huangapple
  • 本文由 发表于 2020年7月28日 19:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63133037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定