Search code examples
javaspringspring-bootehcache

@Cacheable And @CachePut on the same method with opposite condition


Is it possible to use @CachePut and @Cacheable in conjonction on the same method?

@CachePut(value ="cacheName", key = "#id", condition="#cachedData==false")
@Cacheable(value ="cacheName", key = "#id", condition="#cachedData==true")
public Foo doSomthing(String id, boolean cachedData){...}

Solution

  • I think you can use @Caching to group multiple caching annotations to implement a customised logic.

    Maybe something like:

    @Caching(
                put= { @CachePut(value="cacheName", key="#id", condition="#cachedData==false") },
                cacheable = { @Cacheable(value ="cacheName", key="#id", condition="#cachedData==true") }
    )
    public Foo doSomething(String id, boolean cachedData){...}