Questions tagged [spring-cache]

Spring cache provides a Cache and CacheManager abstraction with several implementations including support for ehcache and JSR-107 providers. It also applies caching to Java methods, reducing thus the number of executions based on the information available in the cache. Both declarative annotation-based caching and aspect-oriented caching are supported.

Spring Framework provides support for transparently adding caching into an existing Spring application. Similar to the transaction support, the caching abstraction allows consistent use of various caching solutions with minimal impact on the code.

To use the cache abstraction, the developer needs to take care of two aspects:

  • caching declaration - identify the methods that need to be cached and their policy
  • cache configuration - the backing cache where the data is stored and read from

Cache configuration

Spring provides provides several storages integration. To use them, one needs to simply declare an appropriate CacheManager - an entity that controls and manages Caches and can be used to retrieve these for storage.

Support for simple in-memory map, ehcache, guava and JSR-107 compliant caches are available out of the box.

Useful links:

687 questions
61
votes
3 answers

Spring cache @Cacheable method ignored when called from within the same class

I'm trying to call a @Cacheable method from within the same class: @Cacheable(value = "defaultCache", key = "#id") public Person findPerson(int id) { return getSession().getPerson(id); } public List findPersons(int[] ids) { …
David Zhao
  • 4,284
  • 11
  • 46
  • 60
55
votes
8 answers

Expiry time @cacheable spring boot

I have implemented a cache and now I want to add an expiry time. How can I set an expiry time in spring boot with @Cacheable? This is a code snippet: @Cacheable(value="forecast",unless="#result == null")
nole
  • 1,422
  • 4
  • 20
  • 32
53
votes
2 answers

How to test Spring's declarative caching support on Spring Data repositories?

I have developed a Spring Data repository, MemberRepository interface, that extends org.springframework.data.jpa.repository.JpaRepository. MemberRepository has a method: @Cacheable(CacheConfiguration.DATABASE_CACHE_NAME) Member findByEmail(String…
balteo
  • 23,602
  • 63
  • 219
  • 412
46
votes
5 answers

Spring cache logging on @Cacheable hit

Currently I am working with a Spring Cache and the @Cacheable/@CacheEvict annotations. I would like to get some sort of a console log statement like "INFO: i got those values from the cache, NOT from the host. awesome" Is there a clean and easy way…
BassSultan
  • 574
  • 1
  • 5
  • 11
46
votes
5 answers

Spring Boot - How to disable @Cacheable during development?

I'm looking for 2 things: How to disable all caching during development with Spring boot "dev" profile. There doesn't seem to be a general setting to turn it all off in application.properties. What's the easiest way? How to disable caching for a…
Wouter
  • 1,678
  • 3
  • 20
  • 32
45
votes
4 answers

Is it possible to set a different specification per cache using caffeine in spring boot?

I have a simple sprint boot application using spring boot 1.5.11.RELEASE with @EnableCaching on the Application Configuration class. pom.xml org.springframework.boot
David
  • 7,652
  • 21
  • 60
  • 98
42
votes
2 answers

Spring Cache: Evict multiple caches

I'm using Spring Cache abstraction and I have multiple caches defined. Sometimes, when data changes, I want to evict more than one caches. Is there away to evict multiple cache using Spring's @CacheEvict annotation?
user655145
41
votes
4 answers

Spring Cache with collection of items/entities

I am using Spring Cache, where I pass in a collection of keys, and the return is a list of entities. I would like to have the caching framework understand that each element in the return list is to be cached with the corresponding code. At the…
Charbel
  • 14,187
  • 12
  • 44
  • 66
31
votes
3 answers

How to have multiple cache manager configuration in spring cache java

I want to have multiple spring cache managers configured in my web-application and I would be able to use different cache manager at various places in my project. Is there any way to do this.
Rekha
  • 311
  • 1
  • 3
  • 4
29
votes
7 answers

Schedule Spring cache eviction?

Is it possible to schedule spring cache eviction to everyday at midnight? I've read Springs Cache Docs and found nothing about scheduled cache eviction. I need to evict cache daily and recache it in case there were some changes outside my…
Philippe Gioseffi
  • 1,488
  • 3
  • 24
  • 41
28
votes
3 answers

Spring Webflux and @Cacheable - proper way of caching result of Mono / Flux type

I'm learning Spring WebFlux and during writing a sample application I found a concern related to Reactive types (Mono/Flux) combined with Spring Cache. Consider the following code-snippet (in Kotlin): @Repository interface TaskRepository :…
Tomek Zaremba
  • 353
  • 1
  • 4
  • 7
26
votes
3 answers

Spring cache all elements in list separately

I'm trying to add caching to a CRUD app, I started doing something like this: @Cacheable("users") List list() { return userRepository.findAll() } @CachePut(value = "users", key = "#user.id") void create(User user) { …
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
22
votes
5 answers

Spring @Cacheable default TTL

I generally use the @Cacheable with a cache config in my spring-boot app and set specific TTL (time to live) for each cache. I recently inherited a spring boot app that uses @Cacheable without explicitly stating a cache manager and ttl. I will be…
Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150
20
votes
2 answers

Default Cache Manager with Spring Boot using @EnableCaching

I have implemented caching in my SpringBootApplication as shown below @SpringBootApplication @EnableCaching public class SampleApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder…
PRATHAP S
  • 675
  • 2
  • 8
  • 26
20
votes
2 answers

What are the best Cache practices in ehcache or spring cache for spring MVC?

Planning to implement cache mechanism for static data in spring web based application, can any one explain which is the best and how it works? EhCache Spring Cache
Delli Kilari
  • 988
  • 3
  • 14
  • 31
1
2 3
45 46