You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ma...@apache.org on 2003/07/01 23:08:27 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/cache ObjectCacheUnlimitedImpl.java ObjectCachePerClassImpl.java

mattbaird    2003/07/01 14:08:27

  Added:       src/java/org/apache/ojb/broker/cache
                        ObjectCacheUnlimitedImpl.java
                        ObjectCachePerClassImpl.java
  Log:
  two new cache types
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCacheUnlimitedImpl.java
  
  Index: ObjectCacheUnlimitedImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.util.logging.Logger;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * Created by IntelliJ IDEA.
   * User: matthew.baird
   * Date: Jun 23, 2003
   * Time: 1:24:37 PM
   * To change this template use Options | File Templates.
   */
  public class ObjectCacheUnlimitedImpl implements ObjectCache
  {
  	/**
  	 * the hashtable holding all cached object
  	 */
  	protected static Map objectTable = new HashMap();
  
  	/**
  	 * public Default Constructor
  	 */
  	public ObjectCacheUnlimitedImpl(PersistenceBroker broker)
  	{
  	}
  
  	/**
  	 * Clear ObjectCache. I.e. remove all entries for classes and objects.
  	 */
  	public void clear()
  	{
  		objectTable.clear();
  	}
  
  	/**
  	 * Makes object persistent to the Objectcache.
  	 * I'm using soft-references to allow gc reclaim unused objects
  	 * even if they are still cached.
  	 */
  	public void cache(Identity oid, Object obj)
  	{
  		if ((obj != null))
  		{
  			objectTable.put(oid, obj);
  		}
  	}
  
  	/**
  	 * Lookup object with Identity oid in objectTable.
  	 * Returns null if no matching id is found
  	 */
  	public Object lookup(Identity oid)
  	{
  		return objectTable.get(oid);
  	}
  
  	/**
  	 * Removes an Object from the cache.
  	 */
  	public void remove(Identity oid)
  	{
  		if (oid != null)
  		{
  			objectTable.remove(oid);
  		}
  	}
  
  	public void finalize()
  	{
  		Logger logger = LoggerFactory.getDefaultLogger();
  		logger.debug(this.toString());
  	}
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/cache/ObjectCachePerClassImpl.java
  
  Index: ObjectCachePerClassImpl.java
  ===================================================================
  package org.apache.ojb.broker.cache;
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.Identity;
  
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Iterator;
  
  /**
   * Created by IntelliJ IDEA.
   * User: matthew.baird
   * Date: May 21, 2003
   * Time: 12:18:27 PM
   * To change this template use Options | File Templates.
   */
  public class ObjectCachePerClassImpl extends AbstractMetaCache
  {
      private static Map cachesByClass = new HashMap();
  
      /**
       * Constructor for the MetaObjectCachePerClassImpl object
       */
      public ObjectCachePerClassImpl(PersistenceBroker broker)
      {
          setClassCache(Object.class, new ObjectCacheDefaultImpl(broker));
      }
  
      public ObjectCache getCache(Identity oid, Object obj, int methodCall)
      {
          return getCachePerClass(oid.getObjectsClass());
      }
  
      /**
       * Clears the cache
       */
      public void clear()
      {
          Iterator it = cachesByClass.values().iterator();
          while (it.hasNext())
          {
              ObjectCache cache = (ObjectCache) it.next();
              if (cache != null)
              {
                  cache.clear();
              }
          }
      }
  
      /**
       * Sets the ObjectCache implementation to use for objects with the given
       * type and subclasses
       *
       * @param objectClass  The object's class, use java.lang.Object to alter
       *                     default caching for all objects which have no special
       *                     caching defined
       * @param cache        The new ObjectCache implementation to use for this
       *                     class and subclasses, null to switch off caching
       *                     for the given class
       */
      public void setClassCache(Class objectClass, ObjectCache cache)
  
      {
          setClassCache(objectClass.getName(), cache);
      }
  
      /**
       * Sets the ObjectCache implementation for the given class name
       *
       * @param className  The name of the class to cache
       * @param cache      The ObjectCache to use for this class and subclasses
       */
      private void setClassCache(String className, ObjectCache cache)
      {
          cachesByClass.put(className, cache);
      }
  
      /**
       * Gets the cache for the given class
       *
       * @param objectClass  The class to look up the cache for
       * @return             The cache
       */
      private ObjectCache getCachePerClass(Class objectClass)
      {
          ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
          if (cache == null)
          {
  			cache = new ObjectCacheDefaultImpl(null);
  			setClassCache(objectClass.getName(), cache);
          }
          return cache;
      }
  }