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
19
votes
9 answers

How to disable Redis Caching at run time if redis connection failed

We have rest api application. We use redis for API response caching and internal method caching. If redis connection then it is making our API down. We want to bypass the redis caching if that redis connection fails or any exception instead of…
cooldude
  • 935
  • 2
  • 9
  • 18
17
votes
7 answers

How to load @Cache on startup in spring?

I'm using spring-cache to improve database queries, which works fine as follows: @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("books"); } @Cacheable("books") public Book getByIsbn(String isbn) { return…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
15
votes
4 answers

Multiple Caffeine LoadingCaches added to Spring CaffeineCacheManager

I'm looking to add several distinct LoadingCache's to a Spring CacheManager, however I don't see how this is possible using CaffeineCacheManager. It appears that only a single loader is possible for refreshing content, however I need separate…
Steve
  • 53,375
  • 33
  • 96
  • 141
15
votes
3 answers

Spring Cache with Redis - How to gracefully handle or even skip Caching in case of Connection Failure to Redis

I've enabled Caching in my Spring app and I use Redis to serve the purpose. However, whenever a connection failure occurs, the app stops working whereas I think it had better skip the Caching and go on with normal execution flow. So, does anyone…
Peter Bean
  • 313
  • 2
  • 9
15
votes
5 answers

How update/remove an item already cached within a collection of items

I am working with Spring and EhCache I have the following method @Override @Cacheable(value="products", key="#root.target.PRODUCTS") public Set findAll() { return new LinkedHashSet<>(this.productRepository.findAll()); } I have other…
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
15
votes
4 answers

ehcache configuration in Spring framework

I am trying to load some context from an RSS feed and pass it as a cache to the client using ehcache library in spring. here is my code : import org.springframework.cache.annotation.Cacheable; @Service public class GlossaryReaderService { …
user261002
  • 2,182
  • 10
  • 46
  • 73
14
votes
1 answer

Caching annotation on interface methods

Given I have Spring Data repository and I put Cacheable annotation on findAll method: @Repository @CacheConfig(cacheNames = TEMPLATE_CACHE) public interface TemplateRepository extends JpaRepository { @Override @Cacheable …
Patrik Mihalčin
  • 3,341
  • 7
  • 33
  • 68
14
votes
4 answers

Does Spring @Cacheable block if accessed by more that 1 thread?

If a method marked @Cacheable takes 10 minutes to complete and two threads t1,t2 access the method. t1 accesses at time 0 (cache method is now run for first time) t2 accesses at time t1+5mins Does this mean that t2 will not access the data for…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
13
votes
4 answers

@Cachable on methods without input parameters?

I'm having a problem with @org.springframework.cache.annotation.Cachable annotation: @Bean public ConcurrentMapCache cache() { return new ConcurrentMapCache(CACHE); } @Cacheable(CACHE) public String getApi() { return…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
13
votes
3 answers

Caching Java 8 Optional with Spring Cache

I have a method: @Cacheable(key = "#jobId") public Optional getJobById(String jobId) { log.info("Querying for job " + jobId); counterService.increment("queryJobById"); Job job = jobsRepository.findOne(jobId); if (job !=…
Roi Ezra
  • 506
  • 1
  • 5
  • 11
12
votes
5 answers

Error during Deserialization of PageImpl : Cannot construct instance of `org.springframework.data.domain.PageImpl`

Issue is when using Spring cache with redis cache manager, not able to deserializer Spring Pageable response due to no default constructor The spring boot version used is 2.1.4.RELEASE Redis config class that uses the serializer @Bean public…
12
votes
2 answers

How to cache REST API response in java

I am building an app in java.I hit api more than 15000 times in loop and get the response ( response is static only ) Example ** username in for loop GET api.someapi/username processing end loop ** It is taking hours to complete all the…
thaveethu gce
  • 585
  • 1
  • 9
  • 20
12
votes
2 answers

Why doesn't this stream & lambda expression work with SpEL declaration?

I'm trying to use a Java 8 stream and lambda expression in a Spring @Cache annotation. I'm trying to use the following: @CacheEvict(value = "tags", allEntries = true, condition = "#entity.getTags().stream().anyMatch(tag -> tag.getId() ==…
Justin
  • 6,031
  • 11
  • 48
  • 82
11
votes
2 answers

after upgrade to Spring Boot 2, how to expose cache metrics to prometheus?

I recently upgraded a spring boot application from 1.5 to 2.0.1. I also migrated the prometheus integration to the new actuator approach using micrometer. Most things work now - including some custom counters and gauges. I noted the new prometheus…
wemu
  • 7,952
  • 4
  • 30
  • 59
11
votes
6 answers

Spring @Cacheable and @Async annotation

I have the need to cache some the results of some asynchronous computations. In detail, to overcome this issue, I am trying to use Spring 4.3 cache and asynchronous computation features. As an example, let's take the following code: @Service class…
riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
1
2
3
45 46