java 缓存jcs

来源:互联网 发布:女主播播放软件 编辑:程序博客网 时间:2024/06/10 14:49

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;    }}





0 0