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配置JSR 107标准缓存管理API,并展示了如何通过注解实现缓存读取、写入、清除等操作。文中还提供了自定义CacheManager的创建方式。
8127

被折叠的 条评论
为什么被折叠?



