Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between value and cacheName param of @Cacheable annotation

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?

like image 522
Prasanna Avatar asked Apr 27 '18 10:04

Prasanna


People also ask

What does @cacheable annotation do?

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.

What is cacheNames in cacheable?

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.

How does @cacheable work in spring?

@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.

What is key in @cacheable spring boot?

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.


1 Answers

By taking a look into Cacheable documentation value is an Alias for cacheNames which means that both are tied to the same object.

  • values
  • cacheNames

Value:

@AliasFor(value="cacheNames")
public abstract java.lang.String[] value
Alias for cacheNames().
Default:
{}
like image 105
juanlumn Avatar answered Sep 27 '22 22:09

juanlumn