Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @CachePut to put the same value with two keys

I'm using Spring with build-in cache using @Cacheable and @CachePut annotations.

I have 2 methods in my @Service, one to save value in database, second to get value from database. Both of them uses cache.

@CachePut(key = "#code")
MyObject saveMyObject(MyObject o, String code) {
    return dao.save(o);
}

@Cacheable(key = "#code")
MyObject getMyObject(String code) {
    return dao.getMyObject(code);
}

While saving object I would like to put it in another cache e.g.

@CachePut(key = "'TMP_'.concat(#code)")

but I can't use two @CachePut annotations on saveMyObject method.

What should I do?

like image 815
user3626048 Avatar asked Oct 26 '25 14:10

user3626048


1 Answers

You can use org.springframework.cache.annotation.Caching annotation to group your CachePut:

@Caching( put = {
        @CachePut(key = "#code"),
        @CachePut(key = "'TMP_'.concat(#code)")
})
MyObject saveMyObject(MyObject o, String code) {
    return dao.save(o);
}
like image 65
Mykhailo Hodovaniuk Avatar answered Oct 28 '25 03:10

Mykhailo Hodovaniuk