From 9c7bfc677a96cfc60713f40e339fd991d7697014 Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Wed, 19 May 2010 20:47:27 +0000 Subject: [PATCH] Allow cache size to be configured. --- gson/pom.xml | 4 ++-- .../java/com/google/gson/FieldAttributes.java | 17 +++++++++++++++-- .../test/java/com/google/gson/LruCacheTest.java | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/gson/pom.xml b/gson/pom.xml index 46bb1cea..a91e7355 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -134,7 +134,7 @@ - + org.apache.maven.plugins maven-jxr-plugin diff --git a/gson/src/main/java/com/google/gson/FieldAttributes.java b/gson/src/main/java/com/google/gson/FieldAttributes.java index 870b168d..c278288f 100644 --- a/gson/src/main/java/com/google/gson/FieldAttributes.java +++ b/gson/src/main/java/com/google/gson/FieldAttributes.java @@ -34,9 +34,11 @@ import java.util.Collections; * @since 1.4 */ public final class FieldAttributes { - // TODO(Joel): Fix how we configure this cache in a follow-up CL. + private static final String MAX_CACHE_PROPERTY_NAME = + "com.google.gson.annotation_cache_size_hint"; + private static final Cache, String>, Collection> ANNOTATION_CACHE = - new LruCache,String>, Collection>(1500); + new LruCache,String>, Collection>(getMaxCacheSize()); private final Class parentClazz; private final Field field; @@ -64,6 +66,17 @@ public final class FieldAttributes { field = f; } + private static int getMaxCacheSize() { + final int defaultMaxCacheSize = 2000; + try { + String propertyValue = System.getProperty( + MAX_CACHE_PROPERTY_NAME, String.valueOf(defaultMaxCacheSize)); + return Integer.parseInt(propertyValue); + } catch (NumberFormatException e) { + return defaultMaxCacheSize; + } + } + /** * @return the name of the field */ diff --git a/gson/src/test/java/com/google/gson/LruCacheTest.java b/gson/src/test/java/com/google/gson/LruCacheTest.java index 4725db9a..dbf5034d 100644 --- a/gson/src/test/java/com/google/gson/LruCacheTest.java +++ b/gson/src/test/java/com/google/gson/LruCacheTest.java @@ -53,21 +53,30 @@ public class LruCacheTest extends TestCase { } public void testCacheEviction() throws Exception { - Cache cache = new LruCache(3); + Cache cache = new LruCache(5); cache.addElement("key1", 1); cache.addElement("key2", 2); cache.addElement("key3", 3); + cache.addElement("key4", 4); + cache.addElement("key5", 5); assertEquals(1, cache.getElement("key1").intValue()); assertEquals(2, cache.getElement("key2").intValue()); assertEquals(3, cache.getElement("key3").intValue()); + assertEquals(4, cache.getElement("key4").intValue()); + assertEquals(5, cache.getElement("key5").intValue()); // Access key1 to show key2 will be evicted (shows not a FIFO cache) cache.getElement("key1"); - cache.addElement("key4", 4); + cache.getElement("key3"); + cache.addElement("key6", 6); + cache.addElement("key7", 7); assertEquals(1, cache.getElement("key1").intValue()); assertNull(cache.getElement("key2")); assertEquals(3, cache.getElement("key3").intValue()); - assertEquals(4, cache.getElement("key4").intValue()); + assertNull(cache.getElement("key4")); + assertEquals(5, cache.getElement("key5").intValue()); + assertEquals(6, cache.getElement("key6").intValue()); + assertEquals(7, cache.getElement("key7").intValue()); } }