⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachefactory.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package com.jdon.controller.cache;

import com.jdon.util.Debug;
import java.util.*;

/**
 * 本缓存工厂是以Model为基本对象的缓存系统。
 * 特点有两个:
 * 1. 缓存的Key必须由本类产生
 * 2. 本类的remove将自动清除与该Model相关的所有缓存对象。
 *
 * 使用工厂模式,以便将来更换更好的Cache
 *
 * 凡是需要被缓存的对象,需要两步:
 * 1. 在cacheTypes增加相应名称,
 * 2. 提供获取该对象Key的名称
 *
 *
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p></p>
 * @author banq
 * @version 1.0
 */
public class CacheFactory implements org.picocontainer.Startable {
  public  static String module = CacheFactory.class.getName();

  /**cache的大小由配置文件config.xml定义 */
  private Cache cache;

  /** 将CacheKey和DataKey 对应关系保存起来
   *  可以根据DataKey寻找到不同的CacheKey
   *  可以清除DataKey有关的所有缓存
   *
   *  例如:1875 对应两种缓存:Model和Image,
   *  以1875为Key,可以清除它的所有缓存。
   *
   * */
  private Map cacheKeyMap;

  /**
   * 用于EJB容器
   * @return CacheFactory
   */
  public  CacheFactory() {
    cache = new LRUCache();
    cacheKeyMap = new HashMap();
  }

  public void start() {
    Debug.logVerbose("CacheFactory start .....", module);
    cache.clear();
    cacheKeyMap.clear();
  }

  public void stop() {
    if (cache != null)
      cache.clear();
    cache = null;
    if (cacheKeyMap != null)
      cacheKeyMap.clear();
    cacheKeyMap = null;
  }


  /**
   * 从缓存中获得缓存
   * @param cacheKey
   * @return
   */
  public Object getObect(StringKey key) {
    Debug.logVerbose("<-cache->try to get cache: " + key, module);
    Object o = cache.get(key.toString());
    if (o != null)
      Debug.logVerbose("<-cache->got it", module);
    return o;
  }

  /**
   * 保存到缓存中
   * @param cacheKey
   * @param value
   */
  public void putObect(StringKey key, Object value) {
   if (!cache.contain(key.toString())){
     cache.put(key.toString(), value);
     Debug.logVerbose("<-cache->save cache: " + key + ", size:" + cache.size(),
                      module);
   }
  }

  /**
   * 清除缓存
   * @param cacheKey
   */
  public void removeObect(StringKey key) {
    cache.remove(key.toString());
    Debug.logVerbose("<-cache->remove the object of " + key + " from cache",
                     module);
  }

  /**
   * 清除缓存中该dataKey的相关所有缓存数据
   * 当该dataKey相关的数据增删改时,调用本方法。以便及时清除缓存。
   *
   * dataKey是数据的ID,如ProductId , ItemId等
   * @param dataKey
   * @param formName
   */
  public void removeCache(Object dataKey, CacheKeyFactory cacheKeyFactory) {
    if (dataKey == null)
      return;

    String cacheKeyStr = null;

    Iterator iter = cacheKeyFactory.getAllCacheKey(dataKey).iterator();
    while (iter.hasNext()) {
      cacheKeyStr = (String) iter.next();
      Debug.logVerbose("<-cache->remove cache:" + cacheKeyStr, module);
      removeObject(cacheKeyStr, cacheKeyFactory);
    }
  }

  private void removeObject(String cacheKeyStr, CacheKeyFactory cacheKeyFactory) {
    try {
      cache.remove(cacheKeyStr);
      cacheKeyFactory.removeCacheKey(cacheKeyStr);
    } catch (Exception e) {
      Debug.logError(" remove cache error:" + e + " cacheKey=" + cacheKeyStr,
                     module);
    }
  }

  public Map getCacheKeyMap() {
    return cacheKeyMap;
  }

  public void setCacheKeyMap(Map cacheKeyMap) {
    this.cacheKeyMap = cacheKeyMap;
  }

  public Cache getCache() {
    return cache;
  }

  public void setCache(Cache cache) {
    this.cache = cache;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -