Spring定时缓存

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

Spring Scheduled Cache

问题

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

@Override
@Cacheable(value = "apps")
@Scheduled(cron = "0 0/30 * * * ?")
@CacheEvict(value = "apps", allEntries=true)
public List<Apps> executeLatestApplications(){
...
List<Apps> apps = em.createNamedQuery("Apps", Apps.class).getResultList();
...
}

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

@NamedNativeQuery(
    name = "AppsQuery",
    query = "SELECT id, name, address, repo, version FROM apps",
    resultSetMapping = "Mapping"
)

@SqlResultSetMapping(
    name = "Mapping",
    classes = @ConstructorResult(targetClass= com.meursault.apps.model.Apps.class,
            columns = {
                    @ColumnResult(name = "id", type = Long.class),
                    @ColumnResult(name = "name", type = String.class),
                    @ColumnResult(name = "address", type = String.class),
                    @ColumnResult(name = "repo", type = String.class),
                    @ColumnResult(name = "version", type = String.class)
            }
    )

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

英文:

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:

@Override
@Cacheable(value = &quot;apps&quot;)
@Scheduled(cron = &quot;0 0/30 * * * ?&quot;)
@CacheEvict(value = &quot;apps&quot;,  allEntries=true)
public List&lt;Apps&gt; executeLatestApplications(){
...
List&lt;Apps&gt; apps = em.createNamedQuery(&quot;Apps&quot;, Apps.class).getResultList();
...
}

This is where apps SQL Result Mapping:

@NamedNativeQuery(
        name = &quot;AppsQuery&quot;,
        query = &quot;SELECT id, name, address, repo, version FROM apps&quot;,
        resultSetMapping = &quot;Mapping&quot;
)

@SqlResultSetMapping(
        name = &quot;Mapping&quot;,
        classes = @ConstructorResult(targetClass= com.meursault.apps.model.Apps.class,
                columns = {
                        @ColumnResult(name = &quot;id&quot;, type = Long.class),
                        @ColumnResult(name = &quot;name&quot;, type = String.class),
                        @ColumnResult(name = &quot;address&quot;, type = String.class),
                        @ColumnResult(name = &quot;repo&quot;, type = String.class),
                        @ColumnResult(name = &quot;version&quot;, type = String.class))

答案1

得分: 1

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

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

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

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

"查看这个帖子"

"您可以在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.

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

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.

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

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.

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

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:

确定