首页>代码>Spring Hibernate Spring Data MongoDB Akka Titles Bootstrap JQuery Maven 大全>/ykameshrao-spring-hibernate-springdata-springmvc-maven-project-framework-6ed60ce/commons/src/main/java/com/yourpackagename/commons/util/CacheManager.java
package com.yourpackagename.commons.util;

import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Provides common methods to manage a JCS based cache region.
 *
 * @author Y Kamesh Rao
 */
public class CacheManager {

    private static Logger log = LoggerFactory.getLogger(CacheManager.class);

    /**
     * Fetches a cache region specified in the config file. Returns null if the region is not found or if the config
     * file is bad.
     *
     * @param configFilePath The file path for .ccf file
     * @param regionName     Name of cache region
     * @return JCS    A cache region.
     */

    public static JCS initCacheRegion(String configFilePath, String regionName) throws CacheException {
        JCS cache = null;
        // Set the config file
        try {
            if (configFilePath != null)
                JCS.setConfigFilename(configFilePath);
        } catch (Exception e) {
            // Trace the exception
            log.error(e.getMessage());
            // Throw a new sophisticated exception
            throw new CacheException("Could not set cache config file.\n");
        }

        log.info("Successfully set config file to::" + configFilePath);

        // Fetch the region
        try {
            cache = JCS.getInstance(regionName);
        } catch (Exception e) {
            // Trace the exception
            log.error(e.getMessage());
            // Throw a new sophisticated exception
            throw new CacheException("Could not fetch the cache region::" + regionName + ".\n");
        }

        log.info("Successfully fetched the cache region::" + regionName);
        return cache;
    }


    /**
     * Adds an object to cache with its key.
     *
     * @param cache  The JCS cache object
     * @param key    Key to be used for identification
     * @param object object to be cached
     * @throws CacheException: thrown if the cache instance is found to be null
     */
    public static void addToCache(JCS cache, String key, Object object) throws CacheException {
        if (cache == null)
            throw new CacheException("Null cache");
        cache.put(key, object);
    }


    /**
     * Fetches an object from cache.
     *
     * @param cache The JCS cache object
     * @param key   Key to be used for identification
     * @return object Corresponding cached object. Null if not found.
     * @throws CacheException: thrown if the cache instance is found to be null
     */
    public static Object getFromCache(JCS cache, String key) throws CacheException {
        if (cache == null)
            throw new CacheException("Null cache");
        return cache.get(key);
    }


    /**
     * Flushes the specified cache
     *
     * @param cache Cache instance to be cleared
     * @throws CacheException
     */
    public static void clearCache(JCS cache) throws CacheException {
        if (cache == null)
            throw new CacheException("Null cache");
        cache.clear();
    }

}
最近下载更多
13043860zj  LV16 2020年9月3日
码码码  LV5 2019年4月22日
50636315  LV2 2017年9月30日
ykcro123  LV11 2017年3月2日
刻下来的幸福  LV2 2017年2月5日
zhoushi18  LV7 2016年7月18日
chenda  LV4 2016年6月22日
zang1314  LV2 2016年6月21日
lcjz99  LV11 2016年4月14日
abingagl  LV8 2016年1月24日
最近浏览更多
Dominick  LV14 2023年6月19日
irivn007  LV15 2022年12月24日
Add_Wa  LV1 2022年11月18日
Super强  LV13 2022年10月21日
291373020  LV7 2022年5月31日
fengshengtian  LV8 2022年2月26日
lingtiejing  LV15 2022年2月23日
1615651451  LV2 2021年12月7日
weixiao  LV6 2021年11月8日
1872615558  LV13 2021年11月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友