From 191d9342cd2965c1a1e67d7b02fe46418a764e2b Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Tue, 13 Oct 2015 01:18:01 -0400 Subject: [PATCH] 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. --- build.gradle | 2 + json-path/build.gradle | 2 + .../com/jayway/jsonpath/internal/Cache.java | 95 ++++--------------- .../com/jayway/jsonpath/internal/Path.java | 4 +- .../jsonpath/internal/PathCompiler.java | 2 +- .../com/jayway/jsonpath/old/IssuesTest.java | 9 +- 6 files changed, 31 insertions(+), 83 deletions(-) diff --git a/build.gradle b/build.gradle index 5dc386f7..e3d992c2 100644 --- a/build.gradle +++ b/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', diff --git a/json-path/build.gradle b/json-path/build.gradle index 9686433d..fa7ee0bd 100644 --- a/json-path/build.gradle +++ b/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 diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java b/json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java index 7e680895..853e8659 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java +++ b/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 map = new ConcurrentHashMap(); - private final Deque queue = new LinkedList(); - private final int limit; - - public Cache(int limit) { - this.limit = limit; + /** + * Wrapped JCache. + */ + private final javax.cache.Cache jcache; + + public Cache() { + javax.cache.Cache existingJcache = Caching.getCache("jsonpath", String.class, Path.class); + if (existingJcache == null) { + final MutableConfiguration configuration = new MutableConfiguration(); + 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(); - } } \ No newline at end of file diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java b/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java index b87b7b55..fb35946d 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java +++ b/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 { /** diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java index 78e6a65f..712149c0 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java +++ b/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 filterStack; private final CharacterIndex path; diff --git a/json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java b/json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java index 3336ee28..bb0e97b3 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java +++ b/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