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/29 12:58:07 UTC
cvs commit: jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft SoftRefCache.java SoftRefFileCache.java
hchar 2005/01/29 03:58:07
Modified: sandbox/yajcache/src/org/apache/jcs/yajcache/soft
SoftRefCache.java SoftRefFileCache.java
Log:
use equals utils + add counts
Revision Changes Path
1.6 +37 -7 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCache.java
Index: SoftRefCache.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefCache.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SoftRefCache.java 29 Jan 2005 10:19:24 -0000 1.5
+++ SoftRefCache.java 29 Jan 2005 11:58:07 -0000 1.6
@@ -25,6 +25,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -34,6 +35,7 @@
import org.apache.jcs.yajcache.lang.annotation.*;
import org.apache.jcs.yajcache.lang.ref.KeyedRefCollector;
import org.apache.jcs.yajcache.lang.ref.KeyedSoftReference;
+import org.apache.jcs.yajcache.util.EqualsUtils;
/**
@@ -49,10 +51,18 @@
private final @NonNullable ReferenceQueue<V> refq = new ReferenceQueue<V>();
private final @NonNullable String name;
private final @NonNullable Class<V> valueType;
- private final @NonNullable ConcurrentMap<String, KeyedSoftReference<String,V>> map;
+ private final @NonNullable ConcurrentMap<String,KeyedSoftReference<String,V>> map;
private final @NonNullable KeyedRefCollector<String> collector;
private @NonNullable PerCacheConfig config;
+
+ private AtomicInteger countGet = new AtomicInteger(0);
+ private AtomicInteger countGetHitMemory = new AtomicInteger(0);
+ private AtomicInteger countGetMiss = new AtomicInteger(0);
+ private AtomicInteger countGetEmptyRef = new AtomicInteger(0);
+ private AtomicInteger countPut = new AtomicInteger(0);
+ private AtomicInteger countRemove = new AtomicInteger(0);
+
public @NonNullable String getName() {
return this.name;
}
@@ -102,21 +112,31 @@
// But do we really want to synchronize upon invoking get() ?
// It's not thread-safe, but what's the worst consequence ?
public V get(@NonNullable String key) {
+ if (debug)
+ this.countGet.incrementAndGet();
this.collector.run();
KeyedSoftReference<String,V> ref = map.get(key);
- if (ref == null)
+ if (ref == null) {
+ if (debug)
+ this.countGetMiss.incrementAndGet();
return null;
+ }
V val = ref.get();
if (val == null) {
+ // Rarely gets here, if ever.
// already garbage collected. So try to clean up the key.
+ if (debug)
+ this.countGetEmptyRef.incrementAndGet();
this.map.remove(key, ref);
// SoftRefCacheCleaner.inst.cleanupKey(this.map, key);
}
// cache value exists.
// try to refresh the soft reference.
// this.renewSoftReference(key, val);
+ if (debug)
+ this.countGetHitMemory.incrementAndGet();
return val;
}
// private void renewSoftReference(String key, V val) {
@@ -163,6 +183,8 @@
return key == null ? null : this.get(key.toString());
}
public V put(@NonNullable String key, @NonNullable V value) {
+ if (debug)
+ this.countPut.incrementAndGet();
this.collector.run();
KeyedSoftReference<String,V> oldRef =
map.put(key, new KeyedSoftReference<String,V>(key, value, refq));
@@ -172,7 +194,7 @@
V ret = oldRef.get();
oldRef.clear();
- if (!value.equals(ret)) {
+ if (!EqualsUtils.inst.equals(value, ret)) {
// value changed for the key
this.publishFlushKey(key);
}
@@ -191,6 +213,8 @@
this.put(e.getKey(), e.getValue());
}
public V remove(@NonNullable String key) {
+ if (debug)
+ this.countRemove.incrementAndGet();
this.collector.run();
KeyedSoftReference<String,V> oldRef = map.remove(key);
@@ -254,7 +278,7 @@
for (final KeyedSoftReference<String,V> ref : fromSet) {
V val = ref.get();
- if (value.equals(val))
+ if (EqualsUtils.inst.equals(value, val))
return true;
}
return false;
@@ -273,9 +297,15 @@
}
@Override public String toString() {
return new ToStringBuilder(this)
- .append("name", this.getName())
- .append("valueType", this.getValueType().getName())
- .append("collector", this.collector)
+ .append("\n").append("name", this.getName())
+ .append("\n").append("valueType", this.getValueType().getName())
+ .append("\n").append("countGet", this.countGet)
+ .append("\n").append("countGetEmptyRef", this.countGetEmptyRef)
+ .append("\n").append("countGetMiss", this.countGetMiss)
+ .append("\n").append("countGetHitMemory", this.countGetHitMemory)
+ .append("\n").append("this.countPut", this.countPut)
+ .append("\n").append("this.countRemove", this.countRemove)
+ .append("\n").append("collector", this.collector)
.toString();
}
}
1.5 +88 -10 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefFileCache.java
Index: SoftRefFileCache.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/soft/SoftRefFileCache.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SoftRefFileCache.java 29 Jan 2005 10:19:24 -0000 1.4
+++ SoftRefFileCache.java 29 Jan 2005 11:58:07 -0000 1.5
@@ -45,6 +45,7 @@
import org.apache.jcs.yajcache.file.CacheFileDAO;
import org.apache.jcs.yajcache.lang.ref.KeyedRefCollector;
import org.apache.jcs.yajcache.lang.ref.KeyedSoftReference;
+import org.apache.jcs.yajcache.util.EqualsUtils;
import org.apache.jcs.yajcache.util.concurrent.locks.IKeyedReadWriteLock;
import org.apache.jcs.yajcache.util.concurrent.locks.KeyedReadWriteLock;
@@ -76,11 +77,24 @@
new CacheChangeSupport<V>(this);
private AtomicInteger countGet = new AtomicInteger(0);
+
private AtomicInteger countGetHitMemory = new AtomicInteger(0);
private AtomicInteger countGetHitFile = new AtomicInteger(0);
+
+ private AtomicInteger countGetMissMemory = new AtomicInteger(0);
private AtomicInteger countGetMiss = new AtomicInteger(0);
+ private AtomicInteger countGetCorruptedFile = new AtomicInteger(0);
+ private AtomicInteger countGetEmptyRef = new AtomicInteger(0);
private AtomicInteger countPut = new AtomicInteger(0);
+ private AtomicInteger countPutClearRef = new AtomicInteger(0);
+ private AtomicInteger countPutMissMemory = new AtomicInteger(0);
+ private AtomicInteger countPutNewMemoryValue = new AtomicInteger(0);
+ private AtomicInteger countPutNewFileValue = new AtomicInteger(0);
+ private AtomicInteger countPutSerializable = new AtomicInteger(0);
+ private AtomicInteger countPutReadFile = new AtomicInteger(0);
+ private AtomicInteger countPutWriteFile = new AtomicInteger(0);
+
private AtomicInteger countRemove = new AtomicInteger(0);
public @NonNullable String getName() {
@@ -132,6 +146,8 @@
// It's not thread-safe, but what's the worst consequence ?
public V get(@NonNullable String key) {
collector.run();
+ if (debug)
+ this.countGet.incrementAndGet();
Lock lock = this.krwl.readLock(key);
lock.lock();
try {
@@ -143,13 +159,23 @@
private V doGet(String key) {
KeyedSoftReference<String,V> ref = map.get(key);
V val = null;
-
- if (ref != null)
+
+ if (ref != null) {
val = ref.get();
-
+
+ if (debug) {
+ if (val == null)
+ this.countGetEmptyRef.incrementAndGet();
+ }
+ }
+ else {
+ if (debug)
+ this.countGetMissMemory.incrementAndGet();
+ }
if (val == null) {
// Not in memory.
if (ref != null) {
+ // Rarely gets here, if ever.
// GC'd. So try to clean up the key/ref pair.
this.map.remove(key, ref);
}
@@ -158,13 +184,19 @@
if (cfc == null) {
// Not in file system.
+ if (debug)
+ this.countGetMiss.incrementAndGet();
return null;
}
// Found in file system.
+ if (debug)
+ this.countGetHitFile.incrementAndGet();
val = (V)cfc.deserialize();
if (val == null) {
// Corrupted file. Try remove it from file system.
+ if (debug)
+ this.countGetCorruptedFile.incrementAndGet();
CacheFileDAO.inst.removeCacheItem(this.name, key);
return null;
}
@@ -172,6 +204,10 @@
map.putIfAbsent(key,
new KeyedSoftReference<String,V>(key, val, refq));
}
+ else {
+ if (debug)
+ this.countGetHitMemory.incrementAndGet();
+ }
// cache value exists.
return val;
}
@@ -220,6 +256,9 @@
}
public V put(@NonNullable String key, @NonNullable V value) {
this.collector.run();
+
+ if (debug)
+ this.countPut.incrementAndGet();
Lock lock = this.krwl.writeLock(key);
lock.lock();
try {
@@ -236,18 +275,32 @@
if (oldRef != null) {
ret = oldRef.get();
oldRef.clear();
+
+ if (debug)
+ this.countPutClearRef.incrementAndGet();
}
if (ret == null) {
// Not in memory.
+ if (debug)
+ this.countPutMissMemory.incrementAndGet();
if (value instanceof Serializable) {
// Try the file system.
+ if (debug)
+ this.countPutSerializable.incrementAndGet();
CacheFileContent cfc =
CacheFileDAO.inst.readCacheItem(this.name, key);
- if (cfc != null)
+ if (cfc != null) {
+ if (debug)
+ this.countPutReadFile.incrementAndGet();
ret = (V)cfc.deserialize();
- if (!value.equals(ret)) {
+ }
+ if (!EqualsUtils.inst.equals(value, ret)) {
// Considered new value being put to memory.
// So persist to file system.
+ if (debug) {
+ this.countPutNewFileValue.incrementAndGet();
+ this.countPutWriteFile.incrementAndGet();
+ }
byte[] ba = SerializationUtils.serialize((Serializable)value);
CacheFileDAO.inst.writeCacheItem(
this.name, CacheFileContentType.JAVA_SERIALIZATION, key, ba);
@@ -257,10 +310,16 @@
}
// ret must be non-null.
// Found in memory
- if (!value.equals(ret)) {
+ if (!EqualsUtils.inst.equals(value, ret)) {
+ if (debug)
+ this.countPutNewMemoryValue.incrementAndGet();
// Different value being put to memory.
if (value instanceof Serializable) {
// Persist to file system.
+ if (debug) {
+ this.countPutSerializable.incrementAndGet();
+ this.countPutWriteFile.incrementAndGet();
+ }
byte[] ba = SerializationUtils.serialize((Serializable)value);
CacheFileDAO.inst.writeCacheItem(
this.name, CacheFileContentType.JAVA_SERIALIZATION, key, ba);
@@ -282,6 +341,9 @@
}
public V remove(@NonNullable String key) {
this.collector.run();
+
+ if (debug)
+ this.countRemove.incrementAndGet();
Lock lock = this.krwl.writeLock(key);
lock.lock();
try {
@@ -369,7 +431,7 @@
for (final KeyedSoftReference<String,V> ref : fromSet) {
V val = ref.get();
- if (value.equals(val))
+ if (EqualsUtils.inst.equals(value, val))
return true;
}
return false;
@@ -396,9 +458,25 @@
}
@Override public String toString() {
return new ToStringBuilder(this)
- .append("name", this.getName())
- .append("valueType", this.getValueType().getName())
- .append("collector", this.collector)
+ .append("\n").append("name", this.getName())
+ .append("\n").append("valueType", this.getValueType().getName())
+ .append("\n").append("countGet", this.countGet)
+ .append("\n").append("countGetHitMemory", this.countGetHitMemory)
+ .append("\n").append("countGetHitFile", this.countGetHitFile)
+ .append("\n").append("countGetMissMemory", this.countGetMissMemory)
+ .append("\n").append("countGetEmptyRef", this.countGetEmptyRef)
+ .append("\n").append("countGetMiss", this.countGetMiss)
+ .append("\n").append("countGetCorruptedFile", this.countGetCorruptedFile)
+ .append("\n").append("countPut", this.countPut)
+ .append("\n").append("countPutClearRef", this.countPutClearRef)
+ .append("\n").append("countPutMissMemory", this.countPutMissMemory)
+ .append("\n").append("countPutNewFileValue", this.countPutNewFileValue)
+ .append("\n").append("countPutNewMemoryValue", this.countPutNewMemoryValue)
+ .append("\n").append("countPutReadFile", this.countPutReadFile)
+ .append("\n").append("countPutSerializable", this.countPutSerializable)
+ .append("\n").append("countPutWriteFile", this.countPutWriteFile)
+ .append("\n").append("countRemove", this.countRemove)
+ .append("\n").append("collector", this.collector)
.toString();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org