46

I am using Spring Cache, where I pass in a collection of keys, and the return is a list of entities. I would like to have the caching framework understand that each element in the return list is to be cached with the corresponding code. At the moment, it seems that the key is the whole list, and if I am missing a key in the subsequent call, it'll try to reload the whole collection again.

@Override
@Cacheable(value = "countries")
public List<Country> getAll(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}

another possibility is that the return is a map, similarly I would like the cache to be intelligent enough to only query for items that were never queried before, also to cache them each item with its key.

@Override
@Cacheable(value = "countries")
public Map<String,Country> getAllByCode(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}

Suppose the country class looks like this:

class Country{
  String code; 
  String fullName;
  long id;

... // getters setters constructurs etc.. 
}

Is this possible with Spring Cache?

4
  • 1
    Why not using the second level of JPA ? Spring cache is really helpless and error prone to use for caching compared to JPA or Cache library as EhCache.
    – davidxxx
    Commented Jun 13, 2017 at 18:38
  • 1
    @davidxxx as far as I understand, Spring Cache is just an abstraction, and EhCache can be the implementation underneath. The difference is that it takes away the need to write any caching logic beyond the annotation. How would I write the code above with JPA?
    – Charbel
    Commented Jun 13, 2017 at 23:31
  • 1
    @Charbel have you got any solution for this, I have same problem Commented Jan 3, 2020 at 10:37
  • @VishalKawade nope, I have not, at some point I thought I write an extension and submitting it to Spring Source, but never got round to do it
    – Charbel
    Commented Jan 23, 2020 at 10:21

4 Answers 4

35

In fact, it is possible, even with Spring's Caching Abstraction, but not out-of-the-box (OOTB). Essentially, you must customize Spring's caching infrastructure (Explained further below)

By default, Spring's caching infrastructure uses the entire @Cacheable method parameter arguments as the cache "key", as explained here. Of course you can also customize the key resolution using either a SpEL Expression or with a custom KeyGenerator implementation, as explained here.

Still, that does not break up the collection or array of parameter arguments along with the @Cacheable method's return value into individual cache entries (i.e. key/value pairs based on the array/collection or Map).

For that, you need a custom implementation of Spring's CacheManager (dependent on your caching strategy/provider) and Cache interfaces.

NOTE: Ironically, this will be the 3rd time I have answered nearly the same question, first here, then here and now here, :-). Anyway...

I have updated/cleaned up my example (a bit) for this posting.

Notice that my example extends and customizes the ConcurrentMapCacheManager provided in the Spring Framework itself.

Theoretically, you could extend/customize any CacheManager implementation, like Redis's in Spring Data Redis, here (source), or Pivotal GemFire's CacheManager in Spring Data GemFire, here (source). The open source version of Pivotal GemFire is Apache Geode, which has a corresponding Spring Data Geode project, (source for CacheManager in Spring Data Geode, which is basically identical to SD GemFire). Of course, you can apply this technique to other caching providers... Hazelcast, Ehcache, etc.

However, the real guts of the work is handled by the custom implementation (or mores specifically, the base class) of Spring's Cache interface.

Anyway, hopefully from my example, you will be able to figure out what you need to do in your application to satisfy your application's caching requirements.

Additionally, you can apply the same approach to handling Maps, but I will leave that as an exercise for you, ;-).

Hope this helps!

Cheers, John

7
  • Thank you @John I'll have a look at this and try to implement it for my needs!
    – Charbel
    Commented Jun 20, 2017 at 21:49
  • Thanks @John if I understand you correctly, if we were to do that, we would need custom implementation of Spring's CacheManager for each and every one cache providers (Redis, ehCache) we might used?
    – alchn
    Commented Dec 12, 2019 at 6:26
  • Essentially, extending each cache provider CacheManager & Cache implementation (e.g. Redis or ehCache, etc) is what I was proposing. However, I can also imagine it would be possible to introduce a more common solution using AOP, or even just a simple reusable CacheManager/Cache Decorator implementation wrapping an existing caching provider implementation (i.e. Redis or ehCache) to handle the Collection of cacheable elements. Continued...
    – John Blum
    Commented Dec 12, 2019 at 17:59
  • 1
    In conclusion, if possible, I think it would be better to utilize the underlying caching providers strengths through extension rather than delegation in some Use Cases. The choice is yours though and this is just food for thought. If you need a more concrete example, I can perhaps find some time later to demonstrate what I mean if what I wrote in the comments was not clear. Just let me know. Hope this helps. Cheers!
    – John Blum
    Commented Dec 12, 2019 at 18:08
  • 2
    Hi, how can we implement the partial cache misses? e.g. If fatorial({1,2,3}) and only 1 & 3 are not in cache, we want it to actually only compute 1 & 3. I cannot figure out a way about how to modify your sample code. Thanks!
    – ch271828n
    Commented Aug 28, 2020 at 7:26
4

With @CachePut and a helper method you can achieve it very simply like this :

public List<Country> getAllByCode(List<String>codes) {
    return countryDao.findAllInCodes(codes);
}

public void preloadCache(List<String>codes) {
    List<Country> allCountries = getAllByCode(codes);
    for (Country country : allCountries) {
        cacheCountry(country);
    }
}

@CachePut
public Country cacheCountry(Country country) {
    return country;
}

Note

This will only add values to the cache, but never remove old values. You can easily do cache eviction before you add new values

Option 2

There is a proposal to make it work like this :

@CollectionCacheable
public List<Country> getAllByCode(List<String>codes) {    

See :

If you are impatient take the code from GitHub and integrate locally

1
  • 2
    How would this ever hit the cache when invoking the finder then?
    – Stefano L
    Commented Jul 29, 2020 at 10:38
0

I found two native workarounds to use a complex collection values as a cache key.

The first method is using a computed string as the cache key:

    @Cacheable(value = "Words", key = "{#root.methodName, #a1}", unless = "#result == null")
    //or
    @Cacheable(value = "Words", key = "{#root.methodName, #p1}", unless = "#result == null")
    //or
    @Cacheable(value = "Words", key = "{#root.methodName, #precomputedString}", unless = "#result == null")
    public List<Edge> findWords(HttpServletRequest request, String precomputedStringKey) {

    }

In order to call to this method service as follows:

//use your own complex object collection to string mapping as a second parameter  
 service.findWords(request.getParameterMap().values(),request.getParameterMap()
                        .values()
                        .stream()
                    .map(strings -> Arrays.stream(strings)
                            .collect(Collectors.joining(",")))
                    .collect(Collectors.joining(","));)

And the second method (my preferred way):

@Cacheable(value = "Edges", key = "{#root.methodName, T(package.relationalDatabase.utils.Functions).getSpringCacheKey(#request.getParameterMap().values())}", unless = "#result == null")
    public List<Edge> findWords(HttpServletRequest request, String precomputedStringKey) {
        
        }

Where package.relationalDatabase.utils.Functions getSpringCacheKey is a own created function as follows:

public static String getSpringCacheKey(Object o) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();
        boolean isSpringEntity = o.getClass().getAnnotation(javax.persistence.Entity.class) != null;
        if (isSpringEntity) {
            return objectMapper.writerWithView(JSONViews.Simple.class).writeValueAsString(o);
        } else {
            return objectMapper.writeValueAsString(o);

        }
}

Note 1: this method allows the combination of the native key cache notation with a custom wrapper. Unlike Spring cache's keyGenerator property which does not allow the key annotation (they are mutually exclusive) and which require the creation of a CustomKeyGenerator.

@Cacheable(value = "Edges", unless = "#result == null", keyGenerator = "CustomKeyGenerator")
public List<Edge> findWords(HttpServletRequest request, String precomputedStringKey) {           
            }
////////
public class CustomKeyGenerator implements KeyGenerator {
    Object generate(Object target, Method method, Object... params)

}

And the creation of a return wrapper for each complex collection key.

For example:

@Override
public Object generate(Object target, Method method, Object... params) {
    
    
    if(params[0] instanceof Collection)
    //do something 
    if(params[0] instanceof Map)
    //do something 
    if(params[0] instanceof HttpServletRequest)
    //do something      
}

Therefore, the proposed method allows:

//note #request.getParameterMap().values()
@Cacheable(value = "Edges", key = "{#root.methodName, T(package.relationalDatabase.utils.Functions).getSpringCacheKey(#request.getParameterMap().values())}"
    
//note #request.getParameterMap().keySet()
@Cacheable(value = "Edges", key = "{#root.methodName, T(package.relationalDatabase.utils.Functions).getSpringCacheKey(#request.getParameterMap().keySet())}"

without need to update the method for each collection.

Note 2: this method allows the usage of jackson views for spring entities but in some cases there is needed the @JsonIgnoreProperties({"hibernateLazyInitializer"}) annotation.

Finally the trace result of spring cache for this method is the following:

Computed cache key '[findWords, [[""],["0"],[""],[""],[""],[""],["brazil"],["on"],["false"]]]' for operation Builder[public java.util.List package.relationalDatabase.services.myClass.find(javax.servlet.http.HttpServletRequest)] caches=[myClass] | key='{#root.methodName, T(package.relationalDatabase.utils.Functions).getSpringCacheKey(#request.getParameterMap().values())}' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='#result == null' | sync='false'

On the other hand, it is recommended to use string hash functions to compress the resulting key value.

In order to avoid problems with the T() function in the JAR package lifecycle it is preferable to create a bean:

@Bean
    KeySerializationComponent keySerializationComponent() {
        return new KeySerializationComponent();
    }

and call it with:

@Cacheable(value = "Document", key = "{#root.methodName,#size,@keySerializationComponent.getSpringCacheKey(#ids)}", unless = "#result == null")
-1

Why not caching you list as a String?

@Cacheable(value = "my-cache-bucket:my-id-parameters", key = "{#id, #parameters}")
getMy(UUID id, String parameters) { ... }

Use like:

getMy(randomUUID(), parametersList.toString());

Your cache key will look like:

"my-cache-bucket:my-id-parameters::3ce42cd9-99d4-1d6e-a657-832b4a982c72,[parameterValue1,parameter-value2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.