Spring cache/jsr107: list/collection argument as part of the key
Asked Answered
K

2

6

I have a service which calls external system to retrieve some kind of objects by their external id as well as submit them back to update. Rather than retrieving objects one by one there is a more generic purpose methods:

public interface ExternalSystem {
    List<ExternalDTO> getObjects(List<String> externalIds);

    void updateObjects(List<ExternalDTO> updates);
}

I would like put a cache on top of the ExternalSystem calls because they are quite expensive.

In the implementation of the service I can simply put spring annotations:

@Cacheable("cache-external")
List<ExternalDTO> getObjects(List<String> externalIds) {} 

@CacheEvict(cacheNames="cache-external", allEntries=true)
void updateObjects(List<ExternalDTO> updates);

However, such a cache will behave very badly in case I have a lot of intersection between externalIds, i.e.

  1. Call#1 getObjects([1,2,3,4]) -> cache put by [1,2,3,4] key
  2. Call#2 getObjects([1,2,3,4,5]) -> cache put by [1,2,3,4,5] key
  3. Call#3 getObjects([6,7,8,9]) -> cache put by [6,7,8,9] key
  4. Call#4 updateObjects(1) -> evict all the caches but the third cache doesn't contain 3

So, the question is how to implement the custom strategy (I assume it's not doable out-of the box) which will evict only those entries which really should be evicted and will make the keys such a way that intersecting objects are retrieved from the cache?

Upd. I've found two similar questions:

  1. spring-cache-abstraction-with-multi-value-queries
  2. using-spring-cache-on-methods-that-take-an-array-or-collection
  3. spring-cacheable-methods-with-lists

Upd2. Here is something similar to what I want except I will put into cache pairs of String and ExternalDTO for each item in collection. element-level-caching-of-list-to-list

Kittenish answered 20/5, 2016 at 9:7 Comment(0)
A
0

AFAIK this is not possible with annotations. You can use the imperative API, which contains the bulk operations you need, for example Cache.getAll(keySet) and Cache.removeAll(keySet)

Ahithophel answered 20/5, 2016 at 9:27 Comment(3)
Yes, and I bet there should already be some custom implementations. The question is not only about removals, please see the attached questions in Upd.Kittenish
Just curious: How many instances is your application typically requesting at once? If you cannot do it via a bulk request but one after another, will you fail to meet the maximum latency requirements from your customer? If you go for a custom solution: Have you profiled all your application and this is the optimization that will have the most impact? Just asking ;)Ahithophel
It's a good question but even if it's doable in my case it may not be doable in others case. So, the question is more generic than just my case. So, I am searching some kind of reusable solution for such cases. If I don't find it, I will write my own and post it here :)Kittenish
A
-1

For me it worked fine with this config. This is a obfuscated version of my code.

@Cacheable(cacheNames = "test", key = "#p0")
public List<String> getTestFunction(List<String> someIds) {
getTestFunction(Arrays.asList("A","B","C"));
2020-04-02 15:12:35.492 TRACE 18040 --- [Test worker] o.s.cache.interceptor.CacheInterceptor   : Computed cache key '[A, B, C]' for operation Builder[public java.util.List org.Main.getTestFunction(java.util.List)] caches=[test] | key='#p0' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

You see that it concatinated strings

... Computed cache key '[A, B, C]' ...

My setup: /resources/ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <cache name="test"
           maxBytesLocalHeap="1M"
           timeToLiveSeconds="300"/>
</ehcache>

gradle.build

plugins {
    id "org.springframework.boot" version "2.2.4.RELEASE"
    ....
}
dependencies {
    implementation "org.springframework.boot:spring-boot-starter-cache"
    implementation "org.ehcache:ehcache:3.8.1"
    ...
}

Audible answered 2/4, 2020 at 13:19 Comment(3)
I just googled what #p0 and that's just the value of parameter 1 which is jus a reference in hex. You are using a hex reference as key which I think is not appropriate.Desalvo
@Desalvo you're right, it's the value of the 1st parameter. 1. what is a hex reference ? 2. why is it not appropriate ?Audible
cause if you query each time with a slightly different list you'll always get a complete miss on the cacheAnalog

© 2022 - 2024 — McMap. All rights reserved.