Search code examples
javaspring-bootcachingspring-cache

Caching Filtered Data After Filtering Unwanted Data


Imagine I have a data type called: Result Which contains more data types. Take this data for an example:

[{Albums : { 
  name: “albumName”,
  artist: {name: “artistName,
           link:“https:link.com”
           },
  link: “http...”
       }   Tracks: {
  name: “trackName”,
  artist: {name: “artistName,
           link:“https:link.com”
           },
  link: “http...”
       }    Playlists: {
  name: “playlistName”,
  artist: {name: “artistName,
           link:“https:link.com”
           },
  link: “http...”
       }
}]

Imagine you have filtered the data using any kind of method and the value you only want to get is the names, so you do the filter thing and then cache all the values that is filtered so that when you call the api again, there is no need for filtering it again since you can get it straight in the cache. Is this even possible with or without @Cachaeble and is there any improvement in the performance?


Solution

  • The question is quite generic at the moment, so I do a generic answer:

    It is possible with Cacheable or without, which means you call a cache implementation directly or implement the caching mechanism by yourself. The performance increase depends on the latency of your API calls, the latency / CPU consumption for the filter in contrast to the processing overhead of caching, which is the Spring cache abstraction and the cache solution.

    If there is only a few different queries requested repeatedly your cache will be effective. If you have lots of different queries and not much repetition your cache might be uneffective and/or consume a lot of memory.