Browse Source

Converted Cache to use the JCache API

Cache simply wraps the JCache API in order to prevent too many changes to
the existing code base.

This adds JCache API and JCache Rerefence Implementation dependencies.
The Reference Implementation (RI) dependency is optional.  On production
systems you may use the one provided by your container or use Hazelcast.

Removed issue_94 tests have been updated to accomodate the changes to the
Cache API.
pull/138/head
Archimedes Trajano 9 years ago
parent
commit
191d9342cd
  1. 2
      build.gradle
  2. 2
      json-path/build.gradle
  3. 93
      json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/Path.java
  5. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java
  6. 9
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

2
build.gradle

@ -15,6 +15,8 @@ ext {
jsonSmart: 'net.minidev:json-smart:2.2',
jacksonDatabind: 'com.fasterxml.jackson.core:jackson-databind:2.6.1',
gson: 'com.google.code.gson:gson:2.3.1',
jcacheApi: 'javax.cache:cache-api:1.0.0',
jcacheRi: 'org.jsr107.ri:cache-ri-impl:1.0.0',
hamcrestCore: 'org.hamcrest:hamcrest-core:1.3',
hamcrestLibrary: 'org.hamcrest:hamcrest-library:1.3',

2
json-path/build.gradle

@ -16,6 +16,8 @@ jar {
dependencies {
compile libs.jsonSmart
compile libs.slf4jApi
compile libs.jcacheApi
compile libs.jcacheRi, optional
compile libs.jacksonDatabind, optional
compile libs.gson, optional

93
json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java

@ -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;
/**
* Wrapped JCache.
*/
private final javax.cache.Cache<String, Path> jcache;
public Cache(int limit) {
this.limit = limit;
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();
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/Path.java

@ -16,10 +16,12 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration;
import java.io.Serializable;
/**
*
*/
public interface Path {
public interface Path extends Serializable {
/**

2
json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java

@ -36,7 +36,7 @@ public class PathCompiler {
private static final char ESCAPE = '\\';
private static final char TICK = '\'';
private static final Cache cache = new Cache(200);
private static final Cache cache = new Cache();
private final LinkedList<Predicate> filterStack;
private final CharacterIndex path;

9
json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

@ -605,21 +605,18 @@ public class IssuesTest extends BaseTest {
@Test
public void issue_94_1() throws Exception {
Cache cache = new Cache(200);
Cache cache = new Cache();
Path dummy = new CompiledPath(null, false);
for (int i = 0; i < 10000; ++i) {
String key = String.valueOf(i);
cache.get(key);
cache.put(key, dummy);
}
Thread.sleep(2000);
Assertions.assertThat(cache.size()).isEqualTo(200);
}
@Test
public void issue_94_2() throws Exception {
Cache cache = new Cache(5);
Cache cache = new Cache();
Path dummy = new CompiledPath(null, false);
@ -662,7 +659,7 @@ public class IssuesTest extends BaseTest {
Assertions.assertThat(cache.getSilent("4")).isNotNull();
Assertions.assertThat(cache.getSilent("3")).isNotNull();
Assertions.assertThat(cache.getSilent("2")).isNotNull();
Assertions.assertThat(cache.getSilent("1")).isNull();
Assertions.assertThat(cache.getSilent("1")).isNotNull();
}
@Test

Loading…
Cancel
Save