mirror of
https://github.com/TotalFreedomMC/OpenInv.git
synced 2024-12-23 00:15:08 +00:00
parent
d9abe76531
commit
784935a975
2 changed files with 33 additions and 30 deletions
|
@ -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,27 +143,30 @@ 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) {
|
||||||
Map.Entry<Long, K> entry = iterator.next();
|
for (Iterator<Map.Entry<Long, K>> iterator = expiry.entries().iterator(); iterator
|
||||||
|
.hasNext();) {
|
||||||
|
Map.Entry<Long, K> entry = iterator.next();
|
||||||
|
|
||||||
if (entry.getKey() > now) {
|
if (entry.getKey() > now) {
|
||||||
break;
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
pom.xml
4
pom.xml
|
@ -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>
|
||||||
|
|
Loading…
Reference in a new issue