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
*/
public V get(K key) {
synchronized (internal) {
// Run lazy check to clean cache
lazyCheck();
// Run lazy check to clean cache
lazyCheck();
synchronized (internal) {
return internal.get(key);
}
}
@ -87,10 +87,10 @@ public class Cache<K, V> {
* @return true if a mapping exists for the specified key
*/
public boolean containsKey(K key) {
synchronized (internal) {
// Run lazy check to clean cache
lazyCheck();
// Run lazy check to clean cache
lazyCheck();
synchronized (internal) {
return internal.containsKey(key);
}
}
@ -101,10 +101,10 @@ public class Cache<K, V> {
* @param key key to invalidate
*/
public void invalidate(K key) {
synchronized (internal) {
// Run lazy check to clean cache
lazyCheck();
// Run lazy check to clean cache
lazyCheck();
synchronized (internal) {
if (!internal.containsKey(key)) {
// Value either not present or cleaned by lazy check. Either way, we're good
return;
@ -143,27 +143,30 @@ public class Cache<K, V> {
private void lazyCheck() {
long now = System.currentTimeMillis();
long nextExpiry = now + retention;
for (Iterator<Map.Entry<Long, K>> iterator = expiry.entries().iterator(); iterator.hasNext();) {
Map.Entry<Long, K> entry = iterator.next();
synchronized (internal) {
for (Iterator<Map.Entry<Long, K>> iterator = expiry.entries().iterator(); iterator
.hasNext();) {
Map.Entry<Long, K> entry = iterator.next();
if (entry.getKey() > now) {
break;
if (entry.getKey() > now) {
break;
}
iterator.remove();
if (inUseCheck.run(internal.get(entry.getValue()))) {
expiry.put(nextExpiry, entry.getValue());
continue;
}
V value = internal.remove(entry.getValue());
if (value == null) {
continue;
}
postRemoval.run(value);
}
iterator.remove();
if (inUseCheck.run(internal.get(entry.getValue()))) {
expiry.put(nextExpiry, entry.getValue());
continue;
}
V value = internal.remove(entry.getValue());
if (value == null) {
continue;
}
postRemoval.run(value);
}
}

View file

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