1

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))
meursault
  • 255
  • 1
  • 4
  • 13

2 Answers2

1

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.

Pankaj
  • 2,220
  • 1
  • 19
  • 31
  • i have a list of applications shown in my page and i try to add new app to my database with using swagger, it won't show me the new app. again it does not update my cache – meursault Jul 28 '20 at 12:18
  • i'm using sql result mapping, it's not actually updated in method. To be updated it must call sql query. And one more thing entity manager does not have update method?? it gives an error – meursault Jul 28 '20 at 12:47
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();
    ...
}
Thirumal
  • 8,280
  • 11
  • 53
  • 103