Kalle Stenflo
9 years ago
13 changed files with 236 additions and 25 deletions
@ -0,0 +1,23 @@
|
||||
package com.jayway.jsonpath.spi.cache; |
||||
|
||||
import com.jayway.jsonpath.InvalidJsonException; |
||||
import com.jayway.jsonpath.JsonPath; |
||||
|
||||
public interface Cache { |
||||
|
||||
/** |
||||
* Get the Cached JsonPath |
||||
* @param key cache key to lookup the JsonPath |
||||
* @return JsonPath |
||||
*/ |
||||
public JsonPath get(String key); |
||||
|
||||
/** |
||||
* Add JsonPath to the cache |
||||
* @param key cache key to store the JsonPath |
||||
* @param value JsonPath to be cached |
||||
* @return void |
||||
* @throws InvalidJsonException |
||||
*/ |
||||
public void put(String key, JsonPath value); |
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.jayway.jsonpath.spi.cache; |
||||
|
||||
import com.jayway.jsonpath.JsonPathException; |
||||
|
||||
public class CacheProvider { |
||||
private static Cache cache = new LRUCache(200); |
||||
|
||||
public static void setCache(Cache cache){ |
||||
if (cache != null){ |
||||
CacheProvider.cache = cache; |
||||
} |
||||
} |
||||
|
||||
public static Cache getCache() { |
||||
try { |
||||
return cache; |
||||
} catch (Exception e) { |
||||
throw new JsonPathException("Failed to get cache", e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,112 @@
|
||||
/* |
||||
* Copyright 2011 the original author or authors. |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.jayway.jsonpath.spi.cache; |
||||
|
||||
import com.jayway.jsonpath.JsonPath; |
||||
|
||||
import java.util.Deque; |
||||
import java.util.LinkedList; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import java.util.concurrent.locks.ReentrantLock; |
||||
|
||||
public class LRUCache implements Cache { |
||||
|
||||
private final ReentrantLock lock = new ReentrantLock(); |
||||
|
||||
private final Map<String, JsonPath> map = new ConcurrentHashMap<String, JsonPath>(); |
||||
private final Deque<String> queue = new LinkedList<String>(); |
||||
private final int limit; |
||||
|
||||
public LRUCache(int limit) { |
||||
this.limit = limit; |
||||
} |
||||
|
||||
public void put(String key, JsonPath value) { |
||||
JsonPath oldValue = map.put(key, value); |
||||
if (oldValue != null) { |
||||
removeThenAddKey(key); |
||||
} else { |
||||
addKey(key); |
||||
} |
||||
if (map.size() > limit) { |
||||
map.remove(removeLast()); |
||||
} |
||||
} |
||||
|
||||
public JsonPath get(String key) { |
||||
JsonPath jsonPath = map.get(key); |
||||
if(jsonPath != null){ |
||||
removeThenAddKey(key); |
||||
} |
||||
return jsonPath; |
||||
} |
||||
|
||||
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(); |
||||
} |
||||
} |
||||
|
||||
public JsonPath getSilent(String key) { |
||||
return map.get(key); |
||||
} |
||||
|
||||
public void remove(String key) { |
||||
removeFirstOccurrence(key); |
||||
map.remove(key); |
||||
} |
||||
|
||||
public int size() { |
||||
return map.size(); |
||||
} |
||||
|
||||
public String toString() { |
||||
return map.toString(); |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.jayway.jsonpath.spi.cache; |
||||
|
||||
import com.jayway.jsonpath.JsonPath; |
||||
|
||||
public class NOOPCache implements Cache { |
||||
@Override |
||||
public JsonPath get(String key) { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public void put(String key, JsonPath value) { |
||||
} |
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.jayway.jsonpath.internal; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.assertj.core.api.Assertions; |
||||
import org.junit.Test; |
||||
|
||||
import com.jayway.jsonpath.BaseTest; |
||||
import com.jayway.jsonpath.Criteria; |
||||
import com.jayway.jsonpath.DocumentContext; |
||||
import com.jayway.jsonpath.Filter; |
||||
import com.jayway.jsonpath.JsonPath; |
||||
|
||||
public class JsonReaderTest extends BaseTest { |
||||
|
||||
@Test |
||||
public void cached_path_with_predicates() { |
||||
|
||||
Filter feq = Filter.filter(Criteria.where("category").eq("reference")); |
||||
Filter fne = Filter.filter(Criteria.where("category").ne("reference")); |
||||
|
||||
DocumentContext JsonDoc = JsonPath.parse(JSON_DOCUMENT); |
||||
|
||||
List<String> eq = JsonDoc.read("$.store.book[?].category", feq); |
||||
List<String> ne = JsonDoc.read("$.store.book[?].category", fne); |
||||
|
||||
Assertions.assertThat(eq).contains("reference"); |
||||
Assertions.assertThat(ne).doesNotContain("reference"); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue