英文:
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 = "apps")
@Scheduled(cron = "0 0/30 * * * ?")
@CacheEvict(value = "apps", allEntries=true)
public List<Apps> executeLatestApplications(){
...
List<Apps> apps = em.createNamedQuery("Apps", Apps.class).getResultList();
...
}
This is where apps SQL Result Mapping:
@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))
答案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 = "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();
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论