QuitNow!'s cache

General

Category
Free
Tag
Caching
License
MIT License
Registered
Sep 23, 2015
Favorites
0
Link
https://github.com/Fewlaps/quitnow-cache
See also
CacheUtilsLibrary
Android-BitmapCache
ReactiveCache
Storo
CacheManage

Additional

Language
Java
Version
v3.0.0 (Dec 27, 2016)
Created
Sep 17, 2015
Updated
Jun 1, 2017
Owner
Fewlaps
Contributors
Bernat Borrás Paronella (alorma)
Rafa Vázquez (Sloy)
Sam Wolfand (swolfand)
rocboronat
juanet3
skyweb07
m-ezzat
padilo
nCodefresh
9
Activity
Badge
Generate
Download
Source code

Advertising

QuitNow!'s cache

A memcached-like Java cache, focused on portability, great for Android.

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 limited time.

We've done it using TDD, so it's totally tested. Check the tests! :·)

The sample

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,
cache.set("key", "value", 0); // or forever.
cache.set("key", "value"); // And also for the short version of forever.

cache.get("key"); // It can get them again,
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.clear(); // You can also clear it,
cache.size(); // and ask it how many elements it has

QNCache<String> 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

By default, the cache stores a reference to all stored instances, doesn't matter if they're fresh or not. If you plan to store huge instances, like an Android's Bitmap, you can create it with an auto releaser. Then the cache will remove the old elements after the given amount of time.

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(); //will never free the invalidated items from memory

Are the keys case sensitive?

By default, yes. But you can also specify it at building time.

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

It's possible to change the default keepalive?

Of course! But, again, you have to specify it at building time.

QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(1000).createQNCache(); //a keepalive of one second
QNCache cache = new QNCacheBuilder().setDefaultKeepaliveInMillis(1000 * 60).createQNCache(); //a keepalive of one minute
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.

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

repositories { jcenter() }
    
compile 'com.fewlaps.quitnowcache:quitnow-cache:3.0.0'
  • Grab via Maven:
<repository>
  <id>jcenter</id>
  <url>http://jcenter.bintray.com</url>
</repository>

<dependency>
    <groupId>com.fewlaps.quitnowcache</groupId>
    <artifactId>quitnow-cache</artifactId>
    <version>3.0.0</version>
</dependency>

LICENSE

The MIT License (MIT)

Copyright (c) 2015 Fewlaps

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.