Synchronize cache (for real), updated dependencies

Closes #69
This commit is contained in:
Jikoo
2017-07-03 18:06:18 -04:00
parent d9abe76531
commit 784935a975
2 changed files with 33 additions and 30 deletions

View File

@@ -72,10 +72,10 @@ 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();
synchronized (internal) {
return internal.get(key); return internal.get(key);
} }
} }
@@ -87,10 +87,10 @@ 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();
synchronized (internal) {
return internal.containsKey(key); return internal.containsKey(key);
} }
} }
@@ -101,10 +101,10 @@ 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();
synchronized (internal) {
if (!internal.containsKey(key)) { if (!internal.containsKey(key)) {
// Value either not present or cleaned by lazy check. Either way, we're good // Value either not present or cleaned by lazy check. Either way, we're good
return; return;
@@ -143,7 +143,9 @@ public class Cache<K, V> {
private void lazyCheck() { private void lazyCheck() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long nextExpiry = now + retention; long nextExpiry = now + retention;
for (Iterator<Map.Entry<Long, K>> iterator = expiry.entries().iterator(); iterator.hasNext();) { synchronized (internal) {
for (Iterator<Map.Entry<Long, K>> iterator = expiry.entries().iterator(); iterator
.hasNext();) {
Map.Entry<Long, K> entry = iterator.next(); Map.Entry<Long, K> entry = iterator.next();
if (entry.getKey() > now) { if (entry.getKey() > now) {
@@ -166,5 +168,6 @@ public class Cache<K, V> {
postRemoval.run(value); postRemoval.run(value);
} }
} }
}
} }

View File

@@ -74,7 +74,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version> <version>3.0.0</version>
<configuration> <configuration>
<filters> <filters>
<filter> <filter>
@@ -98,7 +98,7 @@
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version> <version>3.6.1</version>
<configuration> <configuration>
<source>1.6</source> <source>1.6</source>
<target>1.6</target> <target>1.6</target>