How can I reproduce the key (multiple) in Caching Spring Boot to evict? And spring-context-support issue?

Viewed 16

I using cache by multiple keys like this

    @Cacheable(
         value = "cache_name",
         key = "{#id, #name}"
    )

I'd like to evict it in another function using CacheManager

    Long id = 123L;
    String name = "abc";


    cacheManager.getCache("cache_name").evict(cacheKey);

Question #1: How can I create cacheKey to match with real key created by @Cacheable

The real key when I print is a String like this: [123, abc]

I tried with SimpleKey, but it does not work

And I tried with:

String cacheKey = Arrays.toString(new String[]{id.toString(), name});

Ofcourse, it does not work also.

Question #2:

Currently, I'm using solution to call void function and use @CacheEvict in separate Service (ex: CacheService)

It's ok when I call directly this function, but when I call it via another function (same function in the CacheService, the cache doesn't evict? Why? Look like I lose context of spring? Any advice?

Thank you.

0 Answers
Related