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

How to disable Redis cache with Spring Boot?

With Spring Boot 2.1 I am defining a RedisCacheManager bean in a configuration file, with Java configuration. Everything works correctly but I would like sometimes to disable it, for instance in the tests. Spring Boot provides the…
6
votes
2 answers

set expire key at specific time when using Spring caching with Redis

I am spring caching abstraction annotations to apply caching to my service methods. Since I am using Redis as the cache store, I want to use the option of expiring cache at a specific time, since that is supported by Redis. expireat command in redis…
juser
  • 359
  • 1
  • 7
  • 17
6
votes
2 answers

cannot access net.sf.ehcache.CacheManager, class file for net.sf.ehcache.CacheManager not found

I have been implementing some caching in my project using EhCache. I have added following dependencies to my pom.xml
vigamage
  • 1,975
  • 7
  • 48
  • 74
6
votes
3 answers

Using multiple cache implementations with Spring Cache

I'm working on a Spring Boot app where I need to use both distributed (e.g. Hazelcast) and local (e.g. Guava) caches. Is there a way to configure Spring Cache to use both when using @Cacheable and decide which implementation is needed based on the…
Balazs
  • 185
  • 1
  • 3
  • 10
6
votes
2 answers

Spring Cache - Create custom CacheManager

I'm using Spring Boot and EhCache to develop a calendar application. I'm trying to cache the following method: @Override @Cacheable(value = "concerts") public List getEvents(String eventsForUser, Date startDate, Date endDate) throws Exception…
Oleg
  • 2,984
  • 8
  • 43
  • 71
6
votes
1 answer

Spring caching / spring repository, evict multiple keys

I have two methods to fetch an entity with two different parameters. I also have a save method that uses one of those parameters. How can I evict the entity from the cache under both fetch keys? e.g. see below: @Cacheable public User…
Saul
  • 81
  • 1
  • 5
6
votes
1 answer

How to make a list thread-safe for serialization?

I am using a ThreadSafeList and I am getting great mileage out of it for streaming data from a process to a webserver and then streaming the data back out as it comes in to a client. In memory I am saving the data in the JVM with Spring Caching…
6
votes
1 answer

Spring's AspectJ-mode caching versus AspectJ-mode transactions

My question relates to Spring's AspectJ mode and especially how to enable it for: Transaction management Caching 1) I noticed that in order to enable the AspectJ mode for transaction management, I only had to do the…
balteo
  • 23,602
  • 63
  • 219
  • 412
5
votes
3 answers

Hibernate cache vs spring cache

Can anybody explain what is the difference between hibernate second level cache vs spring cache.? Does it make sense to use both in single application? If it is not recommend then when to use which one? Appreciated if someone give real life Scenario…
5
votes
1 answer

Spring Data Cache + Redis: How to avoid storing class names in serialized JSON which waste lots of space?

I am using Spring Data Cache using Redis as the cache manager. The config is like: return RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults( RedisCacheConfiguration.defaultCacheConfig() …
ch271828n
  • 15,854
  • 5
  • 53
  • 88
5
votes
1 answer

How to use cache on controller/service in spring boot?

I'm trying to add caching on my spring-boot app by following instruction on https://www.java4s.com/spring-boot-tutorials/how-to-configure-cache-in-spring-boot-applications/, but it doesn't work. I'm not entirely sure how to test it. I have a…
Chandara Chea
  • 289
  • 3
  • 11
5
votes
2 answers

Order of annotation processing in Spring (@Cacheable and @Timed)

I want to annotate my method with @Cacheable and also with @Timed (from micrometer). But I would like the @Timed to be applied only in case when the data is not timed. Is there a way to do that, is it enough to put the annotations in correct order -…
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
5
votes
2 answers

How to detect if @Cacheable method output comes from cache?

There is a method annotated with @Cacheable that returns an object: @Service public class UserService { @Cacheable("userData") public UserData getUserData(String userName) { UserData userData = new UserData(); …
Politechniczny
  • 453
  • 3
  • 13
5
votes
1 answer

How to create a secondary CacheManager without overriding default spring-cache

I have a default redis cache configuration in my application.yml: cache: type: redis redis: time-to-live: 7200000 # 2 hour TTL - Tune this if needed later redis: host: myHost port: myPort password: myPass ssl: true …
5
votes
2 answers

Spring Boot: Overriding CacheManager bean makes cache related properties not work

I have a Spring Boot 2 application with Redis cache. It worked just fine until I overridden CacheManager bean. Problem: The following configuration property gets ignored (I can't turn off caching anymore): spring.cache.type=none Although according…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148