Understanding Caching in Spring: Local vs Distributed Cache and GuavaCacheManager
This article explains the concept and purpose of caching, compares local and centralized cache approaches, introduces Spring's cache support and various CacheManager implementations, details GuavaCacheManager configuration and expiration strategies, demonstrates @Cacheable usage, and discusses common pitfalls such as cache miss due to internal method calls.
Cache is a memory‑based storage used to improve performance by avoiding frequent database reads, which involve slow disk access.
Two main cache types are local cache (stored in the same JVM, e.g., static variables or a locally deployed Redis) and centralized cache (e.g., Redis cluster) that provides high availability and consistency across multiple servers.
Local cache offers low latency but low reliability; centralized cache adds network latency but ensures data persistence and consistency.
Spring provides cache abstraction via CacheManager; commonly used implementations include ConcurrentMapCacheManager, EhCacheCacheManager, GuavaCacheManager, and RedisCacheManager.
GuavaCacheManager uses LRU + ConcurrentHashMap and can be configured in Spring Boot. Example Maven dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>Configuration class:
@EnableCaching
@Configuration
public class GuavaCacheConfig {
@Bean
public CacheManager cacheManager() {
GuavaCacheManager cacheManager = new GuavaCacheManager();
cacheManager.setCacheBuilder(
CacheBuilder.newBuilder().expireAfterWrite(3, TimeUnit.MINUTES));
return cacheManager;
}
}Cache expiration strategies include expireAfterAccess, expireAfterWrite, and refreshAfterWrite.
The @Cacheable annotation can be used to cache method results; its key and value are defined by EL expressions, and the cached value is the method’s return value.
Cache miss may occur when a cached method is called from within the same class because Spring’s AOP proxy is bypassed.
In summary, combining local and centralized caches, configuring GuavaCacheManager, and using Spring’s caching annotations can improve application performance while being aware of pitfalls such as internal method calls that bypass the proxy.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
