在项目中引入jcs缓存,我想当内存中缓存数超过指定的最大数之后,就把一些缓存保存在磁盘中,但是本地测试一直都没有实现
配置如下:
jcs.region.customerchannel=disk
jcs.region.customerchannel.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
#为了测试,设的小一点
jcs.region.customerchannel.cacheattributes.MaxObjects=4
jcs.region.customerchannel.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCachejcs.region.customerchannel.UseMemoryShrinker=true
#最开始是设成180,没效果,查了官方文档,好像要设成-1,还是没效果
jcs.region.customerchannel.MaxMemoryIdleTimeSeconds=-1
# 设定 shinker 执行时间间隔,为了测试,设的小一点
jcs.region.customerchannel.ShrinkerIntervalSeconds=60jcs.region.customerchannel.elementattributes=org.apache.jcs.engine.ElementAttributes
#开启磁盘缓存,默认为true
jcs.region.customerchannel.elementattributes.IsSpool=true
#关闭远程缓存,默认为true
jcs.region.customerchannel.elementattributes.IsRemote=false
#关闭横向式的并行缓冲,默认为true
jcs.region.customerchannel.elementattributes.IsLateral=false# 设定让 element 会「过期」  单词Eternal(永久的意思)
jcs.region.customerchannel.elementattributes.IsEternal=false
# 设定 element 建立后能存活多久,IsEternal=false时有效,设为24个小时,启动spring的quartz每天凌晨清空所有缓存
jcs.region.customerchannel.elementattributes.MaxLifeSeconds=86400
# 设定 element 可闲置的时间,IsEternal=false时有效,为了测试,设的小一点
jcs.region.customerchannel.elementattributes.IdleTime=180
###################################################################################
################################# 磁盘缓存 ##########################################磁盘缓存,会生成一个.data文件和一个.key文件,key中保存对象在.data中的开始位置
jcs.auxiliary.disk=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
jcs.auxiliary.disk.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
#磁盘缓存的前端内存缓存,在JCS写磁盘时,会把对象同进写入叫purgatory的内存区和磁盘,写入磁盘的过程是异步的。从磁盘取对象是先在purgatory内存区中找,找到了就不会去再查数据文件了
jcs.auxiliary.disk.attributes.MaxPurgatorySize=1000
#磁盘缓存对象个数
jcs.auxiliary.disk.attributes.MaxKeySize=1000
jcs.auxiliary.disk.attributes.DiskPath=d://cache
是我哪里配置有问题吗,还是别的原因?

解决方案 »

  1.   


    看看你是不是测试时的一些运用上的问题。。导致没有效果呢、?闲置内存对象的过期时间
    jcs.region.customerchannel.MaxMemoryIdleTimeSeconds=-1这里设置-1,我也不太清楚代表什么意思。。由于没有去看官方说明
      

  2.   

    http://wenku.baidu.com/view/5b44718102d276a200292e72.html建议再多看看资料
      

  3.   

    运用发面没有涉及到这些,我贴下代码缓存基类public abstract class BaseJCSCache implements Serializable {
    private static final long serialVersionUID = -7120182423259357372L; protected final Log log = LogFactory.getLog(this.getClass());

    private boolean isStartCache;  //是否启动缓存
    private String cacheName;
    private String cacheConfigName;

    private JCS cache;

    /**
     * 初始化
     */
    public void init() {
    try {
    JCS jcs = JCS.getInstance(cacheConfigName);
    log.debug(String.format("创建<%s>缓存仓库,缓存配置<%s>", getCacheName(), getCacheConfigName()));
    if(jcs != null) {
    setCache(jcs);
    }
    } catch (CacheException e) {
    log.debug(String.format("创建<%s>缓存失败", getCacheName()));
    e.printStackTrace();
    }
    }

    /**
     * 
     * @param key
     * @return
     */
    public Object getCache(String key) {
    if(!isStartCache()) {
    return null;
    }
    JCS cache = getCache();
    if(cache == null) {
    log.debug(String.format("保存<%s>缓存失败:缓存容器未创建成功", getCacheName()));
    return null;
    }
    synchronized(cache) {
    logGetCache(key);
    return cache.get(key);
    }
    }

    /**
     * 
     * @param dataKey
     */
    public void logGetCache(String key) {
    log.debug(String.format("<%s>获取缓存:%s", getCacheName(), key));
    }

    /**
     * 
     * @param key
     * @param value
     */
    public void putCache(String key, Object value) {
    if(!isStartCache()) {
    return;
    }
    JCS cache = getCache();
    if(cache == null) {
    log.debug(String.format("保存<%s>缓存失败:缓存容器未创建成功", getCacheName()));
    return;
    }
    synchronized(cache) {
    logPutCache(key, value);
    try {
    cache.put(key, value);
    } catch (CacheException e) {
    log.debug(String.format("保存<%s>缓存失败:%s, %s", getCacheName(), key, value.toString()));
    }
    }
    }

    /**
     * 
     * @param key
     * @param value
     */
    public void logPutCache(String key, Object value) {
    log.debug(String.format("<%s>放入缓存:%s, 数据数%s", getCacheName(), key, value.toString()));
    }

    /**
     * 
     */
    public void cleanCache() {
    JCS cache = getCache();
    if(cache != null) {
    synchronized(cache) {
    try {
    cache.clear();
    } catch (CacheException e) {
    log.debug(String.format("<%s>清除缓存失败", getCacheName()));
    }
    }
    }
    }

    /**
     * 设置缓存容器
     * @param jcs
     */
    public void setCache(JCS jcs) {
    this.cache = jcs;
    }

    /**
     * 获取缓存容器
     * @return
     */
    public JCS getCache() {
    return cache;
    }

    /**
     * @return the isCache
     */
    public boolean isStartCache() {
    return isStartCache;
    } /**
     * @param isCache the isCache to set
     */
    public void setStartCache(boolean isStartCache) {
    this.isStartCache = isStartCache;
    }

    /**
     * @return the cacheName
     */
    public String getCacheName() {
    return cacheName;
    } /**
     * @param cacheName the cacheName to set
     */
    public void setCacheName(String cacheName) {
    this.cacheName = cacheName;
    }

    /**
     * @return the cacheConfigName
     */
    public String getCacheConfigName() {
    return cacheConfigName;
    } /**
     * @param cacheConfigName the cacheConfigName to set
     */
    public void setCacheConfigName(String cacheConfigName) {
    this.cacheConfigName = cacheConfigName;
    }

    }缓存实例:public class CustmerChannelJCSCache extends BaseJCSCache {
    private static final long serialVersionUID = -988452178170932521L;

    }
    项目中运用:....
    //读取缓存
    if(custmerChannelJCSCache != null) {
    Object cacheData = custmerChannelJCSCache.getCache("customerchannel/internetStatByArea/" + areaIdStr + "-" + areaSubIdStr + "-" + monthStr);
    if(cacheData != null) {
    return (List)cacheData;
    }
    }....//放入缓存
    if(custmerChannelJCSCache != null) {
    custmerChannelJCSCache.putCache("customerchannel/internetStatByArea/" + areaIdStr + "-" + areaSubIdStr + "-" + monthStr, datas);
    }
    ...