Cache using @Cacheable annotation in Spring itself......
Spring 3.1 Provides Cache using @Cacheable annotation..
Methods for whom the result is stored into the cache so on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually execute the method. In its simplest form, the annotation declaration requires the name of the cache associated with the annotated method:
@Cacheable("books") public Book findBook(ISBN isbn) {...}
Just like other services in the Spring Framework, the caching service is an abstraction (not a cache implementation) and requires the use of an actual storage to store the cache data - that is, the abstraction frees the developer from having to write the caching logic but does not provide the actual stores. This abstraction is materialized by theorg.springframework.cache.Cache
andorg.springframework.cache.CacheManager
interfaces.There are a few implementations of that abstraction available out of the box: JDKjava.util.concurrent.ConcurrentMap
based caches, EhCache, Gemfire cache,Guava caches and JSR-107 compliant caches.
Configuring the cache storage
Out of the box, the cache abstraction provides several storages integration. To use them, one needs to simply declare an appropriateCacheManager
- an entity that controls and managesCache
s and can be used to retrieve these for storage.JDK ConcurrentMap-based Cache
The JDK-basedCache
implementation resides underorg.springframework.cache.concurrent
package. It allows one to useConcurrentHashMap
as a backingCache
store.<!-- simple cache manager --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="books"/> </set> </property> </bean>The snippet above uses theSimpleCacheManager
to create aCacheManager
for the two nestedConcurrentMapCache
instances named default and books. Note that the names are configured directly for each cache.As the cache is created by the application, it is bound to its lifecycle, making it suitable for basic use cases, tests or simple applications. The cache scales well and is very fast but it does not provide any management or persistence capabilities nor eviction contracts.EhCache-based Cache
The EhCache implementation is located underorg.springframework.cache.ehcache
package. Again, to use it, one simply needs to declare the appropriateCacheManager
:<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/> <!-- EhCache library setup --> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache.xml"/>This setup bootstraps the ehcache library inside Spring IoC (through theehcache
bean) which is then wired into the dedicatedCacheManager
implementation. Note the entire ehcache-specific configuration is read fromehcache.xml
.Guava Cache
The Guava implementation is located underorg.springframework.cache.guava
package and provides access to several features of Guava.Configuring aCacheManager
that creates the cache on demand is straightforward:<bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"/>It is also possible to provide the caches to use explicitly. In that case, only those will be made available by the manager:<bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"> <property name="caches"> <set> <value>default</value> <value>books</value> </set> </property> </bean>The GuavaCacheManager
also supports customsCacheBuilder
andCacheLoader
. See the Guava documentation for more information about those.GemFire-based Cache
GemFire is a memory-oriented/disk-backed, elastically scalable, continuously available, active (with built-in pattern-based subscription notifications), globally replicated database and provides fully-featured edge caching. For further information on how to use GemFire as a CacheManager (and more), please refer to the Spring Data GemFire reference documentation.JSR-107 Cache
JSR-107 compliant caches can also be used by Spring’s caching abstraction. The JCache implementation is located underorg.springframework.cache.jcache
package.Again, to use it, one simply needs to declare the appropriateCacheManager
:<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager" p:cache-manager-ref="jCacheManager"/> <!-- JSR-107 cache manager setup --> <bean id="jCacheManager" .../>
Comments
Post a Comment