Skip to content

Commit

Permalink
Merge branch 'release/v1.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rocboronat committed Jun 7, 2016
2 parents 266800a + 39c23dd commit d1c0b52
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 7 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# [QuitNow!](http://quitnowapp.com)'s cache
A memcached-like Java cache, focused on portability, great for Android.

Before this library, QuitNow! and lots of apps we've seen are caching things until the system kills the app. That's not cool. In the other hand, developing a cache for that little improvement is not trivial. So, we decided to return the work to the open source community by writing this really simple cache, allowing developers to keep information only for a limited time.
Before this library, caching data for a limited time was a hard task to do. The developer had to save the last time the data was stored, and then, check it everytime the data was read. So, we decided to return the work to the open source community by writing this really simple cache, allowing developers to keep information for a limted time.

And we've done it using TDD, so it's totally tested. [Check the tests!](https://github.com/Fewlaps/quitnow-cache/tree/master/src/test/java/com/fewlaps/quitnowcache) :·)
We've done it using TDD, so it's totally tested. [Check the tests!](https://github.com/Fewlaps/quitnow-cache/tree/master/src/test/java/com/fewlaps/quitnowcache) :·)

The sample
----------
Expand Down Expand Up @@ -65,15 +65,25 @@ QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(1000 * 60).crea
QNCache cache = new QNCacheBuilder().createQNCache(); //the default keepalive: remember it forever!
```

Why working with millis and seconds?
---------------------------
Good question! If you prefer to work with TimeUnit, you're free to do it.

```java
QNCache cache = new QNCacheBuilder().setAutoRelease(2, TimeUnit.HOURS).createQNCache();
QNCache cache = new QNCacheBuilder().setDefaultKeepalive(5, TimeUnit.MINUTES).createQNCache();
cache.set("key", "value", 42, TimeUnit.SECONDS);
```

#Download

* Get <a href="https://github.com/Fewlaps/quitnow-cache/releases/download/v1.5.0/quitnow-cache-1.5.0.jar">the latest .jar</a>
* Get <a href="https://github.com/Fewlaps/quitnow-cache/releases/download/v1.6.0/quitnow-cache-1.6.0.jar">the latest .jar</a>

* Grab via Gradle:
```groovy
repositories { jcenter() }
compile 'com.fewlaps.quitnowcache:quitnow-cache:1.5.0'
compile 'com.fewlaps.quitnowcache:quitnow-cache:1.6.0'
```
* Grab via Maven:
```xml
Expand All @@ -85,7 +95,7 @@ compile 'com.fewlaps.quitnowcache:quitnow-cache:1.5.0'
<dependency>
<groupId>com.fewlaps.quitnowcache</groupId>
<artifactId>quitnow-cache</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.fewlaps.quitnowcache'
version '1.5.0'
version '1.6.0'

apply plugin: 'java'
apply plugin: 'com.novoda.bintray-release'
Expand Down Expand Up @@ -34,7 +34,7 @@ publish {
userOrg = 'fewlaps'
groupId = 'com.fewlaps.quitnowcache'
artifactId = 'quitnow-cache'
publishVersion = '1.5.0'
publishVersion = '1.6.0'
desc = 'A memcached-like Java cache, focused on portability'
website = 'https://github.com/Fewlaps/quitnow-cache'
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/fewlaps/quitnowcache/QNCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void set(String key, T value, long keepAliveInMillis) {
}
}

public void set(String key, T value, long keepAliveUnits, TimeUnit timeUnit) {
set(key, value, timeUnit.toMillis(keepAliveUnits));
}

/**
* Gets an element from the cache.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/fewlaps/quitnowcache/QNCacheBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fewlaps.quitnowcache;

import java.util.concurrent.TimeUnit;

public class QNCacheBuilder {
private boolean caseSensitiveKeys = true;
private Integer autoReleaseInSeconds;
Expand All @@ -10,11 +12,21 @@ public QNCacheBuilder setCaseSensitiveKeys(boolean caseSensitiveKeys) {
return this;
}

public QNCacheBuilder setAutoRelease(int units, TimeUnit timeUnit) {
this.autoReleaseInSeconds = Math.toIntExact(timeUnit.toSeconds(units));
return this;
}

public QNCacheBuilder setAutoReleaseInSeconds(Integer autoReleaseInSeconds) {
this.autoReleaseInSeconds = autoReleaseInSeconds;
return this;
}

public QNCacheBuilder setDefaultKeepalive(int units, TimeUnit timeUnit) {
this.defaultKeepaliveInMillis = timeUnit.toMillis(units);
return this;
}

public QNCacheBuilder setDefaultKeepaliveInMillis(long defaultKeepaliveInMillis) {
this.defaultKeepaliveInMillis = defaultKeepaliveInMillis;
return this;
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/fewlaps/quitnowcache/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

public class BuilderTest {
Expand Down Expand Up @@ -43,27 +45,76 @@ public void testSetting10AutoReleaseSecondsBuilder() {
assertEquals(Integer.valueOf(10), cache.getAutoReleaseInSeconds());
}

@Test
public void testSetting10AutoReleaseSecondsBuilder_usingSecondsTimeUnit() {
QNCache<Object> cache = new QNCacheBuilder().setAutoRelease(10, TimeUnit.SECONDS).createQNCache();

assertEquals(Integer.valueOf(10), cache.getAutoReleaseInSeconds());
}

@Test
public void testSetting10AutoReleaseSecondsBuilder_usingMillisTimeUnit() {
QNCache<Object> cache = new QNCacheBuilder().setAutoRelease(10000, TimeUnit.MILLISECONDS).createQNCache();

assertEquals(Integer.valueOf(10), cache.getAutoReleaseInSeconds());
}

@Test
public void testSetting10AutoReleaseSecondsBuilder_usingMicrosTimeUnit() {
QNCache<Object> cache = new QNCacheBuilder().setAutoRelease(10000000, TimeUnit.MICROSECONDS).createQNCache();

assertEquals(Integer.valueOf(10), cache.getAutoReleaseInSeconds());
}

@Test
public void testSettingDefaultKeepaliveBuilder_with10() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(10).createQNCache();

assertEquals(Long.valueOf(10), cache.getDefaultKeepaliveInMillis());
}

@Test
public void testSettingDefaultKeepaliveBuilder_with10000MillisTimeUnits() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepalive(10000, TimeUnit.MILLISECONDS).createQNCache();

assertEquals(Long.valueOf(10000), cache.getDefaultKeepaliveInMillis());
}

@Test
public void testSettingDefaultKeepaliveBuilder_with10SecondsTimeUnits() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepalive(10, TimeUnit.SECONDS).createQNCache();

assertEquals(Long.valueOf(10000), cache.getDefaultKeepaliveInMillis());
}

@Test
public void testSettingDefaultKeepaliveBuilder_withZero() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(0).createQNCache();

assertNull(cache.getDefaultKeepaliveInMillis());
}

@Test
public void testSettingDefaultKeepaliveBuilder_withZeroSecondsTimeUnit() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepalive(0, TimeUnit.SECONDS).createQNCache();

assertNull(cache.getDefaultKeepaliveInMillis());
}

@Test
public void testSettingDefaultKeepaliveBuilder_withMinusTen() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(-10).createQNCache();

assertNull(cache.getDefaultKeepaliveInMillis());
}

@Test
public void testSettingDefaultKeepaliveBuilder_withMinusTenSecondsTimeUnit() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepalive(-10, TimeUnit.SECONDS).createQNCache();

assertNull(cache.getDefaultKeepaliveInMillis());
}

@Test
public void testQNCacheDefaultKeepaliveIsForever() {
QNCache<Object> cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(QNCache.KEEPALIVE_FOREVER).createQNCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void manualReleaseMemoryWorks() throws InterruptedException {
assertEquals(0, cache.sizeCountingDeadAndAliveElements());
}

@Test
public void autoReleaseMemoryWorks() throws InterruptedException {
QNCache<String> cache = new QNCacheBuilder().setAutoReleaseInSeconds(1).createQNCache();

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/fewlaps/quitnowcache/SetAndGetValuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -164,4 +166,18 @@ public void containsReturnsTrueIfSomethingExists() {
public void containsReturnsFalseSomethingDoesntExist() {
assertFalse(cache.contains(A_KEY));
}

@Test
public void savingSomethingForOneSecondsShouldReturnTheSameImmediatelly_usingSecondsTimeUnit() {
cache.set(A_KEY, A_VALUE, 1, TimeUnit.SECONDS);

assertEquals(A_VALUE, cache.get(A_KEY));
}

@Test
public void savingSomethingForOneSecondsShouldReturnTheSameImmediatelly_usingMillisTimeUnit() {
cache.set(A_KEY, A_VALUE, 1000, TimeUnit.MILLISECONDS);

assertEquals(A_VALUE, cache.get(A_KEY));
}
}

0 comments on commit d1c0b52

Please sign in to comment.