From af019701aaf9b13d413c7f6131da055466749683 Mon Sep 17 00:00:00 2001 From: Pablo Diaz Date: Sat, 26 Dec 2015 20:37:52 +0100 Subject: [PATCH 1/3] Put some generics in play --- README.md | 22 ++++++----- .../com/fewlaps/quitnowcache/QNCache.java | 37 +++++++++---------- .../com/fewlaps/quitnowcache/QNCacheBean.java | 8 ++-- .../fewlaps/quitnowcache/QNCacheBuilder.java | 4 +- .../com/fewlaps/quitnowcache/BuilderTest.java | 18 ++++----- .../fewlaps/quitnowcache/CacheBeanTest.java | 10 ++--- .../quitnowcache/CaseSensitiveKeyTest.java | 6 +-- .../com/fewlaps/quitnowcache/CastTest.java | 34 +---------------- .../quitnowcache/DefaultKeepaliveTest.java | 10 ++--- .../quitnowcache/IntroducingQNCacheTest.java | 6 +-- .../fewlaps/quitnowcache/MassiveDataTest.java | 6 +-- .../quitnowcache/MemoryReleaseTest.java | 4 +- .../quitnowcache/SetAndGetValuesTest.java | 7 +++- .../com/fewlaps/quitnowcache/SizeTest.java | 7 +++- .../fewlaps/quitnowcache/ThreadSafeTest.java | 3 +- 15 files changed, 80 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index fbed9a2..a2e352a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The sample ---------- ```java -QNCache cache = new QNCacheBuilder().createQNCache(); +QNCache cache = new QNCacheBuilder().createQNCache(); cache.set("key", "value", 60 * 1000); // It can store things for a minute, cache.set("key", "value", 60 * 60 * 1000); // for an hour, @@ -27,8 +27,10 @@ cache.remove("key"); // and remove it if you want. cache.get("unExistingKey"); // If something doesn't exists, it returns null cache.get("tooOldKey"); // The same if a key is too old -cache.set("AnInteger", new Integer(42)); // You can save all kind of Objects... -cache.set("ACollection", new ArrayList()); // ...whatever you want +cache.set("AnInteger", new Integer(42)); // It is typesafe. This is a compile error + +QNCache objectCache = new QNCacheBuilder().createQNCache(); +objectCache.set("ACollection", new ArrayList()); // But you can still use it a multipurpose one cache.removeAll(); // You can also clean it, cache.size(); // and ask it how many elements it has @@ -39,10 +41,10 @@ Let's talk about the memory By default, the cache stores a reference to all stored instances, doesn't matter if they're alive or they are too old. If you plan to store huge datasets, you can create it with an auto releaser. Then the cache will remove the old elements every 60 seconds, for example. ```java -QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(1).createQNCache(); //frees the memory every second -QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(60).createQNCache(); //frees the memory every minute -QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(60*60).createQNCache(); //frees the memory every hour -QNCache cache = new QNCacheBuilder().createQNCache(); //never frees the memory +QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(1).createQNCache(); //frees the memory every second +QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(60).createQNCache(); //frees the memory every minute +QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(60*60).createQNCache(); //frees the memory every hour +QNCache cache = new QNCacheBuilder().createQNCache(); //never frees the memory ``` Are the keys case sensitive? @@ -50,9 +52,9 @@ Are the keys case sensitive? By default, yes. But you can also specify it at building time. ```java -QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); //"key" and "KEY" will be different items -QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); //"key" and "KEY" will be the same -QNCache cache = new QNCacheBuilder().createQNCache(); //"key" and "KEY" will be different items +QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); //"key" and "KEY" will be different items +QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); //"key" and "KEY" will be the same +QNCache cache = new QNCacheBuilder().createQNCache(); //"key" and "KEY" will be different items ``` #Download diff --git a/src/main/java/com/fewlaps/quitnowcache/QNCache.java b/src/main/java/com/fewlaps/quitnowcache/QNCache.java index e5afdb2..a11d225 100644 --- a/src/main/java/com/fewlaps/quitnowcache/QNCache.java +++ b/src/main/java/com/fewlaps/quitnowcache/QNCache.java @@ -7,7 +7,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -public class QNCache { +public class QNCache { public static final long KEEPALIVE_FOREVER = 0; @@ -25,7 +25,7 @@ public QNCache(boolean caseSensitiveKeys, Integer autoReleaseInSeconds, Long def this.defaultKeepaliveInMillis = defaultKeepaliveInMillis; } - cache = new ConcurrentHashMap(); + cache = new ConcurrentHashMap>(); startAutoReleaseServiceIfNeeded(); } @@ -63,9 +63,9 @@ protected void setDateProvider(DateProvider dateProvider) { this.dateProvider = dateProvider; } - private ConcurrentHashMap cache; + private ConcurrentHashMap> cache; - public void set(String key, Object value) { + public void set(String key, T value) { if (defaultKeepaliveInMillis != null) { set(key, value, defaultKeepaliveInMillis); } else { @@ -73,25 +73,25 @@ public void set(String key, Object value) { } } - public void set(String key, Object value, long keepAliveInMillis) { + public void set(String key, T value, long keepAliveInMillis) { key = getEffectiveKey(key); if (keepAliveInMillis >= 0) { - cache.put(key, new QNCacheBean(value, now(), keepAliveInMillis)); + cache.put(key, new QNCacheBean(value, now(), keepAliveInMillis)); } } /** * Gets an element from the cache. */ - public T get(String key) { + public T get(String key) { key = getEffectiveKey(key); - QNCacheBean retrievedValue = cache.get(key); + QNCacheBean retrievedValue = cache.get(key); if (retrievedValue == null || !retrievedValue.isAlive(now())) { return null; } else { - return (T) retrievedValue.getValue(); + return retrievedValue.getValue(); } } @@ -99,10 +99,10 @@ public T get(String key) { * Gets an element from the cache. If the element exists but it's dead, * it will be removed of the cache, to free memory */ - Object getAndRemoveIfDead(String key) { + T getAndRemoveIfDead(String key) { key = getEffectiveKey(key); - QNCacheBean retrievedValue = cache.get(key); + QNCacheBean retrievedValue = cache.get(key); if (retrievedValue == null) { return null; } else if (retrievedValue.isAlive(now())) { @@ -130,10 +130,11 @@ public void removeAll() { * Removes the dead elements of the cache, to free memory */ void removeTooOldValues() { - Iterator it = cache.entrySet().iterator(); + Iterator>> it = cache.entrySet().iterator(); + while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - QNCacheBean bean = (QNCacheBean) pair.getValue(); + Map.Entry> entry = it.next(); + QNCacheBean bean = entry.getValue(); if (!bean.isAlive(now())) { it.remove(); } @@ -152,11 +153,9 @@ public int size() { */ int sizeCountingOnlyAliveElements() { int size = 0; - Iterator it = cache.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - QNCacheBean bean = (QNCacheBean) pair.getValue(); - if (bean.isAlive(now())) { + + for(QNCacheBean cacheValue: cache.values()) { + if (cacheValue.isAlive(now())) { size++; } } diff --git a/src/main/java/com/fewlaps/quitnowcache/QNCacheBean.java b/src/main/java/com/fewlaps/quitnowcache/QNCacheBean.java index 9b4d1bb..180236c 100644 --- a/src/main/java/com/fewlaps/quitnowcache/QNCacheBean.java +++ b/src/main/java/com/fewlaps/quitnowcache/QNCacheBean.java @@ -3,13 +3,13 @@ /** * This class is not public 'cause we'll hide the implementation of the QNCache itself */ -class QNCacheBean { +class QNCacheBean { private long creationDate; private long keepAliveInMillis; - private Object value; + private T value; - public QNCacheBean(Object value, long creationDate, long keepAliveInMillis) { + public QNCacheBean(T value, long creationDate, long keepAliveInMillis) { this.creationDate = creationDate; this.keepAliveInMillis = keepAliveInMillis; this.value = value; @@ -23,7 +23,7 @@ public boolean isAlive(long now) { } } - public Object getValue() { + public T getValue() { return value; } } diff --git a/src/main/java/com/fewlaps/quitnowcache/QNCacheBuilder.java b/src/main/java/com/fewlaps/quitnowcache/QNCacheBuilder.java index bf488bc..c2fa2cb 100644 --- a/src/main/java/com/fewlaps/quitnowcache/QNCacheBuilder.java +++ b/src/main/java/com/fewlaps/quitnowcache/QNCacheBuilder.java @@ -20,7 +20,7 @@ public QNCacheBuilder setDefaultKeepaliveInMillis(long defaultKeepaliveInMillis) return this; } - public QNCache createQNCache() { - return new QNCache(caseSensitiveKeys, autoReleaseInSeconds, defaultKeepaliveInMillis); + public QNCache createQNCache() { + return new QNCache(caseSensitiveKeys, autoReleaseInSeconds, defaultKeepaliveInMillis); } } \ No newline at end of file diff --git a/src/test/java/com/fewlaps/quitnowcache/BuilderTest.java b/src/test/java/com/fewlaps/quitnowcache/BuilderTest.java index 2a748af..1d99601 100644 --- a/src/test/java/com/fewlaps/quitnowcache/BuilderTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/BuilderTest.java @@ -8,7 +8,7 @@ public class BuilderTest { @Test public void testDefaultBuilder() { - QNCache cache = new QNCacheBuilder().createQNCache(); + QNCache cache = new QNCacheBuilder().createQNCache(); assertTrue(cache.isCaseSensitiveKeys()); assertNull(cache.getAutoReleaseInSeconds()); @@ -17,56 +17,56 @@ public void testDefaultBuilder() { @Test public void testSettingTrueCaseSensitiveKeysBuilder() { - QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); + QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); assertTrue(cache.isCaseSensitiveKeys()); } @Test public void testSettingFalseCaseSensitiveKeysBuilder() { - QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); + QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); assertFalse(cache.isCaseSensitiveKeys()); } @Test public void testSettingMinusOneAutoReleaseSecondsBuilder() { - QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(-1).createQNCache(); + QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(-1).createQNCache(); assertNull(cache.getAutoReleaseInSeconds()); } @Test public void testSetting10AutoReleaseSecondsBuilder() { - QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(10).createQNCache(); + QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(10).createQNCache(); assertEquals(Integer.valueOf(10), cache.getAutoReleaseInSeconds()); } @Test public void testSettingDefaultKeepaliveBuilder_with10() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(10).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(10).createQNCache(); assertEquals(Long.valueOf(10), cache.getDefaultKeepaliveInMillis()); } @Test public void testSettingDefaultKeepaliveBuilder_withZero() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(0).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(0).createQNCache(); assertNull(cache.getDefaultKeepaliveInMillis()); } @Test public void testSettingDefaultKeepaliveBuilder_withMinusTen() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(-10).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(-10).createQNCache(); assertNull(cache.getDefaultKeepaliveInMillis()); } @Test public void testQNCacheDefaultKeepaliveIsForever() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(QNCache.KEEPALIVE_FOREVER).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(QNCache.KEEPALIVE_FOREVER).createQNCache(); assertNull(cache.getDefaultKeepaliveInMillis()); } diff --git a/src/test/java/com/fewlaps/quitnowcache/CacheBeanTest.java b/src/test/java/com/fewlaps/quitnowcache/CacheBeanTest.java index bfd45b4..725b690 100644 --- a/src/test/java/com/fewlaps/quitnowcache/CacheBeanTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/CacheBeanTest.java @@ -9,31 +9,31 @@ public class CacheBeanTest extends BaseTest { @Test public void aJustCreatedBeanWithMaxKeepaliveIsAliveRightNow() { - QNCacheBean bean = new QNCacheBean(A_VALUE, now(), FOREVER); + QNCacheBean bean = new QNCacheBean(A_VALUE, now(), FOREVER); assertTrue(bean.isAlive(now())); } @Test public void aJustCreatedBeanWithMaxKeepaliveIsAliveAfterThreeDays() { - QNCacheBean bean = new QNCacheBean(A_VALUE, now(), FOREVER); + QNCacheBean bean = new QNCacheBean(A_VALUE, now(), FOREVER); assertTrue(bean.isAlive(threeDaysFromNow())); } @Test public void aJustCreatedBeanWithOneSecondOfKeepaliveIsAliveRightNow() { - QNCacheBean bean = new QNCacheBean(A_VALUE, now(), ONE_SECOND); + QNCacheBean bean = new QNCacheBean(A_VALUE, now(), ONE_SECOND); assertTrue(bean.isAlive(now())); } @Test public void aJustCreatedBeanWithTwoHoursOfKeepaliveIsAliveAfterASecond() { - QNCacheBean bean = new QNCacheBean(A_VALUE, now(), TWO_HOURS); + QNCacheBean bean = new QNCacheBean(A_VALUE, now(), TWO_HOURS); assertTrue(bean.isAlive(oneSecondFromNow())); } @Test public void aJustCreatedBeanWithOneSecondOfKeepaliveIsNotAliveAfterTwoSeconds() { - QNCacheBean bean = new QNCacheBean(A_VALUE, now(), ONE_SECOND); + QNCacheBean bean = new QNCacheBean(A_VALUE, now(), ONE_SECOND); assertFalse(bean.isAlive(twoHoursFromNow())); } } diff --git a/src/test/java/com/fewlaps/quitnowcache/CaseSensitiveKeyTest.java b/src/test/java/com/fewlaps/quitnowcache/CaseSensitiveKeyTest.java index 4401600..b2c982e 100644 --- a/src/test/java/com/fewlaps/quitnowcache/CaseSensitiveKeyTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/CaseSensitiveKeyTest.java @@ -9,7 +9,7 @@ public class CaseSensitiveKeyTest extends BaseTest { @Test public void shouldReturnTheSameIfIgnoringCaseSensitive() { - QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); + QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); cache.set(A_KEY.toLowerCase(), A_VALUE, FOREVER); assertEquals(A_VALUE, cache.get(A_KEY.toUpperCase())); @@ -17,7 +17,7 @@ public void shouldReturnTheSameIfIgnoringCaseSensitive() { @Test public void shouldReturnNullIfUsingCaseSensitive() { - QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); + QNCache cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); cache.set(A_KEY.toLowerCase(), A_VALUE, FOREVER); assertNull(cache.get(A_KEY.toUpperCase())); @@ -25,7 +25,7 @@ public void shouldReturnNullIfUsingCaseSensitive() { @Test public void shouldReturnNullIfUsingDefaultBuilder() { - QNCache cache = new QNCacheBuilder().createQNCache(); + QNCache cache = new QNCacheBuilder().createQNCache(); cache.set(A_KEY.toLowerCase(), A_VALUE, FOREVER); assertNull(cache.get(A_KEY.toUpperCase())); diff --git a/src/test/java/com/fewlaps/quitnowcache/CastTest.java b/src/test/java/com/fewlaps/quitnowcache/CastTest.java index 73de5bb..687c70b 100644 --- a/src/test/java/com/fewlaps/quitnowcache/CastTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/CastTest.java @@ -1,16 +1,15 @@ package com.fewlaps.quitnowcache; import com.fewlaps.quitnowcache.bean.ObjectTestOne; -import com.fewlaps.quitnowcache.bean.ObjectTestTwo; + import org.junit.Before; import org.junit.Test; import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; public class CastTest extends BaseTest { - QNCache cache; + QNCache cache; @Before public void init() { @@ -24,21 +23,6 @@ public void saveObjectAndReturnSameInstance() { assertThat(cache.get(A_KEY), instanceOf(ObjectTestOne.class)); } - @Test - public void shouldThrowExceptionOnCastReturnObject() { - cache.set(A_KEY, new ObjectTestOne()); - - Exception e = null; - - try { - ObjectTestTwo o = (ObjectTestTwo) cache.get(A_KEY); - } catch (ClassCastException cle) { - e = cle; - } - - assertNotNull(e); - } - @Test public void shouldCastObject() { cache.set(A_KEY, new ObjectTestOne()); @@ -48,18 +32,4 @@ public void shouldCastObject() { assertThat(objectTestOne, instanceOf(ObjectTestOne.class)); } - @Test - public void shouldThrowExceptionOnCastWithGenericReturnObject() { - cache.set(A_KEY, new ObjectTestOne()); - - Exception e = null; - - try { - ObjectTestTwo o = cache.get(A_KEY); - } catch (ClassCastException cle) { - e = cle; - } - - assertNotNull(e); - } } diff --git a/src/test/java/com/fewlaps/quitnowcache/DefaultKeepaliveTest.java b/src/test/java/com/fewlaps/quitnowcache/DefaultKeepaliveTest.java index c09622c..da0373e 100644 --- a/src/test/java/com/fewlaps/quitnowcache/DefaultKeepaliveTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/DefaultKeepaliveTest.java @@ -9,7 +9,7 @@ public class DefaultKeepaliveTest extends BaseTest { @Test public void shouldHaveInfiniteKeepaliveByDefault() { - QNCache cache = new QNCacheBuilder().createQNCache(); + QNCache cache = new QNCacheBuilder().createQNCache(); MockDateProvider dateProvider = new MockDateProvider(); cache.setDateProvider(dateProvider); @@ -22,7 +22,7 @@ public void shouldHaveInfiniteKeepaliveByDefault() { @Test public void testOneSecondKeepaliveRightNow() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(ONE_SECOND).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(ONE_SECOND).createQNCache(); MockDateProvider dateProvider = new MockDateProvider(); cache.setDateProvider(dateProvider); @@ -33,7 +33,7 @@ public void testOneSecondKeepaliveRightNow() { @Test public void testOneSecondKeepaliveAfterOneSecond() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(ONE_SECOND).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(ONE_SECOND).createQNCache(); MockDateProvider dateProvider = new MockDateProvider(); cache.setDateProvider(dateProvider); @@ -46,7 +46,7 @@ public void testOneSecondKeepaliveAfterOneSecond() { @Test public void testOneSecondKeepaliveAfterTwoHours() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(ONE_SECOND).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(ONE_SECOND).createQNCache(); MockDateProvider dateProvider = new MockDateProvider(); cache.setDateProvider(dateProvider); @@ -59,7 +59,7 @@ public void testOneSecondKeepaliveAfterTwoHours() { @Test public void testTwoSecondsKeepaliveAfterOneSecond() { - QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(2 * ONE_SECOND).createQNCache(); + QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(2 * ONE_SECOND).createQNCache(); MockDateProvider dateProvider = new MockDateProvider(); cache.setDateProvider(dateProvider); diff --git a/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java b/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java index 6415d19..049d662 100644 --- a/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java @@ -1,17 +1,17 @@ package com.fewlaps.quitnowcache; -import org.junit.Before; import org.junit.Test; import java.util.ArrayList; public class IntroducingQNCacheTest { + @SuppressWarnings("UnusedAssignment") @Test public void theCodeOfTheReadmeWorks() { //##The sample - QNCache cache = new QNCacheBuilder().createQNCache(); + QNCache cache = new QNCacheBuilder().createQNCache(); cache.set("key", "value", 60 * 1000); // It can store things for a minute, cache.set("key", "value", 60 * 60 * 1000); // for an hour, @@ -24,7 +24,7 @@ public void theCodeOfTheReadmeWorks() { cache.get("unExistingKey"); // If something doesn't exists, it returns null cache.get("tooOldKey"); // The same if a key is too old - cache.set("AnInteger", new Integer(42)); // You can save all kind of Objects... + cache.set("AnInteger", 42); // You can save all kind of Objects... cache.set("ACollection", new ArrayList()); // ...whatever you want cache.removeAll(); // You can also clean it, diff --git a/src/test/java/com/fewlaps/quitnowcache/MassiveDataTest.java b/src/test/java/com/fewlaps/quitnowcache/MassiveDataTest.java index d44ede3..90df0e6 100644 --- a/src/test/java/com/fewlaps/quitnowcache/MassiveDataTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/MassiveDataTest.java @@ -6,7 +6,7 @@ import static org.junit.Assert.assertEquals; public class MassiveDataTest extends BaseTest { - QNCache cache; + QNCache cache; @Before public void init() { @@ -17,10 +17,10 @@ public void init() { public void worksWithLotsOfEntries() { int iterations = 10000; for (int i = 0; i < iterations; i++) { - cache.set(Integer.valueOf(i).toString(), i, 0); + cache.set(String.valueOf(i), i, 0); } for (Integer i = 0; i < iterations; i++) { - assertEquals(i, cache.get(Integer.valueOf(i).toString())); + assertEquals(i, cache.get(String.valueOf(i))); } } } diff --git a/src/test/java/com/fewlaps/quitnowcache/MemoryReleaseTest.java b/src/test/java/com/fewlaps/quitnowcache/MemoryReleaseTest.java index 70aed77..ebcd6eb 100644 --- a/src/test/java/com/fewlaps/quitnowcache/MemoryReleaseTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/MemoryReleaseTest.java @@ -13,7 +13,7 @@ public void init() { @Test public void manualReleaseMemoryWorks() throws InterruptedException { - QNCache cache = new QNCacheBuilder().createQNCache(); + QNCache cache = new QNCacheBuilder().createQNCache(); //adding 3 values that will be alive for 1 second, 2 seconds, 3 seconds. cache.set("1", A_VALUE, 1000); @@ -38,7 +38,7 @@ public void manualReleaseMemoryWorks() throws InterruptedException { } public void autoReleaseMemoryWorks() throws InterruptedException { - QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(1).createQNCache(); + QNCache cache = new QNCacheBuilder().setAutoReleaseInSeconds(1).createQNCache(); //adding 3 values that will be alive for 1 second, 2 seconds, 3 seconds. cache.set("1", A_VALUE, 1000); diff --git a/src/test/java/com/fewlaps/quitnowcache/SetAndGetValuesTest.java b/src/test/java/com/fewlaps/quitnowcache/SetAndGetValuesTest.java index cab4577..1186dd9 100644 --- a/src/test/java/com/fewlaps/quitnowcache/SetAndGetValuesTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/SetAndGetValuesTest.java @@ -3,11 +3,14 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class SetAndGetValuesTest extends BaseTest { - QNCache cache; + QNCache cache; MockDateProvider dateProvider; @Before diff --git a/src/test/java/com/fewlaps/quitnowcache/SizeTest.java b/src/test/java/com/fewlaps/quitnowcache/SizeTest.java index 82c9e7d..d8212af 100644 --- a/src/test/java/com/fewlaps/quitnowcache/SizeTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/SizeTest.java @@ -3,11 +3,14 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class SizeTest extends BaseTest { - QNCache cache; + QNCache cache; MockDateProvider dateProvider; @Before diff --git a/src/test/java/com/fewlaps/quitnowcache/ThreadSafeTest.java b/src/test/java/com/fewlaps/quitnowcache/ThreadSafeTest.java index d2cd048..4633a34 100644 --- a/src/test/java/com/fewlaps/quitnowcache/ThreadSafeTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/ThreadSafeTest.java @@ -1,6 +1,7 @@ package com.fewlaps.quitnowcache; import com.fewlaps.quitnowcache.util.RandomGenerator; + import org.junit.Before; import org.junit.Test; @@ -13,7 +14,7 @@ public class ThreadSafeTest extends BaseTest { static final int ITERATIONS = 10000; - QNCache cache; + QNCache cache; @Before public void init() { From 9c0c6a08609275a49c9f4a44b036dc8cf0dac77a Mon Sep 17 00:00:00 2001 From: Roc Boronat Date: Thu, 31 Dec 2015 19:06:35 +0100 Subject: [PATCH 2/3] Improving the description of the new feature in the README.md and testing it --- README.md | 10 ++++------ .../quitnowcache/IntroducingQNCacheTest.java | 14 ++++++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 445c048..495164b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The sample ---------- ```java -QNCache cache = new QNCacheBuilder().createQNCache(); +QNCache cache = new QNCacheBuilder().createQNCache(); cache.set("key", "value", 60 * 1000); // It can store things for a minute, cache.set("key", "value", 60 * 60 * 1000); // for an hour, @@ -27,13 +27,11 @@ cache.remove("key"); // and remove it if you want. cache.get("unExistingKey"); // If something doesn't exists, it returns null cache.get("tooOldKey"); // The same if a key is too old -cache.set("AnInteger", new Integer(42)); // It is typesafe. This is a compile error - -QNCache objectCache = new QNCacheBuilder().createQNCache(); -objectCache.set("ACollection", new ArrayList()); // But you can still use it a multipurpose one - cache.removeAll(); // You can also clean it, cache.size(); // and ask it how many elements it has + +QNCache stringCache = new QNCacheBuilder().createQNCache(); //You can also make it typesafe +stringCache.set("key", 42); //so this line does not compile :) ``` Let's talk about the memory diff --git a/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java b/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java index 049d662..74f7415 100644 --- a/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java +++ b/src/test/java/com/fewlaps/quitnowcache/IntroducingQNCacheTest.java @@ -3,6 +3,7 @@ import org.junit.Test; import java.util.ArrayList; +import java.util.List; public class IntroducingQNCacheTest { @@ -11,7 +12,7 @@ public class IntroducingQNCacheTest { public void theCodeOfTheReadmeWorks() { //##The sample - QNCache cache = new QNCacheBuilder().createQNCache(); + QNCache cache = new QNCacheBuilder().createQNCache(); cache.set("key", "value", 60 * 1000); // It can store things for a minute, cache.set("key", "value", 60 * 60 * 1000); // for an hour, @@ -24,12 +25,12 @@ public void theCodeOfTheReadmeWorks() { cache.get("unExistingKey"); // If something doesn't exists, it returns null cache.get("tooOldKey"); // The same if a key is too old - cache.set("AnInteger", 42); // You can save all kind of Objects... - cache.set("ACollection", new ArrayList()); // ...whatever you want - cache.removeAll(); // You can also clean it, cache.size(); // and ask it how many elements it has + QNCache stringCache = new QNCacheBuilder().createQNCache(); //You can also make it typesafe + //stringCache.set("key", 42); //so this will not compile :) + //##Let's talk about the memory cache = new QNCacheBuilder().setAutoReleaseInSeconds(1).createQNCache(); //frees the memory every second cache = new QNCacheBuilder().setAutoReleaseInSeconds(60).createQNCache(); //frees the memory every minute @@ -40,5 +41,10 @@ public void theCodeOfTheReadmeWorks() { cache = new QNCacheBuilder().setCaseSensitiveKeys(true).createQNCache(); //"key" and "KEY" will be different items cache = new QNCacheBuilder().setCaseSensitiveKeys(false).createQNCache(); //"key" and "KEY" will be the same cache = new QNCacheBuilder().createQNCache(); //"key" and "KEY" will be different items + + //##It's possible to change the default keepalive? + cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(1000).createQNCache(); //a keepalive of one second + cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(1000 * 60).createQNCache(); //a keepalive of one minute + cache = new QNCacheBuilder().createQNCache(); //the default keepalive: remember it forever! } } From 9dad80514415e3bf72a6ad088ea96ea5020fbc81 Mon Sep 17 00:00:00 2001 From: Roc Boronat Date: Thu, 31 Dec 2015 19:14:10 +0100 Subject: [PATCH 3/3] Version++ --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 55710bd..7e705c8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.fewlaps.quitnowcache' -version '1.4' +version '1.5.0' apply plugin: 'java' apply plugin: 'com.novoda.bintray-release' @@ -34,7 +34,7 @@ publish { userOrg = 'fewlaps' groupId = 'com.fewlaps.quitnowcache' artifactId = 'quitnow-cache' - publishVersion = '1.4' + publishVersion = '1.5.0' desc = 'A memcached-like Java cache, focused on portability' website = 'https://github.com/Fewlaps/quitnow-cache' }