Spring Cache Overview

Overview

  • A cache stores frequently used resources with an expiration time after retrieving them. When users request the same resource while the cached value remains valid, the application can use that value and significantly improve query performance.
  • Caching is generally used to reduce DBMS load caused by frequent queries for the same resource.

Spring Cache

  • Spring supports convenient caching features through its cache abstraction.
    • The Spring Cache abstraction lets users apply caching through a public interface without concerning themselves with the cache implementation.
    • More specifically, business logic that needs caching can use an abstract public interface without depending on caching infrastructure such as Ehcache or Redis. This means that switching from Ehcache to Redis does not affect the business logic.
  • With legacy Spring, you can create a CacheManager bean through SimpleCacheManager. With Spring Boot, add the spring-boot-starter-cache artifact to use caching features.
  • If no separate third-party module is present, the ConcurrentMap-based ConcurrentMapCacheManager, which stores values in local memory, is registered automatically as a bean. If you add dependencies such as Ehcache or Redis, you can register and use EhCacheCacheManager or RedisCacheManager as a bean.