java 緩存jcs:
有時候我們需要頻繁的訪問數據庫獲得某些數據,這樣大大的增加了訪問數據庫方面的開銷,降低了系統的性能。
一 解決辦法:
將數據放進緩存中。
二 適用條件:
1 需要經常使用
2 數據不經常更新
三 需要導入jar包:
jcs-1.3.jar
四 流程嚮導:
五 代碼演示:
package jcsCache;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.admin.CacheElementInfo;
import org.apache.jcs.admin.CacheRegionInfo;
import org.apache.jcs.admin.JCSAdminBean;
import org.apache.jcs.engine.control.CompositeCache;
/**
*
* cache jcs 練習
* <功能詳細描述>
*
* @version [版本號, 2014-2-7]
* @see [相關類/方法]
* @since [產品/模塊版本]
*/
public class CacheUtil implements Serializable
{
private static final long serialVersionUID = 1L;
/**
* 獲取cache
* <功能詳細描述>
* @param cacheName
* @return JCS
* @see [類、類#方法、類#成員]
*/
public static JCS getCache(String cacheName)
{
JCS cache = null;
try
{
cache = JCS.getInstance(cacheName);
}
catch (CacheException e)
{
e.printStackTrace();
}
return cache;
}
/**
* 獲取cache 中key對應的value
* <功能詳細描述>
* @param cacheName
* @param key
* @return Object
* @see [類、類#方法、類#成員]
*/
public static Object getObject(String cacheName, Object key)
{
Object obj = null;
JCS cache = getCache(cacheName);
if (null != cache)
{
obj = cache.get(key);
}
if (null == obj)
{
//obj = DatabaseManager.getByKey(key);
obj = "from data base";
}
return obj;
}
/**
* 將對象放進緩存
* <功能詳細描述>
* @param cacheName
* @param key
* @param value
* @return boolean
* @see [類、類#方法、類#成員]
*/
public static boolean putObject(String cacheName, Object key, Object value)
{
JCS cache = getCache(cacheName);
boolean result = false;
if (null != cache)
{
try
{
cache.put(key, value);
result = true;
}
catch (CacheException e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 清除所有緩存
* <功能詳細描述>
* @param cacheName
* @return
* @see [類、類#方法、類#成員]
*/
public static boolean cleanAll(String cacheName)
{
JCS cache = getCache(cacheName);
boolean result = false;
if (null != cache)
{
try
{
cache.clear();
result = true;
}
catch (CacheException e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 根據key清除對應的緩存
* <功能詳細描述>
* @param cacheName
* @param key
* @return
* @see [類、類#方法、類#成員]
*/
public static boolean cleanByKey(String cacheName, Object key)
{
JCS cache = getCache(cacheName);
boolean result = false;
if (null != cache)
{
try
{
cache.remove(key);
result = true;
}
catch (CacheException e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 獲取某個緩存的元數據
* <功能詳細描述>
* @param cacheName
* @return CacheElementInfo
* @see [類、類#方法、類#成員]
*/
public static CacheElementInfo getMetadataByCacheName(String cacheName)
{
JCSAdminBean admin = new JCSAdminBean();
CacheElementInfo info = null;
LinkedList<?> linkedList = null;
try
{
linkedList = admin.buildElementInfo(cacheName);
ListIterator<?> iterator = linkedList.listIterator();
while (iterator.hasNext())
{
info = (CacheElementInfo)iterator.next();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return info;
}
/**
* 獲取緩存元數據
* <功能詳細描述>
* @return
* @see [類、類#方法、類#成員]
*/
public static List<CompositeCache> getMetadata()
{
JCSAdminBean admin = new JCSAdminBean();
LinkedList<?> linkedList = null;
CacheRegionInfo info = null;
CompositeCache compCache = null;
List<CompositeCache> list = new ArrayList<CompositeCache>();
try
{
linkedList = admin.buildCacheInfo();
ListIterator<?> iterator = linkedList.listIterator();
while (iterator.hasNext())
{
info = (CacheRegionInfo)iterator.next();
compCache = info.getCache();
list.add(compCache);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
}