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

前端 未结 2 1505
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 01:37

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

相关标签:
2条回答
  • 2020-12-22 02:06

    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"
        ...
    }
    
    
    0 讨论(0)
  • 2020-12-22 02:12

    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)

    0 讨论(0)
提交回复
热议问题