Details
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/Cache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/Cache.java
index 35c1aa9..8336a09 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/Cache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/Cache.java
@@ -15,9 +15,9 @@ import java.util.function.Supplier;
*/
public interface Cache<K, V> {
- public V put(K key, V value, long timeToLive);
+ public void put(K key, V value, long timeToLive);
- public V put(K key, V value);
+ public void put(K key, V value);
public V get(K key);
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java b/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
new file mode 100644
index 0000000..ea9d275
--- /dev/null
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/CacheEvent.java
@@ -0,0 +1,78 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package br.ufrgs.inf.prosoft.cache;
+
+/**
+ *
+ * @author romulo
+ */
+public class CacheEvent {
+
+ private final long time;
+ private final String type;
+ private final int size;
+
+ private CacheEvent(String type) {
+ this(type, 0);
+ }
+
+ private CacheEvent(String type, int size) {
+ this(System.currentTimeMillis(), type, size);
+ }
+
+ private CacheEvent(long time, String type) {
+ this(time, type, 0);
+ }
+
+ private CacheEvent(long time, String type, int size) {
+ this.time = time;
+ this.type = type;
+ this.size = size;
+ }
+
+ public static CacheEvent miss() {
+ return new CacheEvent("MISS");
+ }
+
+ public static CacheEvent addition() {
+ return new CacheEvent("ADDITION");
+ }
+
+ public static CacheEvent addition(int size) {
+ return new CacheEvent("ADDITION", size);
+ }
+
+ public static CacheEvent hit() {
+ return new CacheEvent("HIT");
+ }
+
+ public static CacheEvent hit(int size) {
+ return new CacheEvent("HIT", size);
+ }
+
+ public static CacheEvent invalidation() {
+ return new CacheEvent("INVALIDATION");
+ }
+
+ public static CacheEvent invalidation(int size) {
+ return new CacheEvent("INVALIDATION", size);
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append("{");
+ stringBuilder.append("\"time\":").append(this.time);
+ stringBuilder.append(",");
+ stringBuilder.append("\"type\":").append("\"").append(this.type).append("\"");
+ if (this.size > 0) {
+ stringBuilder.append(",");
+ stringBuilder.append("\"size\":").append(this.size);
+ }
+ stringBuilder.append("}");
+ return stringBuilder.toString();
+ }
+}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/GetterCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/GetterCache.java
index 997985e..d09a5de 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/GetterCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/GetterCache.java
@@ -6,90 +6,47 @@
package br.ufrgs.inf.prosoft.cache;
import java.util.function.Supplier;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
*
* @author romulo
* @param <V>
*/
-public class GetterCache<V> {
-
- private final CachingPerformance cachingPerformance;
- private V result;
- private boolean isFilled;
+public class GetterCache<V> extends MultiCache<Void, V> {
public GetterCache() {
- this.isFilled = false;
- this.cachingPerformance = new CachingPerformance();
+ super();
}
public GetterCache(String name) {
- this.isFilled = false;
- this.cachingPerformance = new CachingPerformance(name);
+ super(name);
}
- public GetterCache(CachingPerformance cachingPerformance) {
- this.isFilled = false;
- this.cachingPerformance = cachingPerformance;
+ public GetterCache(CachePerformance cachingPerformance) {
+ super(cachingPerformance);
}
- public V put(V value, long timeToLive) {
- V put = put(value);
- Thread thread = new Thread(() -> {
- try {
- Thread.sleep(timeToLive);
- } catch (InterruptedException ex) {
- Logger.getLogger(SingleCache.class.getName()).log(Level.SEVERE, "interrupted time to live");
- }
- this.result = null;
- this.isFilled = false;
- });
- thread.start();
- return put;
+ public void put(V value, long timeToLive) {
+ super.put(null, value, timeToLive);
}
- public V put(V value) {
- this.result = value;
- this.isFilled = true;
- this.cachingPerformance.registerSize(1);
- return this.result;
+ public void put(V value) {
+ super.put(null, value);
}
public V get() {
- if (this.isFilled == false) {
- this.cachingPerformance.registerMiss();
- return null;
- }
- this.cachingPerformance.registerBytesHit(CachingPerformance.calculateObjectSize(this.result));
- this.cachingPerformance.registerHit();
- return this.result;
+ return super.get(null);
}
public void invalidate() {
- this.isFilled = false;
- this.result = null;
- this.cachingPerformance.registerInvalidation();
+ super.invalidate(null);
}
public V computeIfAbsent(Supplier<V> supplier, long timeToLive) {
- V get = get();
- if (get != null) {
- return get;
- }
- get = supplier.get();
- put(get, timeToLive);
- return get;
+ return super.computeIfAbsent(null, supplier, timeToLive);
}
public V computeIfAbsent(Supplier<V> supplier) {
- V get = get();
- if (get != null) {
- return get;
- }
- get = supplier.get();
- put(get);
- return get;
+ return super.computeIfAbsent(null, supplier);
}
}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
index 43d3cab..ae70731 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/MultiCache.java
@@ -17,50 +17,52 @@ import java.util.logging.Logger;
*/
public class MultiCache<K, V> implements Cache<K, V> {
+ private static final boolean CACHE_REGISTER_SIZE = System.getenv("CACHE_REGISTER_SIZE") != null && System.getenv("CACHE_REGISTER_SIZE").equals("true");
private final HashMap<K, V> map;
- private final CachingPerformance cachingPerformance;
+ private final CachePerformance cachingPerformance;
+ private static final Logger LOGGER = Logger.getLogger(Cache.class.getName());
public MultiCache() {
- this.cachingPerformance = new CachingPerformance();
- this.map = new HashMap<>();
+ this(new CachePerformance());
}
public MultiCache(String name) {
- this.cachingPerformance = new CachingPerformance(name);
- this.map = new HashMap<>();
+ this(new CachePerformance(name));
}
- public MultiCache(CachingPerformance cachingPerformance) {
+ public MultiCache(CachePerformance cachingPerformance) {
this.cachingPerformance = cachingPerformance;
this.map = new HashMap<>();
}
- public CachingPerformance getCachingPerformance() {
+ public CachePerformance getCachingPerformance() {
return this.cachingPerformance;
}
@Override
- public V put(K key, V value, long timeToLive) {
- V put = put(key, value);
+ public void put(K key, V value, long timeToLive) {
+ put(key, value);
Thread thread = new Thread(() -> {
try {
Thread.sleep(timeToLive);
} catch (InterruptedException ex) {
- Logger.getLogger(MultiCache.class.getName()).log(Level.SEVERE, "interrupted time to live");
+ LOGGER.log(Level.SEVERE, "interrupted time to live");
}
- this.map.remove(key);
+ invalidate(key);
});
thread.start();
- return put;
}
@Override
- public V put(K key, V value) {
- V oldReference = this.map.put(key, value);
- if (oldReference == null) {
- this.cachingPerformance.registerSize(this.map.size());
+ public void put(K key, V value) {
+ invalidate(key);
+ this.map.put(key, value);
+ if (CACHE_REGISTER_SIZE) {
+ this.cachingPerformance.registerAddition(CachePerformance.calculateObjectSize(value));
+ } else {
+ this.cachingPerformance.registerAddition();
}
- return oldReference;
+ this.cachingPerformance.registerSize(this.map.size());
}
@Override
@@ -69,8 +71,11 @@ public class MultiCache<K, V> implements Cache<K, V> {
if (get == null) {
this.cachingPerformance.registerMiss();
} else {
- this.cachingPerformance.registerHit();
- this.cachingPerformance.registerBytesHit(CachingPerformance.calculateObjectSize(get));
+ if (CACHE_REGISTER_SIZE) {
+ this.cachingPerformance.registerHit(CachePerformance.calculateObjectSize(get));
+ } else {
+ this.cachingPerformance.registerHit();
+ }
}
return get;
}
@@ -79,8 +84,11 @@ public class MultiCache<K, V> implements Cache<K, V> {
public void invalidate(K key) {
V remove = this.map.remove(key);
if (remove != null) {
- this.cachingPerformance.registerInvalidation();
+ if (CACHE_REGISTER_SIZE) {
+ this.cachingPerformance.registerInvalidation(CachePerformance.calculateObjectSize(remove));
+ } else {
+ this.cachingPerformance.registerInvalidation();
+ }
}
}
-
}
diff --git a/src/main/java/br/ufrgs/inf/prosoft/cache/SingleCache.java b/src/main/java/br/ufrgs/inf/prosoft/cache/SingleCache.java
index 37c32d9..34d9c51 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/cache/SingleCache.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/cache/SingleCache.java
@@ -5,77 +5,23 @@
*/
package br.ufrgs.inf.prosoft.cache;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
/**
*
* @author romulo
* @param <K>
* @param <V>
*/
-public class SingleCache<K, V> implements Cache<K, V> {
-
- private final CachingPerformance cachingPerformance;
- private K lastInput;
- private V result;
+public class SingleCache<K, V> extends MultiCache<K, V> {
public SingleCache() {
- this.cachingPerformance = new CachingPerformance();
+ super();
}
public SingleCache(String name) {
- this.cachingPerformance = new CachingPerformance(name);
- }
-
- public SingleCache(CachingPerformance cachingPerformance) {
- this.cachingPerformance = cachingPerformance;
+ super(name);
}
- public CachingPerformance getCachingPerformance() {
- return this.cachingPerformance;
+ public SingleCache(CachePerformance cachingPerformance) {
+ super(cachingPerformance);
}
-
- @Override
- public V put(K key, V value, long timeToLive) {
- V put = put(key, value);
- Thread thread = new Thread(() -> {
- try {
- Thread.sleep(timeToLive);
- } catch (InterruptedException ex) {
- Logger.getLogger(SingleCache.class.getName()).log(Level.SEVERE, "interrupted time to live");
- }
- this.lastInput = null;
- this.result = null;
- });
- thread.start();
- return put;
- }
-
- @Override
- public V put(K key, V value) {
- this.lastInput = key;
- this.result = value;
- this.cachingPerformance.registerSize(1);
- return this.result;
- }
-
- @Override
- public V get(K key) {
- if (key.equals(this.lastInput)) {
- this.cachingPerformance.registerHit();
- this.cachingPerformance.registerBytesHit(CachingPerformance.calculateObjectSize(this.result));
- return this.result;
- }
- this.cachingPerformance.registerMiss();
- return null;
- }
-
- @Override
- public void invalidate(K key) {
- this.lastInput = null;
- this.result = null;
- this.cachingPerformance.registerInvalidation();
- }
-
}