You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by co...@apache.org on 2002/01/17 16:47:05 UTC

cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache AbstractCache.java Cache.java DefaultCache.java

colus       02/01/17 07:47:05

  Modified:    src/scratchpad/org/apache/avalon/excalibur/cache
                        AbstractCache.java Cache.java DefaultCache.java
  Log:
  Removed thread-safety from Cache.
  Added SynchronizedCache decorator.
  
  Revision  Changes    Path
  1.6       +6 -24     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/AbstractCache.java
  
  Index: AbstractCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/AbstractCache.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- AbstractCache.java	8 Jan 2002 12:58:04 -0000	1.5
  +++ AbstractCache.java	17 Jan 2002 15:47:05 -0000	1.6
  @@ -26,34 +26,22 @@
   
       public void addListener( final CacheListener listener )
       {
  -        synchronized ( m_listeners )
  -        {
  -            m_listeners.add( listener );
  -        }
  +        m_listeners.add( listener );
       }
   
       public void removeListener( final CacheListener listener )
       {
  -        synchronized ( m_listeners )
  -        {
  -            m_listeners.remove( listener );
  -        }
  +        m_listeners.remove( listener );
       }
   
       protected void notifyAdded( final Object key, final Object value )
       {
           final CacheEvent event = new CacheEvent( this, key, value );
   
  -        ArrayList listeners;
  -        synchronized ( m_listeners )
  -        {
  -            listeners = (ArrayList)m_listeners.clone();
  -        }
  -
  -        final int s = listeners.size();
  +        final int s = m_listeners.size();
           for ( int i = 0; i < s; i++ )
           {
  -            ((CacheListener)listeners.get( i )).added( event );
  +            ((CacheListener)m_listeners.get( i )).added( event );
           }
       }
   
  @@ -61,16 +49,10 @@
       {
           final CacheEvent event = new CacheEvent( this, key, value );
   
  -        ArrayList listeners;
  -        synchronized ( m_listeners )
  -        {
  -            listeners = (ArrayList)m_listeners.clone();
  -        }
  -
  -        final int s = listeners.size();
  +        final int s = m_listeners.size();
           for ( int i = 0; i < s; i++ )
           {
  -            ((CacheListener)listeners.get( i )).removed( event );
  +            ((CacheListener)m_listeners.get( i )).removed( event );
           }
       }
   }
  
  
  
  1.7       +1 -2      jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/Cache.java
  
  Index: Cache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/Cache.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Cache.java	8 Jan 2002 12:58:04 -0000	1.6
  +++ Cache.java	17 Jan 2002 15:47:05 -0000	1.7
  @@ -8,7 +8,6 @@
   package org.apache.avalon.excalibur.cache;
   
   import org.apache.avalon.framework.component.Component;
  -import org.apache.avalon.framework.thread.ThreadSafe;
   
   /**
    * This is a cache that caches objects for reuse.
  @@ -17,7 +16,7 @@
    * @author <a href="mailto:colus@apache.org">Eung-ju Park</a>
    */
   public interface Cache
  -    extends ThreadSafe, Component
  +    extends Component
   {
       /**
        * Add listener.
  
  
  
  1.8       +16 -39    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/DefaultCache.java
  
  Index: DefaultCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/cache/DefaultCache.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DefaultCache.java	8 Jan 2002 12:58:04 -0000	1.7
  +++ DefaultCache.java	17 Jan 2002 15:47:05 -0000	1.8
  @@ -37,50 +37,33 @@
   
       public Object put( final Object key, final Object value )
       {
  -        Object oldValue;
  +        final Object oldValue = remove( key );
   
  -        synchronized ( m_store )
  +        if ( m_store.isFull() )
           {
  -            if ( m_store.containsKey( key ) )
  -            {
  -                oldValue = remove( key );
  -            }
  -            else
  -            {
  -                oldValue = null;
  -            }
  -
  -            if ( m_store.isFull() )
  -            {
  -                remove( m_policy.selectVictim() );
  -            }
  -
  -            m_store.put( key, value );
  -            m_policy.add( key );
  -            notifyAdded( key, value );
  +            remove( m_policy.selectVictim() );
           }
   
  +        m_store.put( key, value );
  +        m_policy.add( key );
  +        notifyAdded( key, value );
  +
           return oldValue;
       }
   
       public Object get( final Object key )
       {
  -        Object value;
  -        
  -        synchronized ( m_store )
  -        {
  -            value = m_store.get( key );
  -            m_policy.hit( key );
  -        }
  +        final Object value = m_store.get( key );
  +        m_policy.hit( key );
   
           return value;
       }
   
       public Object remove( final Object key )
       {
  -        Object value;
  -        
  -        synchronized ( m_store )
  +        Object value = null;
  +
  +        if ( m_store.containsKey( key ) )
           {
               value = m_store.remove( key );
               m_policy.remove( key );
  @@ -92,21 +75,15 @@
   
       public boolean containsKey( final Object key )
       {
  -        synchronized ( m_store )
  -        {
  -            return m_store.containsKey( key );
  -        }
  +        return m_store.containsKey( key );
       }
   
       public void clear()
       {
  -        synchronized ( m_store )
  +        final Object[] keys = m_store.keys();
  +        for ( int i = 0; i < keys.length; i++ )
           {
  -            final Object[] keys = m_store.keys();
  -            for ( int i = 0; i < keys.length; i++ )
  -            {
  -                remove( keys[ i ] );
  -            }
  +            remove( keys[ i ] );
           }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>