directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r345925 - /directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/LRUMap.java
Date Mon, 21 Nov 2005 16:10:41 GMT
Author: elecharny
Date: Mon Nov 21 08:10:37 2005
New Revision: 345925

URL: http://svn.apache.org/viewcvs?rev=345925&view=rev
Log:
Synchronized the HashMap to avoid problem on heavy loaded servers.

Modified:
    directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/LRUMap.java

Modified: directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/LRUMap.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/LRUMap.java?rev=345925&r1=345924&r2=345925&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/LRUMap.java
(original)
+++ directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/util/LRUMap.java
Mon Nov 21 08:10:37 2005
@@ -58,7 +58,8 @@
      * LRU limit of 100 keys, but this value may be overridden
      * internally as a result of de-externalization.
      */
-    public LRUMap() {
+    public LRUMap() 
+    {
         this( 100 );
     }
 
@@ -69,7 +70,8 @@
      * 
      * @param i      Maximum capacity of the LRUMap
      */
-    public LRUMap(int i) {
+    public LRUMap(int i) 
+    {
         super( i );
         maximumSize = i;
     }
@@ -86,11 +88,15 @@
      * @return Returns the value.  Returns null if the key has a
      *         null value <i>or</i> if the key has no value.
      */
-    public Object get(Object key) {
-        if(!containsKey(key)) return null;
-
-        Object value = remove(key);
-        super.put(key,value);
+    public synchronized Object get(Object key) 
+    {
+    	if(!containsKey(key)) 
+    	{
+    		return null;
+    	}
+    
+    	Object value = remove(key);
+        super.put(key, value);
         return value;
     }
 
@@ -106,23 +112,32 @@
       * @param value  Object to add
       * @return Former value of the key
       */    
-    public Object put( Object key, Object value ) {
+    public Object put( Object key, Object value ) 
+    {
 
         int mapSize = size();
         Object retval = null;
 
-        if ( mapSize >= maximumSize ) {
+        if ( mapSize >= maximumSize ) 
+        {
+	        synchronized (this)
+	        {
+	            // don't retire LRU if you are just
+	            // updating an existing key
+	            if (!containsKey(key)) 
+	            {
+	                // lets retire the least recently used item in the cache
+	                removeLRU();
+	            }
 
-            // don't retire LRU if you are just
-            // updating an existing key
-            if (!containsKey(key)) {
-                // lets retire the least recently used item in the cache
-                removeLRU();
-            }
+	            retval = super.put(key,value);
+	        }
         }
-
-        retval = super.put(key,value);
-
+        else
+        {
+	        retval = super.put(key,value);
+        }
+        
         return retval;
     }
 
@@ -130,15 +145,14 @@
      * This method is used internally by the class for 
      * finding and removing the LRU Object.
      */
-    protected void removeLRU() {
+    protected void removeLRU() 
+    {
         Object key = getFirstKey();
         // be sure to call super.get(key), or you're likely to 
         // get infinite promotion recursion
-        Object value = super.get(key);
+        super.get(key);
         
         remove(key);
-
-        processRemovedLRU(key,value);
     }
 
     /**
@@ -150,16 +164,19 @@
      * @param key    key that was removed
      * @param value  value of that key (can be null)
      */
-    protected void processRemovedLRU(Object key, Object value) {
+    protected void processRemovedLRU(Object key, Object value) 
+    {
     }
  
     // Externalizable interface
     //-------------------------------------------------------------------------        
-    public void readExternal( ObjectInput in )  throws IOException, ClassNotFoundException
{
+    public void readExternal( ObjectInput in )  throws IOException, ClassNotFoundException

+    {
         maximumSize = in.readInt();
         int size = in.readInt();
         
-        for( int i = 0; i < size; i++ )  {
+        for( int i = 0; i < size; i++ )  
+        {
             Object key = in.readObject();
             Object value = in.readObject();
             put(key,value);
@@ -169,7 +186,9 @@
     public void writeExternal( ObjectOutput out ) throws IOException {
         out.writeInt( maximumSize );
         out.writeInt( size() );
-        for( Iterator iterator = keySet().iterator(); iterator.hasNext(); ) {
+        
+        for( Iterator iterator = keySet().iterator(); iterator.hasNext(); ) 
+        {
             Object key = iterator.next();
             out.writeObject( key );
             // be sure to call super.get(key), or you're likely to 
@@ -185,16 +204,24 @@
     /** Getter for property maximumSize.
      * @return Value of property maximumSize.
      */
-    public int getMaximumSize() {
+    public int getMaximumSize() 
+    {
         return maximumSize;
     }
+    
     /** Setter for property maximumSize.
      * @param maximumSize New value of property maximumSize.
      */
-    public void setMaximumSize(int maximumSize) {
-        this.maximumSize = maximumSize;
-        while (size() > maximumSize) {
-            removeLRU();
+    public void setMaximumSize(int maximumSize) 
+    {
+        synchronized (this)
+        {
+        	this.maximumSize = maximumSize;
+        
+	        while (size() > maximumSize) 
+	        {
+	            removeLRU();
+	        }
         }
     }
 



Mime
View raw message