48197

How to cache the list of entires based on its primary key using spring caching

Question:

I want to cache the response of the repository class which has the following methods:

@Cacheable(cacheNames = "books", key="#id") Book findById(Long Id); @CacheEvict(cacheNames = "books", key = "#id") void deleteById(Long id); @Cacheable(cacheNames = "books", key="#book.id") Book save(Book book); @Cacheable("books") List<Book> findAll();

Except the <strong>findAll()</strong> method, others are working as expected.

How to make <strong>findAll()</strong> to populate the <strong>books</strong> cache with <strong>book.id</strong> as key?

Answer1:

Once more, this <a href="https://stackoverflow.com/questions/44529029/spring-cache-with-collection-of-items-entities" rel="nofollow">nearly identical question</a> keeps coming up over and over again with only slightly different expectations, this time when combined with SD <em>Repositories</em>, and specifically the findAll() query method. And again, the answer is the same in each use case... you need to provide a customized <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/CacheManager.html" rel="nofollow">CacheManager</a> and <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/Cache.html" rel="nofollow">Cache</a> implementation for your caching provider (e.g. Ehcache, Redis or Hazelcast).

By default, OOTB, <em>Spring's Cache Abstraction</em> does not split up cached method array/collection type return values into separate entries in the targeted cache. You must handle this yourself.

See my last response for 1 example of how someone might go about handling this.

-j

Recommend

  • How would I send and receive packets over a WebSocket in Javascript
  • Which table should be Parent table and which should be child table?
  • removing the default blue color on focus
  • Capturing STDOUT in RSpec
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • Hibernate Joda DateTime Sorting
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Classic ASP URL Rewriting
  • Efficient User-Agent Regex to find Safari in Python
  • BeautifulSoup difference between findAll and findChildren
  • C#: Import/Export Settings into/from a File
  • JPA flush vs commit
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • How can I set a binding to a Combox in a UserControl?
  • AJAX Html Editor Extender upload image appearing blank
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Java applet as stand-alone Windows application?
  • Trying to switch camera back to front but getting exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Web-crawler for facebook in python
  • Matrix multiplication with MKL
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • File not found error Google Drive API
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • Programmatically clearing map cache
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?