简体   繁体   中英

Spring Boot Cache List

I have a method which returns list. List variables are coming from database and sometimes data is updating so i want to cache list but for example in every half an hour i want to clear the cache. How can i do this with Cacheable and CacheEvict method? I tried to use them in same method as annotation but it didn't work and i have nowhere to call them other than this method.

@Cacheable(value = "apps")
@Scheduled(cron = "0 0/30 * * * ?")
@CacheEvict(value = "apps")
public List<Apps> findLatestApps() {
List<Apps> apps = entityManager.createNamedQuery("AppsQuery", Apps.class).getResultList();
...
return Apps;

}

This is where i call list method;

public ResponseEntity<AppsResponse> getLatestApps(@PathVariable String name) throws ResourceNotFoundException {
        AppsDto appsDto = appsService.findLatestApps(name);
        return new ResponseEntity<>(new ModelMapper().map(appsDto, AppsResponse.class), HttpStatus.OK);
    }

You can't do this in the same method. Because what should the method do? Cache or Evict?

@Cacheable(value = "apps")
public List<Apps> findLatestApps() {
    List<Apps> apps = entityManager.createNamedQuery("AppsQuery", Apps.class).getResultList();
    ...
    return apps;
}

@Scheduled(cron = "0 0/30 * * * ?")
@CacheEvict(value = "apps")
public void clearAppsCache() {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM