You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/20 09:33:10 UTC
cvs commit: jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft KeyedSoftRef.java KeyedSoftRefCollector.java SoftRefCache.java SoftRefCacheCleaner.java SoftRefCacheSafe.java
hchar 2005/01/20 00:33:10
Modified: auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft
KeyedSoftRef.java KeyedSoftRefCollector.java
SoftRefCache.java SoftRefCacheCleaner.java
SoftRefCacheSafe.java
Log:
no message
Revision Changes Path
1.4 +0 -0 jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/KeyedSoftRef.java
Index: KeyedSoftRef.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/KeyedSoftRef.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
1.3 +8 -7 jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/KeyedSoftRefCollector.java
Index: KeyedSoftRefCollector.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/KeyedSoftRefCollector.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- KeyedSoftRefCollector.java 19 Jan 2005 11:22:52 -0000 1.2
+++ KeyedSoftRefCollector.java 20 Jan 2005 08:33:10 -0000 1.3
@@ -4,13 +4,14 @@
package net.sf.yajcache.soft;
+import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
- * Collects and clears state cache entries implemented using Soft References.
+ * Collects and clears stale cache entries implemented using Soft References.
*
* @author Hanson Char
*/
@@ -29,17 +30,17 @@
* Removes stale entries from the cache map collected by GC.
* Thread safetyness provided by ReferenceQueue.
*/
+// @SuppressWarnings("unchecked")
public void run() {
-// if (debug)
-// log.debug("Run...");
- KeyedSoftRef<V> ksr;
+ Reference<? extends V> r;
- while ((ksr = (KeyedSoftRef<V>)this.q.poll()) != null) {
+ while ((r = this.q.poll()) != null) {
+ KeyedSoftRef ksr = (KeyedSoftRef)r;
String key = ksr.getKey();
if (debug)
log.debug("Remove stale entry with key=" + key);
-// map.remove(key);
SoftRefCacheCleaner.inst.cleanupKey(map, key);
+ // referent should have been cleared. Defensively clear it again.
ksr.clear();
count++;
}
1.3 +21 -5 jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/SoftRefCache.java
Index: SoftRefCache.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/SoftRefCache.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SoftRefCache.java 19 Jan 2005 11:22:52 -0000 1.2
+++ SoftRefCache.java 20 Jan 2005 08:33:10 -0000 1.3
@@ -18,6 +18,7 @@
import net.sf.yajcache.core.ICache;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import net.sf.yajcache.annotate.*;
/**
@@ -25,6 +26,7 @@
*
* @author Hanson Char
*/
+@TODO("Annotate the thread-safetyness of the methods")
public class SoftRefCache<V> implements ICache<V> {
private static final boolean debug = true;
private Log log = debug ? LogFactory.getLog(this.getClass()) : null;
@@ -143,10 +145,23 @@
return null;
V ret = oldRef.get();
oldRef.clear();
+
+ if (!value.equals(ret)) {
+ // value changed for the key
+ this.publishFlushKey(key);
+ }
return ret;
}
+
+ @TODO(
+ value="Queue up a flush event for the key.",
+ details="This is useful for synchronizing caches in a cluster environment."
+ )
+ private void publishFlushKey(String key) {
+ }
+
public void putAll(Map<? extends String, ? extends V> map) {
- for (Map.Entry<? extends String, ? extends V> e : map.entrySet())
+ for (final Map.Entry<? extends String, ? extends V> e : map.entrySet())
this.put(e.getKey(), e.getValue());
}
public V remove(String key) {
@@ -155,6 +170,7 @@
if (oldRef == null)
return null;
+ this.publishFlushKey(key);
V ret = oldRef.get();
oldRef.clear();
return ret;
@@ -175,7 +191,7 @@
Set<Map.Entry<String,KeyedSoftRef<V>>> fromSet = map.entrySet();
Set<Map.Entry<String,V>> toSet = new HashSet<Map.Entry<String,V>>();
- for (Map.Entry<String,KeyedSoftRef<V>> item : fromSet) {
+ for (final Map.Entry<String,KeyedSoftRef<V>> item : fromSet) {
KeyedSoftRef<V> ref = item.getValue();
V val = ref.get();
@@ -191,7 +207,7 @@
Collection<KeyedSoftRef<V>> fromSet = map.values();
List<V> toCol = new ArrayList<V>(fromSet.size());
- for (KeyedSoftRef<V> ref : fromSet) {
+ for (final KeyedSoftRef<V> ref : fromSet) {
V val = ref.get();
if (val != null) {
@@ -209,7 +225,7 @@
this.collector.run();
Collection<KeyedSoftRef<V>> fromSet = map.values();
- for (KeyedSoftRef<V> ref : fromSet) {
+ for (final KeyedSoftRef<V> ref : fromSet) {
V val = ref.get();
if (value.equals(val))
1.3 +3 -1 jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/SoftRefCacheCleaner.java
Index: SoftRefCacheCleaner.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/SoftRefCacheCleaner.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SoftRefCacheCleaner.java 19 Jan 2005 11:22:52 -0000 1.2
+++ SoftRefCacheCleaner.java 20 Jan 2005 08:33:10 -0000 1.3
@@ -46,12 +46,14 @@
V oldVal = oldRef.get();
if (val == oldVal) {
+ // not considered a race condition
oldRef.clear();
if (debug)
log.debug("Key removed and Soft Reference cleared.");
this.countKeyCleaned++;
return;
}
+ // Race condition.
do {
if (debug)
log.debug("Race condition occurred. So put back the old stuff.");
1.3 +7 -5 jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/SoftRefCacheSafe.java
Index: SoftRefCacheSafe.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/soft/SoftRefCacheSafe.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SoftRefCacheSafe.java 19 Jan 2005 11:22:52 -0000 1.2
+++ SoftRefCacheSafe.java 20 Jan 2005 08:33:10 -0000 1.3
@@ -11,11 +11,13 @@
import net.sf.yajcache.core.ICacheSafe;
import net.sf.yajcache.util.BeanUtils;
import net.sf.yajcache.util.SerializeUtils;
+import net.sf.yajcache.annotate.*;
/**
*
* @author Hanson Char
*/
+@TODO("Annotate the thread-safetyness of the methods")
public class SoftRefCacheSafe<V> extends SoftRefCache<V>
implements ICacheSafe<V>
{
@@ -41,11 +43,11 @@
return this.put(key, this.dup(value));
}
public void putAll(Map<? extends String, ? extends V> map) {
- for (Map.Entry<? extends String, ? extends V> e : map.entrySet())
+ for (final Map.Entry<? extends String, ? extends V> e : map.entrySet())
this.put(e.getKey(), e.getValue());
}
public void putAllCopies(Map<? extends String, ? extends V> map) {
- for (Map.Entry<? extends String, ? extends V> e : map.entrySet())
+ for (final Map.Entry<? extends String, ? extends V> e : map.entrySet())
this.put(e.getKey(), this.dup(e.getValue()));
}
public V getBeanCopy(String key) {
@@ -56,7 +58,7 @@
return this.put(key, BeanUtils.inst.cloneDeep(value));
}
public void putAllBeanCopies(Map<? extends String, ? extends V> map) {
- for (Map.Entry<? extends String, ? extends V> e : map.entrySet())
+ for (final Map.Entry<? extends String, ? extends V> e : map.entrySet())
this.put(e.getKey(), BeanUtils.inst.cloneDeep(e.getValue()));
}
public V getBeanClone(String key) {
@@ -67,7 +69,7 @@
return this.put(key, BeanUtils.inst.cloneShallow(value));
}
public void putAllBeanClones(Map<? extends String, ? extends V> map) {
- for (Map.Entry<? extends String, ? extends V> e : map.entrySet())
+ for (final Map.Entry<? extends String, ? extends V> e : map.entrySet())
this.put(e.getKey(), BeanUtils.inst.cloneShallow(e.getValue()));
}
private V dup(V val) {
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org