I am new to @Cacheable and @CacheEvict annotations which we use for caching in Spring framework. But while looking into its implementation and parameters that @Cacheable uses what is the difference between value and cacheName params of @Cacheable annotation?
As the name implies, @Cacheable is used to demarcate methods that are cacheable - that is, methods for whom the result is stored into the cache so on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually execute the method.
The cacheNames are the names of the caches itself, where the data is stored. You can have multiple caches, e.g. for different entity types different caches, or depending on replication needs etc.
@CacheableThis method-level annotation lets Spring Boot know that the return value of the annotated method can be cached. Each time a method marked with this @Cacheable is called, the caching behavior will be applied.
Any data stored in a cache requires a key for its speedy retrieval. Spring, by default, creates caching keys using the annotated method's signature as demonstrated by the code above. You can override this using @Cacheable's second parameter: key. To define a custom key you use a SpEL expression.
By taking a look into Cacheable documentation value is an Alias for cacheNames which means that both are tied to the same object.
Value:
@AliasFor(value="cacheNames")
public abstract java.lang.String[] value
Alias for cacheNames().
Default:
{}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With