Skip to content

Commit 169f226

Browse files
committed
Merge pull request #3 from mmolimar/configurable_cache_size
Removing configurable cache size by env variable
2 parents 50fa133 + 36644bc commit 169f226

File tree

7 files changed

+47
-39
lines changed

7 files changed

+47
-39
lines changed

src/main/java/com/github/fge/jsonschema/core/load/SchemaLoader.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.github.fge.msgsimple.bundle.MessageBundle;
3333
import com.github.fge.msgsimple.load.MessageBundles;
3434
import com.google.common.cache.CacheBuilder;
35-
import com.google.common.cache.CacheBuilderSpec;
3635
import com.google.common.cache.CacheLoader;
3736
import com.google.common.cache.LoadingCache;
3837
import com.google.common.collect.ImmutableMap;
@@ -101,9 +100,11 @@ public SchemaLoader(final LoadingConfiguration cfg)
101100
manager = new URIManager(cfg);
102101
preloadedSchemas = ImmutableMap.copyOf(cfg.getPreloadedSchemas());
103102

104-
cache = CacheBuilder.newBuilder()
105-
.maximumSize(cfg.getEnableCache() ? cfg.getCacheSize() : 0) // cache size zero disables caching
106-
.build(new CacheLoader<URI, JsonNode>()
103+
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
104+
if (cfg.getCacheSize() != -1) {
105+
builder.maximumSize(cfg.getCacheSize());
106+
}
107+
cache = builder.build(new CacheLoader<URI, JsonNode>()
107108
{
108109
@Nonnull
109110
@Override

src/main/java/com/github/fge/jsonschema/core/load/configuration/LoadingConfiguration.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,11 @@ public final class LoadingConfiguration
8989
final URITranslatorConfiguration translatorCfg;
9090

9191
/**
92-
* Should we cache schemas
92+
* Cache size
9393
*
9494
* <p>Note that this do not affect preloaded schemas; these are always
9595
* cached.</p>
9696
*/
97-
final boolean enableCache;
98-
99-
/**
100-
* Cache size
101-
*
102-
* if enableCache is false, this field is ignored.
103-
* enableCache = true && cacheSize = 0 == enableCache = false
104-
*/
10597
final int cacheSize;
10698

10799
/**
@@ -173,7 +165,6 @@ public static LoadingConfiguration byDefault()
173165
preloadedSchemas = ImmutableMap.copyOf(builder.preloadedSchemas);
174166
parserFeatures = EnumSet.copyOf(builder.parserFeatures);
175167
reader = buildReader();
176-
enableCache = builder.enableCache;
177168
cacheSize = builder.cacheSize;
178169
}
179170

@@ -246,17 +237,20 @@ public JsonNodeReader getReader()
246237
* Return if we want to cache loaded schema or not
247238
* note that this do not affect preloadedSchema that are always cached
248239
*
240+
* @deprecated Use cacheSize getter instead to get the cache size
241+
*
249242
* @return if the cache has to be enabled
250243
*/
244+
@Deprecated
251245
public boolean getEnableCache() {
252-
return enableCache;
246+
return this.cacheSize != 0;
253247
}
254-
248+
255249
/**
256250
* Return the size of the cache to use
257251
* note that this do not affect preloadedSchema that are always cached
258252
*
259-
* @return the size of the cache, if enabled
253+
* @return the size of the cache. A zero-value means that it is not enabled
260254
*/
261255
public int getCacheSize() {
262256
return cacheSize;

src/main/java/com/github/fge/jsonschema/core/load/configuration/LoadingConfigurationBuilder.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public final class LoadingConfigurationBuilder
5757
* EnumSets to collect them, so we have to do that...
5858
*/
5959
private static final EnumSet<JsonParser.Feature> DEFAULT_PARSER_FEATURES;
60+
61+
private static final int DEFAULT_CACHE_SIZE = 512;
6062

6163
static {
6264
DEFAULT_PARSER_FEATURES = EnumSet.noneOf(JsonParser.Feature.class);
@@ -78,14 +80,9 @@ public final class LoadingConfigurationBuilder
7880
URITranslatorConfiguration translatorCfg;
7981

8082
/**
81-
* Loaded schemas are cached by default
82-
*/
83-
boolean enableCache = true;
84-
85-
/**
86-
* Cache size is 4096 by default
83+
* Cache size is 512 by default
8784
*/
88-
int cacheSize = 4096;
85+
int cacheSize = DEFAULT_CACHE_SIZE;
8986

9087
/**
9188
* Dereferencing mode
@@ -137,28 +134,31 @@ public final class LoadingConfigurationBuilder
137134
dereferencing = cfg.dereferencing;
138135
preloadedSchemas = Maps.newHashMap(cfg.preloadedSchemas);
139136
parserFeatures = EnumSet.copyOf(cfg.parserFeatures);
140-
enableCache = cfg.enableCache;
141137
cacheSize = cfg.cacheSize;
142138
}
143-
139+
144140
/**
145141
* Should we enable caching of downloaded schemas
142+
*
143+
* @deprecated Just for backward compatibility
144+
* Use cacheSize setter instead to set the maximum size of the cache
146145
*
147146
* <p>Note that this does <b>not</b> affect preloaded schemas</p>
148147
*
149148
* @param enableCache if loaded schemas have to be cached
150149
* @return this
151150
*/
151+
@Deprecated
152152
public LoadingConfigurationBuilder setEnableCache(final boolean enableCache)
153153
{
154-
this.enableCache = enableCache;
154+
this.cacheSize = enableCache ? DEFAULT_CACHE_SIZE : 0;
155155
return this;
156156
}
157157

158158
/**
159159
* How many schemas should be cached
160-
* <p>Note if enableCache is false this setting is ignored</p>
161-
* <p>Note setting enableCache to false or this to zero both effectively disable the cache</p>
160+
* <p>Note setting to zero effectively disables the cache</p>
161+
* <p>Note settting to -1 creates an unlimited cache</p>
162162
* <p>Note that this does <b>not</b> affect preloaded schemas</p>
163163
*
164164
* @param cacheSize if loaded schemas have to be cached

src/main/java/com/github/fge/jsonschema/core/processing/CachingProcessor.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public final class CachingProcessor<IN extends MessageProvider, OUT extends Mess
5353
private static final MessageBundle BUNDLE
5454
= MessageBundles.getBundle(JsonSchemaCoreMessageBundle.class);
5555

56-
public static final String CACHE_SIZE_PROPERTY_NAME = "com.github.fge.jsonschema.processorCacheSize";
57-
public static final int DEFAULT_CACHE_SIZE = 4096;
56+
private static final int DEFAULT_CACHE_SIZE = 512;
5857

5958
/**
6059
* The wrapped processor
@@ -89,7 +88,7 @@ public CachingProcessor(final Processor<IN, OUT> processor)
8988
public CachingProcessor(final Processor<IN, OUT> processor,
9089
final Equivalence<IN> equivalence)
9190
{
92-
this(processor, equivalence, Integer.getInteger(CACHE_SIZE_PROPERTY_NAME, DEFAULT_CACHE_SIZE));
91+
this(processor, equivalence, DEFAULT_CACHE_SIZE);
9392
}
9493
/**
9594
* Main constructor
@@ -104,11 +103,14 @@ public CachingProcessor(final Processor<IN, OUT> processor,
104103
{
105104
BUNDLE.checkNotNull(processor, "processing.nullProcessor");
106105
BUNDLE.checkNotNull(equivalence, "processing.nullEquivalence");
106+
BUNDLE.checkArgument(cacheSize >= -1, "processing.invalidCacheSize");
107107
this.processor = processor;
108108
this.equivalence = equivalence;
109-
cache = CacheBuilder.newBuilder()
110-
.maximumSize(cacheSize)
111-
.build(loader());
109+
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
110+
if (cacheSize != -1) {
111+
builder.maximumSize(cacheSize);
112+
}
113+
cache = builder.build(loader());
112114
}
113115

114116
@Override

src/main/resources/com/github/fge/jsonschema/core/core.properties

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ processing.nullLevel = log level must not be null
4848
processing.nullPredicate = predicate cannot be null
4949
processing.nullProcessor = processor cannot be null
5050
processing.nullReport = report cannot be null
51+
processing.invalidCacheSize = cache size must be greater than -1. -1 value sets a cache with unlimited records, zero-value disables the cache
5152
refProcessing.danglingRef = JSON Reference "%s" cannot be resolved
5253
refProcessing.refLoop = JSON Reference "%s" loops on itself
5354
refProcessing.unhandledScheme = URI scheme "%s" not supported (URI: "%s")

src/test/java/com/github/fge/jsonschema/core/load/SchemaLoaderTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void preloadedSchemasAreNotFetchedAgain()
141141
verify(mock, never()).fetch(uri);
142142

143143
//even if cache is disabled
144-
cfg = builder.setEnableCache(false).freeze();
144+
cfg = builder.setCacheSize(0).freeze();
145145
registry = new SchemaLoader(cfg);
146146
registry.get(uri);
147147
verify(mock, never()).fetch(uri);
@@ -184,7 +184,7 @@ public InputStream fetch(final URI source)
184184

185185
final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
186186
.addScheme("foo", downloader)
187-
.setEnableCache(false)
187+
.setCacheSize(0)
188188
.freeze();
189189
final SchemaLoader loader = new SchemaLoader(cfg);
190190

@@ -210,7 +210,6 @@ public InputStream fetch(final URI source)
210210

211211
final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
212212
.addScheme("foo", downloader)
213-
.setEnableCache(true)
214213
.setCacheSize(0)
215214
.freeze();
216215
final SchemaLoader loader = new SchemaLoader(cfg);

src/test/java/com/github/fge/jsonschema/core/processing/CachingProcessorTest.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.github.fge.jsonschema.core.util.equivalence.Equivalences;
2727
import com.github.fge.msgsimple.bundle.MessageBundle;
2828
import com.github.fge.msgsimple.load.MessageBundles;
29-
import com.google.common.cache.CacheBuilder;
3029
import org.testng.annotations.BeforeMethod;
3130
import org.testng.annotations.Test;
3231

@@ -75,6 +74,18 @@ public void cannotInputNullEquivalence()
7574
}
7675
}
7776

77+
@Test
78+
public void cannotInputInvalidCacheSize()
79+
{
80+
try {
81+
new CachingProcessor<In, Out>(processor, Equivalences.<In>identity(), -2);
82+
fail("No exception thrown!!");
83+
} catch (IllegalArgumentException e) {
84+
assertEquals(e.getMessage(),
85+
BUNDLE.getMessage("processing.invalidCacheSize"));
86+
}
87+
}
88+
7889
@Test
7990
public void cachedValueIsNotProcessedTwiceButReportedTwice()
8091
throws ProcessingException

0 commit comments

Comments
 (0)