【发布时间】:2020-07-29 10:28:54
【问题描述】:
我有一个方法可以在我的 spring-boot 项目中返回列表。此列表是使用 DTO 映射创建的,我不希望我的代码在每次刷新页面时都执行 sql 查询。有时新的更新可能会出现在数据库中。因此,例如,我希望它每 5 小时刷新一次缓存。 Cacheable 正在运行但未接收新数据。我查看了 Cache Evict 示例,但是当我应用它时,我的代码没有进行任何缓存。还有一件事,是否可以在同一方法中缓存多个列表?例如下面的代码,但它不起作用:
@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();
...
}
这是应用程序 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))
【问题讨论】:
标签: java spring spring-boot caching