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
11
votes
1 answer

Spring ehcache vs Memcached?

I have worked on spring cahing using ehcache . To me it is like same with different set of API exposed and their implementation. What's the difference in terms of features provided between them apart from API/implementation ? Update:- I have…
scott miles
  • 1,511
  • 2
  • 21
  • 36
11
votes
1 answer

Redis versus Guava Cache

I have a code in which i have implemented cache mechanism. Previously it was Guava based caching now i am shifting to Redis considering the needs of centralized cache. But I am concerned about its performance as i have seen drastically low…
GP007
  • 691
  • 2
  • 9
  • 24
11
votes
2 answers

Asynchronous Cache Update With Spring Cache Abstraction

Using Spring's caching abstraction, how can I have a cache refresh an entry asynchronously while still returning the old entry? I am trying to use Spring's caching abstraction to create a caching system where after a relatively short "soft" timeout,…
Luke Atkinson
  • 111
  • 1
  • 2
  • 6
11
votes
3 answers

How to have spring cache store the ResponseBody and not the intermediary object

I use spring cache with this method which returns the queried value as JSON: @RequestMapping("/getById") @ResponseBody @Cacheable public HugeValue getHugeValueFromSlowFoo( @RequestParam(value = "id", defaultValue = "") String id ) { return…
Marged
  • 10,577
  • 10
  • 57
  • 99
10
votes
4 answers

Enabling Redis cache in spring boot

I have following configuration on my spring boot project. @SpringBootApplication @EnableTransactionManagement @EnableCaching @EnableScheduling @EnableAsync public class Application { String redisHost = "localhost"; int redisPort = 6379; …
lsc
  • 681
  • 3
  • 8
  • 26
10
votes
2 answers

How to cache 2 different bean methods in same ehCache?

Say I have 2 different bean methods which I want to cache by EhCache: @Component public class StatService { @Cacheable(value = "statCalc") public int getMeth1(int param) { // LOGIC1 } @Cacheable(value = "statCalc") public int getMeth2(int…
corvax
  • 1,095
  • 1
  • 10
  • 35
10
votes
3 answers

spring-boot-devtools causing ClassCastException while getting from cache.

I am facing issue while getting value from cache. java.lang.RuntimeException: java.lang.ClassCastException: com.mycom.admin.domain.User cannot be cast to com.mycom.admin.domain.User Cache…
titogeo
  • 2,156
  • 2
  • 24
  • 41
10
votes
1 answer

How do I use the key, in a condition in Cacheable annotation

I'm caching the results of a function using the @cacheable annotation. I have 3 different caches and the key for each one is the user id of the currently logged in user concatenated with an argument in the method . On a certain event I want to evict…
Nandita Rao
  • 277
  • 1
  • 4
  • 17
10
votes
1 answer

Spring cache constant as a key

As part of my code I have a method with empty parameters. For e..g, public MasterData fetchMasterData() { // DO something. } I want to add a @Cacheable with key as 'masterdata'. I tried the following, but it looks for a bean named 'masterdata'. I…
Poorna Subhash
  • 2,078
  • 2
  • 21
  • 28
10
votes
1 answer

Generating unique cache key with Spring KeyGenerator not working

I'm having the issue when my cache keys are colliding in Spring using the @Cacheable annotation. For instance, with the following two methods: @Cacheable("doOneThing") public void doOneThing(String name) { // do something with…
blacktide
  • 10,654
  • 8
  • 33
  • 53
9
votes
4 answers

Cannot find the cache named xxx for the builder in spring boot application

I have a Spring Boot application where I want to use Spring Boot cache on a repository method. I have specified @EnableCaching annotation in my Spring Boot app. When I try to use @Cacheable annotation on my repository method, it throws…
Priya
  • 1,096
  • 4
  • 15
  • 32
9
votes
3 answers

Caching lookups on application startup doesn't work

I am using Spring Boot 1.5.9 on Tomcat 9.0.2 and I am trying to cache lookups using spring @Cacheable scheduling a cache refresh job that runs on application startup and repeats every 24 hours as follows: @Component public class RefreshCacheJob { …
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
9
votes
2 answers

Spring Caching - ignore parameter for key

I want to cache the results of a simple getter that has an optional parameter (user-agent in the example below). How can I instruct the key to be created without taking into consideration the optional user-agent…
hansi
  • 2,278
  • 6
  • 34
  • 42
9
votes
1 answer

Spring Caching not working for findAll method

I have recently started working on caching the result from a method. I am using @Cacheable and @CachePut to implement the desired the functionality. But somehow, the save operation is not updating the cache for findAll method. Below is the code…
Madhav Pandey
  • 91
  • 1
  • 1
  • 5
9
votes
1 answer

Limit cache size using JCache configuration API

I am using JCache API to configure caches in my application that uses spring caching with Ehcache 3. cacheManager.createCache("users", new MutableConfiguration() .setStoreByValue(false) …
abhijitab
  • 101
  • 1
  • 3
1 2
3
45 46