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

Spring cacheable not synchronized

I have a singleton class (@Service annotated). This class has a method which takes 200/300ms to execute. This method is annotated with @Cacheable and synchronized. @Cacheable(value="nextPlaying", key = "#startingFrom.getYear() +…
ianaz
  • 2,490
  • 3
  • 28
  • 37
9
votes
4 answers

Spring cache using @Cacheable during @PostConstruct does not work

related to the commit in spring framework https://github.com/spring-projects/spring-framework/commit/5aefcc802ef05abc51bbfbeb4a78b3032ff9eee3 the initialisation is set to a later stage from afterPropertiesSet() to afterSingletonsInstantiated() In…
TimothyBrake
  • 551
  • 6
  • 9
8
votes
2 answers

Spring Boot Native cache : How to expire/remove cache data by individual keys/elements

We are calling Identity federation service to acquire user tokens very frequently and almost running load test on Identity service. A potential solution is to cache the user tokens in existing application, however with native spring-cache, can we…
Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
8
votes
0 answers

Caffeine cache logging

I am using caffeine cache provider with spring annotations, I am not able to see the logs from caffeinecachemanager when the entry’s put in cache or evicted or any activities that happens on cache.Do I have to explicitly mention the properties to…
Ags
  • 81
  • 3
8
votes
1 answer

how to manage spring cache in cluster environment

I am trying to build a cache service for my application using spring. Cache needs to be populated from database. My application runs on three nodes and would like all three nodes to be in sync with the cache. If one node gets an updated value in the…
kevin
  • 570
  • 4
  • 22
8
votes
2 answers

Cache interceptor call is ignored

I am working on cache implementation (exstremescale)for maven multi module project, where i have added below maven dependency com.ibm.extremescale ogclient
Sunil Rk
  • 999
  • 6
  • 12
  • 35
8
votes
2 answers

What strategies exist for using Spring Cache on methods that take an array or collection parameter?

I want to use Spring's Cache abstraction to annotate methods as @Cacheable. However, some methods are designed to take an array or collection of parameters and return a collection. For example, consider this method to find entites: public…
E-Riz
  • 31,431
  • 9
  • 97
  • 134
7
votes
2 answers

How to use spring boot 2 and ehcache 3 without xml?

For now I have following config: @Configuration @EnableCaching public class EhcacheConfig { @Bean public CacheManager cacheManager() throws URISyntaxException { return new…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
7
votes
1 answer

How to update cache every 30 minutes in spring?

I have following declaration: @Cacheable("books") public Book findBook(ISBN isbn) {...} But I want to update the cache every 30 minutes. I understand that I can create @Scheduled job to invoke method annotated @CacheEvict("books") Also, I suppose…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
7
votes
3 answers

Is it possible to make the application ignore caching in case the cache server fails?

I have a spring boot application with the following properties: spring.cache.type: redis spring.redis.host: spring.redis.port: Right now if the remote host fails the application also fails with a connection error. As in this…
Phate
  • 6,066
  • 15
  • 73
  • 138
7
votes
1 answer

Spring Cache Exception: io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for 'set' command

I use Spring Boot Version: 2.1.4.RELEASE When I set spring.cache.redis.time-to-live=20000 in application.yml file.I got a exception. What should I do when I want to add TTL in redis cache ? How to use cache expiration at Spring Cache with…
itning
  • 150
  • 1
  • 8
7
votes
1 answer

Custom CacheInterceptor is being overridden by default Spring's CacheInterceptor

I've implemented a custom CacheInterceptor which allows evicting the cache by wildcard: public class CustomCacheInterceptor extends CacheInterceptor { private static final Logger LOGGER = LoggerFactory.getLogger(CustomCacheInterceptor.class); …
Mykola Yashchenko
  • 5,103
  • 3
  • 39
  • 48
7
votes
1 answer

Handling Java 8 Optional with Spring cache

Considering a service class that can insert and retrieve objects and use Spring cache abstraction, how can I annotate methods in a way that an Optional is returned? class MyServiceImpl implements MyService { private static final String…
andrucz
  • 1,971
  • 2
  • 18
  • 30
6
votes
1 answer

Spring Cache Caffeine bulk retrieval

Is it possible to use Caffeine's CacheLoader::loadAll with @Cacheable annotated method with collection parameter, like @Cacheable(cacheNames = "exampleCache", cacheManager="exampleCacheManager", keyGenerator = "complexKeyGenerator") List
dev123
  • 477
  • 8
  • 20
6
votes
2 answers

Caffeine Cache in Spring Boot Cache : Get all cached keys

I'm using Caffeine Cache library for Spring Cache. Is there a way to get all the cached keys? My current application works on a near-realtime data, with the flow as : In the Cache Updater Thread(which runs at a fixed interval, irrespective of the…
imAmanRana
  • 123
  • 1
  • 3
  • 9