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.Cacheandorg.springframework.cache.CacheManagerinterfaces.There are a few implementations of that abstraction available out of the box: JDKjava.util.concurrent.ConcurrentMapbased 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 managesCaches and can be used to retrieve these for storage.JDK ConcurrentMap-based Cache
The JDK-basedCacheimplementation resides underorg.springframework.cache.concurrentpackage. It allows one to useConcurrentHashMapas a backingCachestore.<!-- 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 theSimpleCacheManagerto create aCacheManagerfor the two nestedConcurrentMapCacheinstances 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.ehcachepackage. 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 theehcachebean) which is then wired into the dedicatedCacheManagerimplementation. Note the entire ehcache-specific configuration is read fromehcache.xml.Guava Cache
The Guava implementation is located underorg.springframework.cache.guavapackage and provides access to several features of Guava.Configuring aCacheManagerthat 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 GuavaCacheManageralso supports customsCacheBuilderandCacheLoader. 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.jcachepackage.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