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

📄 bdbcache.java

📁 高效海量访问系统
💻 JAVA
字号:
package net.sf.hibernate.cache;

import com.cache.behavior.ICache;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import net.sf.hibernate.cache.Timestamper;
import com.cache.bdb.BDBCacheManager;
import com.sleepycat.je.*;

/**
 * Berkeley DB Java Edition plugin for Hibernate
 *
 * Berkeley DB JE is a high performance, transactional storage engine written
 * entirely in Java. This is a plugin for using BDB as a second-level cache in
 * hibernate.
 *
 * @version 1.0 01/21/2005
 */
public class BDBCache
    implements Cache {
  private static final Log log = LogFactory.getLog(BDBCache.class);

  private ICache cache;

  public BDBCache(ICache cache) {
    this.cache = cache;
  }

  /**
   * Gets a value of an object which matches the given key.
   * @param key Object The key of the object to return.
   * @return Object The value placed into the cache with an earlier put, or null if not found
   * @throws CacheException
   */
  public Object get(Object key) throws CacheException {
    if (log.isDebugEnabled()) {
      log.debug("key: " + key);
    }
    if (key == null) {
      return null;
    }
    else {
      Object value = cache.get(key);
      if (value == null) {
        if (log.isDebugEnabled()) {
          log.debug("Value for " + key + " is null");
        }
        return null;
      }
      else {
        return value;
      }
    }
  }

  /**
   * Puts an object into the cache.
   * @param key Object A key
   * @param value Object The value placed into the cache
   * @throws CacheException
   */
  public void put(Object key, Object value) throws CacheException {
    if (log.isDebugEnabled()) {
      log.debug("put object to cache, key: " + key);
    }
    //System.out.println("put an object into bdb cache.... key=" +key + "  value="+value);
    cache.put(key, value);
  }

  /**
   * Removes the object which matches the key.
   * Do nothing if no object matches.
   * @param key Object
   * @throws CacheException
   */
  public void remove(Object key) throws CacheException {
    if (log.isDebugEnabled()) {
      log.debug("remove object from bdb cache, key: " + key);
    }
    cache.remove(key);
  }

  /**
   * Removes all the objects from the BDB cache.
   * @throws CacheException
   */
  public void clear() throws CacheException {
    if (log.isDebugEnabled()) {
      log.debug("remove all the objects in cache...");
    }
    cache.removeAll();
  }

  /**
   * Close BDB environment and databases, and make it unuseable.
   * @throws CacheException
   */
  public void destroy() throws CacheException {
    try {
      BDBCacheManager.getInstance().close();
    }
    catch (DatabaseException ex) {
      throw new CacheException(ex);
    }
  }

  public void lock(Object key) throws CacheException {

  }

  public void unlock(Object key) throws CacheException {

  }

  public long nextTimestamp() {
    return Timestamper.next();
  }

  public int getTimeout() {
    return Timestamper.ONE_MS * 60000; // 60 second lock timeout
  }

}

⌨️ 快捷键说明

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