@ -14,96 +14,41 @@
* /
package com.jayway.jsonpath.internal ;
import java.util.Deque ;
import java.util.LinkedList ;
import java.util.Map ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.locks.ReentrantLock ;
import javax.cache.Caching ;
import javax.cache.configuration.MutableConfiguration ;
public class Cache {
private final ReentrantLock lock = new ReentrantLock ( ) ;
private final Map < String , Path > map = new ConcurrentHashMap < String , Path > ( ) ;
private final Deque < String > queue = new LinkedList < String > ( ) ;
private final int limit ;
public Cache ( int limit ) {
this . limit = limit ;
/ * *
* Wrapped JCache .
* /
private final javax . cache . Cache < String , Path > jcache ;
public Cache ( ) {
javax . cache . Cache < String , Path > existingJcache = Caching . getCache ( "jsonpath" , String . class , Path . class ) ;
if ( existingJcache = = null ) {
final MutableConfiguration < String , Path > configuration = new MutableConfiguration < String , Path > ( ) ;
configuration . setTypes ( String . class , Path . class ) ;
configuration . setStoreByValue ( false ) ;
existingJcache = Caching . getCachingProvider ( ) . getCacheManager ( ) . createCache ( "jsonpath" , configuration ) ;
}
jcache = existingJcache ;
}
public void put ( String key , Path value ) {
Path oldValue = map . put ( key , value ) ;
if ( oldValue ! = null ) {
removeThenAddKey ( key ) ;
} else {
addKey ( key ) ;
}
if ( map . size ( ) > limit ) {
map . remove ( removeLast ( ) ) ;
}
jcache . put ( key , value ) ;
}
public Path get ( String key ) {
if ( map . containsKey ( key ) ) {
removeThenAddKey ( key ) ;
}
return map . get ( key ) ;
}
private void addKey ( String key ) {
lock . lock ( ) ;
try {
queue . addFirst ( key ) ;
} finally {
lock . unlock ( ) ;
}
}
private String removeLast ( ) {
lock . lock ( ) ;
try {
final String removedKey = queue . removeLast ( ) ;
return removedKey ;
} finally {
lock . unlock ( ) ;
}
}
private void removeThenAddKey ( String key ) {
lock . lock ( ) ;
try {
queue . removeFirstOccurrence ( key ) ;
queue . addFirst ( key ) ;
} finally {
lock . unlock ( ) ;
}
}
private void removeFirstOccurrence ( String key ) {
lock . lock ( ) ;
try {
queue . removeFirstOccurrence ( key ) ;
} finally {
lock . unlock ( ) ;
}
return jcache . get ( key ) ;
}
public Path getSilent ( String key ) {
return map . get ( key ) ;
return get ( key ) ;
}
public void remove ( String key ) {
removeFirstOccurrence ( key ) ;
map . remove ( key ) ;
jcache . remove ( key ) ;
}
public int size ( ) {
return map . size ( ) ;
}
public String toString ( ) {
return map . toString ( ) ;
}
}