Synchronize cache (#63)

This commit is contained in:
Jikoo 2017-02-13 19:56:32 -05:00
parent 7ab0003b62
commit 443e0c489e

View file

@ -9,7 +9,7 @@ import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap; import com.google.common.collect.TreeMultimap;
/** /**
* A minimal time-based cache implementation backed by a HashMap and TreeMultimap. * A minimal thread-safe time-based cache implementation backed by a HashMap and TreeMultimap.
* *
* @author Jikoo * @author Jikoo
*/ */
@ -59,9 +59,11 @@ public class Cache<K, V> {
// Invalidate key - runs lazy check and ensures value won't be cleaned up early // Invalidate key - runs lazy check and ensures value won't be cleaned up early
invalidate(key); invalidate(key);
synchronized (internal) {
internal.put(key, value); internal.put(key, value);
expiry.put(System.currentTimeMillis() + retention, key); expiry.put(System.currentTimeMillis() + retention, key);
} }
}
/** /**
* Returns the value to which the specified key is mapped, or null if no value is mapped for the key. * Returns the value to which the specified key is mapped, or null if no value is mapped for the key.
@ -70,11 +72,13 @@ public class Cache<K, V> {
* @return the value to which the specified key is mapped, or null if no value is mapped for the key * @return the value to which the specified key is mapped, or null if no value is mapped for the key
*/ */
public V get(K key) { public V get(K key) {
synchronized (internal) {
// Run lazy check to clean cache // Run lazy check to clean cache
lazyCheck(); lazyCheck();
return internal.get(key); return internal.get(key);
} }
}
/** /**
* Returns true if the specified key is mapped to a value. * Returns true if the specified key is mapped to a value.
@ -83,11 +87,13 @@ public class Cache<K, V> {
* @return true if a mapping exists for the specified key * @return true if a mapping exists for the specified key
*/ */
public boolean containsKey(K key) { public boolean containsKey(K key) {
synchronized (internal) {
// Run lazy check to clean cache // Run lazy check to clean cache
lazyCheck(); lazyCheck();
return internal.containsKey(key); return internal.containsKey(key);
} }
}
/** /**
* Forcibly invalidates a key, even if it is considered to be in use. * Forcibly invalidates a key, even if it is considered to be in use.
@ -95,6 +101,7 @@ public class Cache<K, V> {
* @param key key to invalidate * @param key key to invalidate
*/ */
public void invalidate(K key) { public void invalidate(K key) {
synchronized (internal) {
// Run lazy check to clean cache // Run lazy check to clean cache
lazyCheck(); lazyCheck();
@ -114,17 +121,20 @@ public class Cache<K, V> {
} }
} }
} }
}
/** /**
* Forcibly invalidates all keys, even if they are considered to be in use. * Forcibly invalidates all keys, even if they are considered to be in use.
*/ */
public void invalidateAll() { public void invalidateAll() {
synchronized (internal) {
for (V value : internal.values()) { for (V value : internal.values()) {
postRemoval.run(value); postRemoval.run(value);
} }
expiry.clear(); expiry.clear();
internal.clear(); internal.clear();
} }
}
/** /**
* Invalidate all expired keys that are not considered in use. If a key is expired but is * Invalidate all expired keys that are not considered in use. If a key is expired but is