spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Add support to clear cache by key pattern

Open JosRoseboom opened this issue 2 years ago • 0 comments

Suppose we have this expensive method that we want to cache:

public MyData getCarBrandDataForCountry(long carbrandId, long countryId){ // my expensive logic }

Now, if some underlying data of a carbrand changes, we want to clear the caches that involves that brand. Similarly, if relevant data of a country changes, we want to clear all the caches that involve that country. Currently, we can only:

  • evict cache by key. We cannot use that, since we only know half of the key
  • clear the entire cache. That would give correct results, but clears a lot of keys unnecessarily.

Currently, clear cache is implemented by clearing all the keys that match the pattern *. Making the pattern a parameter with * as the default would solve the above case. For example, if we would update relevant data for country 42, we could call clearByPattern("*42")

Note: Created this issue to refer to in unit test. PR is on the way. Edit: Created PR #2380

JosRoseboom avatar Aug 01 '22 08:08 JosRoseboom