Sub topic 2
How Spring's cache abstraction works
This chapter assumes you know basic Spring Boot structure, how beans and dependency injection work, and how to add a dependency with Maven or Gradle. You do not need any caching experience. Later chapters cover the annotations and providers in detail. This one explains the model that sits underneath them.
The problem the abstraction solves
Caches differ a lot. Caffeine keeps entries in the JVM's memory. Redis keeps them in a separate server that many application instances can share. Each has its own API, so code written against one is tied to it.
Spring's cache abstraction puts a small, common interface between your code and the store. You declare what should be cached, and the configured provider decides how to store it. Switching from Caffeine to Redis then becomes a configuration change, not a rewrite of your service classes.
The two core interfaces
The abstraction rests on two types in org.springframework.cache.
Cacheis one named store of key-value pairs. It offers operations such asget,put,evict, andclear.CacheManagercreates and looks upCacheobjects by name. When your code refers to a cache calledproducts, theCacheManagerreturns the matchingCache.
Each provider supplies its own implementations. CaffeineCacheManager wraps Caffeine, and RedisCacheManager wraps Redis. Spring Boot picks one for you through auto-configuration, which later chapters cover.
You can use these interfaces directly when you need to:
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
@Component
public class CacheInspector {
private final CacheManager cacheManager;
public CacheInspector(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
public boolean isCached(Long id) {
Cache cache = cacheManager.getCache("products");
return cache != null && cache.get(id) != null;
}
}getCache returns null when the name is unknown, so the code checks for that. Most application code never does this. It uses annotations instead.
Annotations and the proxy
The annotations, such as @Cacheable, are declarative. You mark a method, and Spring adds the caching behavior around it. It does this with a proxy, which is a generated object that wraps your bean.
Other beans do not receive your ProductService object. They receive the proxy. When a caller invokes a cached method, the proxy runs this sequence:
- Build a cache key from the method arguments.
- Look in the named cache for that key.
- On a hit, return the stored value without running your method.
- On a miss, run your method, store the result, and return it.
Because the proxy sits between the caller and your bean, caching applies only to calls that arrive through it. A later chapter shows what goes wrong when a bean calls its own methods.
Where the pieces connect
The annotations name a cache. The CacheManager resolves that name to a Cache. The Cache talks to the provider.
| Piece | Role | Example |
|---|---|---|
| Annotation | Declares what to cache | `@Cacheable("products")` |
| Proxy | Intercepts calls and applies the logic | Generated by Spring |
| `CacheManager` | Finds a cache by name | `CaffeineCacheManager` |
| `Cache` | Stores and returns entries | One cache named `products` |
| Provider | Holds the data | Caffeine or Redis |
Common errors at this level
- No
CacheManagerbean. Without@EnableCachingand a provider, caching annotations do nothing. Boot falls back to a simple in-memoryConcurrentMapCacheManagerwhen the cache starter is present and no other provider is found. - Unknown cache names. Some providers create caches on demand. Others, when you list
spring.cache.cache-names, accept only those names and fail on others. - Assuming a shared cache. A Caffeine cache lives inside one JVM. Two instances of your application each hold their own copy.
Exercise
A bean calls cacheManager.getCache("orders") and gets null. What does that tell you, and what should the code do?
Show answer
The CacheManager has no cache named "orders". The code should handle the null result, for example by treating it as a miss, and you should check that the name is spelled correctly or is included in the configured cache names.
Exercise
In the proxy sequence, at which step does your own method run, and when is it skipped?
Show answer
Your method runs only on a cache miss, after the lookup fails. On a hit, the proxy returns the stored value and your method is skipped.