package ;
import ;
import ;
import ;
import ;
import ;
/**
* Ehcache缓存管理的初步学习实例
* @author lushuaiyin
*
*/
public class EhcacheManagerTest {
public static cacheManager = null;
private static String configPath="org/base/cache/test/";//配置文件路径,一般会放在源文件夹
private static String CACHE_MYCACHE1="myCache1";//定义文件中配置的缓存
//实例化cacheManager,单例模式
public static CacheManager getCacheManagerInstance(){
if (cacheManager == null) {
URL configUrl=null;
configUrl = ().getResource(configPath);
cacheManager = (configUrl);
}
return cacheManager;
}
public static getCacheManager() {
return getCacheManagerInstance();//单例缓存管理
}
//这个set可以不开放
public static void setCacheManager( cacheManager) {
= cacheManager;
}
//添加新缓存
public static void addCacheByName(String cacheName){
if(cacheName==null||().equals("")){
("cacheName is null");
}else{
if(getCacheManager().getCache(())!=null){
getCacheManager().removeCache(());
}
getCacheManager().addCache(());
(cacheName+ "重新添加");
}
}
//得到cache对象
public static Cache getCacheByName(String cacheName){
Cache cache=null;
if(cacheName==null||().equals("")){
("cacheName is null");
}else{
if(getCacheManager().getCache(())!=null){
cache=getCacheManager().getCache(());
}
}
return cache;
}
//往缓存中添加元素
public static void putElementToCache(String cacheName,String elementKey,Object elementValue){
Cache cache=null;
if(cacheName==null||().equals("")){
("添加缓存元素失败,cacheName is null");
}else if(elementKey==null||elementValue==null){
("添加缓存元素失败,elementKey or elementValue is null");
}else{
if(getCacheByName(())!=null){//缓存存在
cache=getCacheByName(());
}else{//缓存不存在
addCacheByName(());
cache=getCacheByName(());
}
//对cache对象添加Element
Element element=null;
if((())!=null){
(());
}
element=new Element((),elementValue);
(element);
("添加缓存元素:"+elementKey+"成功!");
}
}
//从缓存中获取指定key的值
public static Object getElementValueFromCache(String cacheName,String elementKey){
Object result=null;
Cache cache=null;
if(cacheName==null||().equals("")){
("获取缓存元素失败,cacheName is null");
}else if(elementKey==null){
("获取缓存元素失败,elementKey is null");
}else{
if(getCacheByName(())!=null){//缓存存在
cache=getCacheByName(());
Element element=null;
if((())!=null){
element=(());
if(()==null){
("缓存中"+elementKey+" 的值为空.");
}else{
result=();
}
}else{
("缓存中"+elementKey+" 的Element值为空.");
}
}else{//缓存不存在
("获取缓存元素失败,缓存"+cacheName+" 为空.");
}
}
return result;
}
/**
* 把所有cache中的内容删除,但是cache对象还是保留.
* Clears the contents of all caches in the CacheManager,
* but without removing any caches.
*/
public static void clearAllFromCacheManager(){
if(getCacheManager()!=null){
getCacheManager().clearAll();
("CacheManager was clearAll...");
}
}
/**
* 把所有cache对象都删除。慎用!
* Removes all caches using removeCache(String) for each cache.
*/
public static void removalAllFromCacheManager(){
if(getCacheManager()!=null){
getCacheManager().removalAll();
("CacheManager was removalAll...");
}
}
//不用缓存时,要关闭,不然会占用cpu和内存资源。
public static void shutdownCacheManager(){
if(getCacheManager()!=null){
getCacheManager().shutdown();
("CacheManager was shutdown...");
}
}
//打印方法1,为了测试用
public static void printCache(Cache cache){
("缓存状态: "+().toString());
if(cache==null){
("cache is null,no print info.");
}else if(().toString().equals(Status.STATUS_UNINITIALISED)){
("缓存状态: 未初始化"+().toString());
}else if(().toString().equals(Status.STATUS_SHUTDOWN)){
("缓存状态: 已关闭"+().toString());
}else if(().toString().equals(Status.STATUS_ALIVE)){
if(().size()==0){
(()+" exits,but no value.");
}else{
for(int i=0;i<().size();i++){
Object thekey=().get(i);
Object thevalue=(thekey);
(()+"--"+i+",key:"+()+",value:"+());
}
}
}
}
//打印方法2,为了测试用
public static void printCacheByName(String cacheName){
if(cacheName==null||().equals("")){
("cacheName is null,no print info.");
}else{
if(getCacheManager().getCache(())!=null){
Cache cache=getCacheManager().getCache(());
printCache(cache);
}else{
(cacheName+" --null");
}
}
}
public static void main(String[] sdfsf){
Cache cache1=(EhcacheManagerTest.CACHE_MYCACHE1);
printCache(cache1);
(EhcacheManagerTest.CACHE_MYCACHE1, "111", "111haah");
(EhcacheManagerTest.CACHE_MYCACHE1, "222", "222haah");
(EhcacheManagerTest.CACHE_MYCACHE1, "333", "333haah");
printCache(cache1);
(EhcacheManagerTest.CACHE_MYCACHE1, "111", "111的新值。");
((EhcacheManagerTest.CACHE_MYCACHE1, "111"));
printCache(cache1);
clearAllFromCacheManager();
printCache(cache1);
removalAllFromCacheManager();
printCache(cache1);
shutdownCacheManager();
}
/*打印
缓存状态: STATUS_ALIVE
添加缓存元素:111成功!
添加缓存元素:222成功!
添加缓存元素:333成功!
缓存状态: STATUS_ALIVE
添加缓存元素:111成功!
111的新值。
缓存状态: STATUS_ALIVE
CacheManager was clearAll...
缓存状态: STATUS_ALIVE
CacheManager was removalAll...
缓存状态: STATUS_SHUTDOWN
CacheManager was shutdown...
*/
}