Spring Cache internals
Spring Cache provides a lightweight abstraction over caching implementations and is driven by annotations such as @Cacheable, @CachePut, and @CacheEvict.
Spring Cache architecture
The Spring Cache architecture is centered on a few core concepts:
CacheManagerprovides namedCacheinstances.Cacheexposes basic operations:get,put,evict, andclear.CacheInterceptor(orCacheAspectSupport) intercepts method execution and applies caching logic.CacheOperationSourcereads cache-related annotations and buildsCacheableOperationmetadata.CacheResolverchooses the actual cache(s) to use for an operation.SimpleKeyGeneratoror SpEL-based key evaluation computes the cache key from method arguments.
@startuml
class CacheManager {
getCache(name)
}
class Cache {
get(key)
put(key, value)
evict(key)
clear()
}
class "Cache.ValueWrapper" as ValueWrapper {
value
}
class CacheInterceptor {
aroundMethod()
}
class CacheAspectSupport {
handleIntercept()
}
class CacheOperationSource {
getCacheOperations()
}
class CacheResolver {
resolveCaches()
}
class CacheableOperation {
attributes
}
class SimpleKeyGenerator {
generateKey(args)
}
CacheManager --> Cache
Cache --> ValueWrapper
CacheInterceptor --> CacheOperationSource
CacheInterceptor --> CacheResolver
CacheInterceptor --> SimpleKeyGenerator
CacheInterceptor --> Cache
CacheAspectSupport <|-- CacheInterceptor
CacheOperationSource --> CacheableOperation
@enduml
Key flow
CacheInterceptorintercepts the target method.CacheOperationSourcereads the@Cacheable,@CachePut, or@CacheEvictmetadata.CacheResolverselects theCacheinstance(s) fromCacheManager.SimpleKeyGeneratoror key expression evaluates the cache key.- The
Cachechecks for a cachedCache.ValueWrapper. - If a value exists, it returns the cached result; otherwise, the method executes and the result is stored.
This structure makes Spring Cache pluggable, letting the same annotation-driven API work with different cache providers.