jtaylor 02/05/14 14:48:53
Modified: src/java/org/apache/jcs/auxiliary/disk
AbstractDiskCache.java
src/java/org/apache/jcs/auxiliary/disk/indexed
IndexedDiskCache.java
src/test/org/apache/jcs TestDiskCache.java
Log:
Step two: With this change (moving code inside locked region) I no longer have
any problems.
Also, I pushed the other lock into IndexedDiskCache since it is only used
there.
Revision Changes Path
1.13 +38 -42
jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/AbstractDiskCache.java
Index: AbstractDiskCache.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/AbstractDiskCache.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- AbstractDiskCache.java 14 May 2002 21:45:52 -0000 1.12
+++ AbstractDiskCache.java 14 May 2002 21:48:53 -0000 1.13
@@ -54,24 +54,23 @@
* <http://www.apache.org/>.
*/
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Hashtable;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.jcs.engine.CacheElement;
+import org.apache.jcs.auxiliary.AuxiliaryCache;
+import org.apache.jcs.engine.CacheConstants;
import org.apache.jcs.engine.CacheEventQueue;
import org.apache.jcs.engine.CacheInfo;
-import org.apache.jcs.engine.CacheConstants;
import org.apache.jcs.engine.behavior.ICache;
-import org.apache.jcs.engine.behavior.ICacheEventQueue;
import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.engine.behavior.ICacheEventQueue;
import org.apache.jcs.engine.behavior.ICacheListener;
-import org.apache.jcs.auxiliary.AuxiliaryCache;
import org.apache.jcs.utils.locking.ReadWriteLock;
import org.apache.jcs.utils.locking.ReadWriteLockManager;
-import java.io.IOException;
-import java.io.Serializable;
-import java.util.Hashtable;
-
/**
* Abstract class providing a base implementation of a disk cache, which can
* be easily extended to implement a disk cache for a specific perstistence
@@ -88,7 +87,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Aaron Smuts</a>
* @author <a href="mailto:[EMAIL PROTECTED]">James Taylor</a>
- * @version $Id: AbstractDiskCache.java,v 1.12 2002/05/14 21:45:52 jtaylor Exp $
+ * @version $Id: AbstractDiskCache.java,v 1.13 2002/05/14 21:48:53 jtaylor Exp $
*/
public abstract class AbstractDiskCache implements AuxiliaryCache, Serializable
{
@@ -258,23 +257,24 @@
*/
public final boolean remove( Serializable key )
{
- // Remove element from purgatory if it is there
+ String keyAsString = key.toString();
+ writeLock( keyAsString );
- writeLock( this.cacheName + key.toString() );
try
{
+ // Remove element from purgatory if it is there
- purgatory.remove( key );
+ purgatory.remove( key );
- doRemove( key );
+ // Remove from persistent store immediately
+ doRemove( key );
}
finally
{
- releaseLock( this.cacheName + key.toString() );
+ releaseLock( keyAsString );
}
- // Remove from persistent store immediately
return false;
}
@@ -344,7 +344,6 @@
return DISK_CACHE;
}
-
/**
* Internally used write lock for purgatory item modification.
*
@@ -365,11 +364,10 @@
catch ( Throwable e )
{
- log.error( e );
+ log.error( e );
}
}
-
/**
* Internally used write lock for purgatory item modification.
*
@@ -384,11 +382,10 @@
catch ( Throwable e )
{
- log.error( e );
+ log.error( e );
}
}
-
/**
* Cache that implements the CacheListener interface, and calls appropriate
* methods in its parent class.
@@ -435,38 +432,38 @@
{
PurgatoryElement pe = ( PurgatoryElement ) element;
- // If the element has already been removed from purgatory
- // do nothing
+ String keyAsString = element.getKey().toString();
+
+ writeLock( keyAsString );
- String lK =element.getKey().toString();
- writeLock( getCacheName() + lK );
try
{
+ // If the element has already been removed from
+ // purgatory do nothing
- if ( ! purgatory.contains( pe ) )
- {
- return;
- }
+ if ( ! purgatory.contains( pe ) )
+ {
+ return;
+ }
- }
- finally
- {
- releaseLock( getCacheName() + lK );
- }
+ element = pe.getCacheElement();
+
+ // If the element is still eligable, spool it.
- element = pe.getCacheElement();
+ if ( pe.isSpoolable() )
+ {
+ doUpdate( element );
+ }
- // If the element is still eligable, spool it.
+ // After the update has completed, it is safe to remove
+ // the element from purgatory.
- if ( pe.isSpoolable() )
+ purgatory.remove( element.getKey() );
+ }
+ finally
{
- doUpdate( element );
+ releaseLock( keyAsString );
}
-
- // After the update has completed, it is safe to remove
- // the element from purgatory.
-
- purgatory.remove( element.getKey() );
}
else
{
@@ -548,7 +545,6 @@
* setting alive to false does NOT need to be done by this method.
*/
protected abstract void doDispose();
-
}
1.4 +22 -15
jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
Index: IndexedDiskCache.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- IndexedDiskCache.java 10 Apr 2002 15:00:59 -0000 1.3
+++ IndexedDiskCache.java 14 May 2002 21:48:53 -0000 1.4
@@ -68,13 +68,14 @@
import org.apache.jcs.engine.CacheConstants;
import org.apache.jcs.engine.CacheElement;
import org.apache.jcs.engine.behavior.ICacheElement;
+import org.apache.jcs.utils.locking.ReadWriteLock;
/**
* Disk cache that uses a RandomAccessFile with keys stored in memory
*
* @author <a href="mailto:[EMAIL PROTECTED]">Aaron Smuts</a>
* @author <a href="mailto:[EMAIL PROTECTED]">James Taylor</a>
- * @version $Id: IndexedDiskCache.java,v 1.3 2002/04/10 15:00:59 jtaylor Exp $
+ * @version $Id: IndexedDiskCache.java,v 1.4 2002/05/14 21:48:53 jtaylor Exp $
*/
public class IndexedDiskCache extends AbstractDiskCache
{
@@ -91,6 +92,12 @@
IndexedDiskCacheAttributes cattr;
/**
+ * Each instance of a Disk cache should use this lock to synchronize reads
+ * and writes to the underlying storage mechansism.
+ */
+ protected ReadWriteLock storageLock = new ReadWriteLock();
+
+ /**
* Constructor for the DiskCache object
*
* @param cattr
@@ -162,7 +169,7 @@
private void loadKeys()
throws InterruptedException
{
- lock.writeLock();
+ storageLock.writeLock();
try
{
@@ -185,7 +192,7 @@
}
finally
{
- lock.done();
+ storageLock.done();
}
}
@@ -202,7 +209,7 @@
", key count: " + keyHash.size() );
}
- lock.writeLock();
+ storageLock.writeLock();
try
{
@@ -215,7 +222,7 @@
}
finally
{
- lock.done();
+ storageLock.done();
}
}
catch ( Exception e )
@@ -245,7 +252,7 @@
ded.init( dataFile.length(), data );
// make sure this only locks for one particular cache region
- lock.writeLock();
+ storageLock.writeLock();
try
{
@@ -269,7 +276,7 @@
}
finally
{
- lock.done();
+ storageLock.done();
}
if ( log.isDebugEnabled() )
{
@@ -305,7 +312,7 @@
try
{
- lock.readLock();
+ storageLock.readLock();
if ( !alive )
{
@@ -325,7 +332,7 @@
}
finally
{
- lock.done();
+ storageLock.done();
}
return object;
@@ -363,7 +370,7 @@
{
try
{
- lock.writeLock();
+ storageLock.writeLock();
if ( key instanceof String
&& key.toString().endsWith(
CacheConstants.NAME_COMPONENT_DELIMITER ) )
@@ -401,7 +408,7 @@
}
finally
{
- lock.done();
+ storageLock.done();
}
return false;
@@ -436,7 +443,7 @@
try
{
- lock.writeLock();
+ storageLock.writeLock();
dataFile.close();
File file = new File( rafDir, fileName + ".data" );
@@ -460,7 +467,7 @@
}
finally
{
- lock.done();
+ storageLock.done();
}
}
@@ -471,7 +478,7 @@
{
try
{
- lock.writeLock();
+ storageLock.writeLock();
if ( !alive )
{
@@ -509,7 +516,7 @@
finally
{
alive = false;
- lock.done();
+ storageLock.done();
}
}
1.5 +3 -2 jakarta-turbine-jcs/src/test/org/apache/jcs/TestDiskCache.java
Index: TestDiskCache.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/test/org/apache/jcs/TestDiskCache.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestDiskCache.java 14 May 2002 17:44:40 -0000 1.4
+++ TestDiskCache.java 14 May 2002 21:48:53 -0000 1.5
@@ -64,7 +64,7 @@
* regions for thre threads.
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Taylor</a>
- * @version $Id: TestDiskCache.java,v 1.4 2002/05/14 17:44:40 jtaylor Exp $
+ * @version $Id: TestDiskCache.java,v 1.5 2002/05/14 21:48:53 jtaylor Exp $
*/
public class TestDiskCache extends TestCase
{
@@ -175,7 +175,8 @@
for ( int i = 0; i <= items; i++ )
{
- assertNull( jcs.get( i + ":key" ) );
+ assertNull( "Removed key should be null: " + i + ":key",
+ jcs.get( i + ":key" ) );
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>