Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/cache/TransactionalCache.java
//$Id: TransactionalCache.java 9278 2006-02-13 16:57:22Z steveebersole $
package org.hibernate.cache;

import java.util.Comparator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Support for fully transactional cache implementations like
 * JBoss TreeCache. Note that this might be a less scalable
 * concurrency strategy than <tt>ReadWriteCache</tt>. This is
 * a "synchronous" concurrency strategy.
 *
 @author Gavin King
 */
public class TransactionalCache implements CacheConcurrencyStrategy {

  private static final Log log = LogFactory.getLogTransactionalCache.class );

  private Cache cache;

  public String getRegionName() {
    return cache.getRegionName();
  }

  public Object get(Object key, long txTimestampthrows CacheException {
    if log.isDebugEnabled() ) {
      log.debug"cache lookup: " + key );
    }
    Object result = cache.readkey );
    if log.isDebugEnabled() ) {
      log.debugresult == null "cache miss" "cache hit" );
    }
    return result;
  }

  public boolean put(
      Object key,
          Object value,
          long txTimestamp,
          Object version,
          Comparator versionComparator,
          boolean minimalPutthrows CacheException {
    if minimalPut && cache.readkey != null ) {
      if log.isDebugEnabled() ) {
        log.debug"item already cached: " + key );
      }
      return false;
    }
    if log.isDebugEnabled() ) {
      log.debug"caching: " + key );
    }
    if cache instanceof OptimisticCache ) {
      ( ( OptimisticCache cache ).writeLoadkey, value, version );
    }
    else {
      cache.putkey, value );
    }
    return true;
  }

  /**
   * Do nothing, returning null.
   */
  public SoftLock lock(Object key, Object versionthrows CacheException {
    //noop
    return null;
  }

  /**
   * Do nothing.
   */
  public void release(Object key, SoftLock clientLockthrows CacheException {
    //noop
  }

  public boolean update(
      Object key,
          Object value,
          Object currentVersion,
          Object previousVersionthrows CacheException {
    if log.isDebugEnabled() ) {
      log.debug"updating: " + key );
    }
    if cache instanceof OptimisticCache ) {
      ( ( OptimisticCache cache ).writeUpdatekey, value, currentVersion, previousVersion );
    }
    else {
      cache.updatekey, value );
    }
    return true;
  }

  public boolean insert(
      Object key,
          Object value,
          Object currentVersionthrows CacheException {
    if log.isDebugEnabled() ) {
      log.debug"inserting: " + key );
    }
    if cache instanceof OptimisticCache ) {
      ( ( OptimisticCache cache ).writeInsertkey, value, currentVersion );
    }
    else {
      cache.updatekey, value );
    }
    return true;
  }

  public void evict(Object keythrows CacheException {
    cache.removekey );
  }

  public void remove(Object keythrows CacheException {
    if log.isDebugEnabled() ) {
      log.debug"removing: " + key );
    }
    cache.removekey );
  }

  public void clear() throws CacheException {
    log.debug"clearing" );
    cache.clear();
  }

  public void destroy() {
    try {
      cache.destroy();
    }
    catch Exception e ) {
      log.warn"could not destroy cache", e );
    }
  }

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

  public Cache getCache() {
    return cache;
  }

  /**
   * Do nothing.
   */
  public boolean afterInsert(
      Object key,
          Object value,
          Object versionthrows CacheException {
    return false;
  }

  /**
   * Do nothing.
   */
  public boolean afterUpdate(
      Object key,
          Object value,
          Object version,
          SoftLock clientLockthrows CacheException {
    return false;
  }

  public String toString() {
    return cache + "(transactional)";
  }

}