Search code examples
javaspringspring-bootcachingspring-cache

How to cache a List<Object> in Java such that the elements in list are cached as a single entry each, using Spring Boot provided caching techniques?


@Query(value = "select  student_rid as row_id, student_name as Name from student_data where student_rid=:Student and visibility=true)", nativeQuery = true)
public List<Map<String, String>> findNameAndRowID(@Param("Student") Long Student);

I want to cache the list output, but when I am trying to cache the output the whole List is cached as a single cache entry, and because of this, I have to evict this whole cache(list) entry all the time when I either insert/delete even a single record into the database, which is not what I want as it does not serves the purpose of caching. So is there a way we can cache the list elements as a single entries such that I can only evict/update the single record at any insert/delete statement.

I am using Spring Boot, Java 8, and Spring tool suite as IDE.


Solution

  • This question has been asked multiple times.

    1. How to cache the list of entires based on its primary key using spring caching
    2. Spring Cache with collection of items/entities
    3. Putting all returned elements into a Spring-Boot cache using annotations
    4. What strategies exist for using Spring Cache on methods that take an array or collection parameter?

    The answer is generally the same.

    I suspect any 1 of the links will help, but perhaps start with #3.