You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by dl...@apache.org on 2001/06/09 02:12:56 UTC

cvs commit: jakarta-turbine/src/java/org/apache/turbine/util BufferCache.java

dlr         01/06/08 17:12:56

  Modified:    src/java/org/apache/turbine/util BufferCache.java
  Log:
  * Leveraged the new SequencedHashtable.ELDEST_INDEX constant and
  assertReasonableSize(int) class method.
  
  * Hand-merged in patch by Leoanrd Richardson <le...@collab.net>
  which makes a BufferCache resizable.
  
  Revision  Changes    Path
  1.4       +54 -5     jakarta-turbine/src/java/org/apache/turbine/util/BufferCache.java
  
  Index: BufferCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/util/BufferCache.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BufferCache.java	2001/05/20 02:41:07	1.3
  +++ BufferCache.java	2001/06/09 00:12:54	1.4
  @@ -57,11 +57,12 @@
   import java.util.Hashtable;
   
   /**
  - * A fixed length object cache implementing the LRU algorithm.  Convenient for
  - * buffering recently used objects.
  + * A resizably fixed length object cache implementing the LRU
  + * algorithm.  Convenient for buffering recently used objects.
    *
    * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  - * @version $Id: BufferCache.java,v 1.3 2001/05/20 02:41:07 dlr Exp $
  + * @author <a href="mailto:leonardr@collab.net">Leonard Richardson</a>
  + * @version $Id: BufferCache.java,v 1.4 2001/06/09 00:12:54 dlr Exp $
    */
   public class BufferCache extends SequencedHashtable
   {
  @@ -87,15 +88,51 @@
       /**
        * Creates a new instance with the specified storage buffer pre-allocated.
        *
  -     * @param maxSize The maximum size of the cache.
  +     * @param maxSize The maximum size of the cache (must be greater
  +     *                than zero).
        */
       public BufferCache (int maxSize)
       {
           super(maxSize);
  +        assertReasonableMaxSize(maxSize);
           this.maxSize = maxSize;
       }
   
       /**
  +     * Returns the current maximum size of the storage buffer.
  +     *
  +     * @return The current maximum size of the storage buffer.
  +     */
  +    public int getMaxSize()
  +    {
  +        return maxSize;
  +    }
  +
  +    /**
  +     * Changes the maximum size of the storage buffer.
  +     *
  +     * @param newMaxSize The new maximum size of the storage buffer.
  +     */
  +    public synchronized void resize (int newMaxSize)
  +    {
  +        assertReasonableMaxSize(newMaxSize);
  +
  +        // If the cache is shrinking, we may currently be storing more
  +        // entires than will fit in a cache of the new size.  If this is
  +        // so, then remove entries until we have as many as we can hold.
  +        if (newMaxSize > maxSize)
  +        {
  +            int actualSize = keySet().size();
  +            while (actualSize > newMaxSize)
  +            {
  +                remove(ELDEST_INDEX);
  +                actualSize--;
  +            }
  +        }
  +        maxSize = newMaxSize;
  +    }
  +
  +    /**
        * Stores the provided key/value pair, freshening its list index if the
        * specified key already exists.
        *
  @@ -111,7 +148,7 @@
           {
               // Stay within constraints of allocated buffer by releasing the
               // eldest buffered object.
  -            remove(0);
  +            remove(ELDEST_INDEX);
           }
           return super.put(key, value);
       }
  @@ -131,5 +168,17 @@
               freshenSequence(key, value);
           }
           return value;
  +    }
  +
  +    /**
  +     * Asserts that the specified size is greater than zero.
  +     */
  +    private static final void assertReasonableMaxSize(int maxSize)
  +    {
  +        if (maxSize <= 0)
  +        {
  +            throw new IllegalArgumentException
  +                ("BufferCache size must be 1 or greater");
  +        }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org