Javax.cache

创建一个CacheManager

CacheManager cacheManager = Caching.getCacheManager();


它返回一个default 单例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());

 

创建缓存

cacheManager = getCacheManager();
CacheConfiguration cacheConfiguration = cacheManager.createCacheConfiguration();
cacheConfiguration.setReadThrough(true);
Cache testCache = cacheManager.createCacheBuilder("testCache")
 .setCacheConfiguration(cacheConfiguration).build();

 

获取缓存引用

Cache<Integer, Date> cache = cacheManager.getCache("testCache");

 

基本缓存操作

把数据存入缓存 

Cache<Integer, Date> cache = cacheManager.getCache(cacheName);
Date value1 = new Date();
Integer key = 1;
cache.put(key, value1);


从缓存中获取数据

Cache<Integer, Date> cache =
 cacheManager.getCache(cacheName);
Date value2 = cache.get(key);


从缓存中删除

Cache<Integer, Date> cache =
 cacheManager.getCache(cacheName);
Integer key = 1;
cache.remove(key);


最后欢迎大家访问我的个人网站:1024s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值