JSR107 中的 缓存机制,及spring集成

Spring 配置:

<beans>
 <context:annotation-config/>
 <jcache-spring:annotation-driven proxy-target-class="true"/>
 <bean id="cacheManager" factory-method="getCacheManager" />
</beans>

maven:
<dependency>
     <groupId>javax.cache</groupId>
     <artifactId>cache-api</artifactId>
     <version>0.3</version>
</dependency>

注解使用:
public class BlogManager {
 
 @CacheResult(cacheName="blogManager")
 public Blog getBlogEntry(String title) {...}
  
 @CacheRemoveEntry(cacheName="blogManager")
 public void removeBlogEntry(String title) {...}
  
 @CacheRemoveAll(cacheName="blogManager")
 public void removeAllBlogs() {...}
 
 @CachePut(cacheName="blogManager")
 public void createEntry(@CacheKeyParam String title, @CacheValue Blog blog) {...}
  
 @CacheResult(cacheName="blogManager")
 public Blog getEntryCached(String randomArg, @CacheKeyParam String title){...}
}
 JSR中最基本缓存注解:
  • @CacheResult – 使用缓存
  • @CachePut – 保存缓存
  • @CacheRemoveEntry – 从缓存中删除单条记录
  • @CacheRemoveAll – 删除缓存中的所有记录

自定义CacheManager:

CacheManagers can have names and classloaders configured in. e.g.
 
    CacheManager cacheManager =
     Caching.getCacheManager("app1", Thread.currentThread().getContextClassLoader());
 
Implementations may also support direct creation with new for maximum flexibility:
 
    CacheManager cacheManager =
        new RICacheManager("app1", Thread.currentThread().getContextClassLoader());
 
CacheManager可以通过下面的方式来配置名称和classloader:
 
    CacheManager cacheManager =
        new RICacheManager("app1", Thread.currentThread().getContextClassLoader());

 

String className = "javax.cache.implementation.RIServiceProvider";
Class<ServiceProvider> clazz =
(Class<ServiceProvider>)Class.forName(className);
ServiceProvider provider = clazz.newInstance();
return provider.createCacheManager(Thread.currentThread().getContextClassLoader(), "app1");

 

你可能感兴趣的:(spring)