• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    Java
  • License
    MIT License
  • Created about 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

βŒ› A collection to store data for a given time

Coverage Status Gitter

QuitNow's cache

A temporary cache for JVM applications.

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 QNCache.Builder().build();

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 exist, it returns null
cache.get("tooOldKey"); // The same if a key is too old

cache.clear(); // You can also clean it,
cache.size(); // and ask it how many elements it has

QNCache<String> stringCache = new QNCache.Builder().build(); // You can also make it typesafe
stringCache.set("key", 42); // so this won't 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.

new QNCache.Builder().autoReleaseInSeconds(1).build(); // frees the memory every second
new QNCache.Builder().autoReleaseInSeconds(60).build(); // frees the memory every minute
new QNCache.Builder().autoReleaseInSeconds(60*60).build(); // frees the memory every hour
new QNCache.Builder().build(); // never frees the memory

By the way, if you use the auto releaser, you should know that you can stop it. You should do it when your Servlet container notifies you that it wants to finish the application. It's the same you should do with any database connection, for example. In Android environments you can avoid it, but you might be interested in this extended explanation.

cache.shutdown();

Are the keys case sensitive?

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

new QNCache.Builder().caseSensitiveKeys(true).build(); // "key" and "KEY" will be different items
new QNCache.Builder().caseSensitiveKeys(false).build(); // "key" and "KEY" will be the same
new QNCache.Builder().build(); // "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.

new QNCache.Builder().defaultKeepaliveInMillis(1000).build(); // a keepalive of one second
new QNCache.Builder().defaultKeepaliveInMillis(1000 * 60).build(); // a keepalive of one minute
new QNCache.Builder().build(); // 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.

new QNCache.Builder().autoRelease(2, TimeUnit.HOURS).build();
new QNCache.Builder().defaultKeepalive(5, TimeUnit.MINUTES).build();
cache.set("key", "value", 42, TimeUnit.SECONDS);

Download

  • Grab via Gradle:
repositories { jcenter() }
    
compile 'com.fewlaps.quitnowcache:quitnow-cache:3.4.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.4.0</version>
</dependency>

LICENSE

The MIT License (MIT)

Copyright (c) 2018 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.

More Repositories

1

flone-android

🚁 A remote controller for drones with CleanFlight boards
Java
55
star
2

quitnow-email-suggester

πŸ“§ Correct typos in email addresses like [email protected]
Java
46
star
3

quitnow-sleep-time

The user is sleeping: don't launch that notification!
Java
34
star
4

bixolon-printer-example

Print things with Bixolon printers on Android
Java
25
star
5

superpi

A Docker image to becnhmark your CPU
Java
22
star
6

slim-jpg

🀏 Optimize your JPGs on the fly
Java
18
star
7

mention-detector

Detect @mentions and @sequences @of @mentions in your Strings
Java
11
star
8

Picoleto

A simple library to verify Spanish identity documents like NIF and NIE
Java
11
star
9

PrettyStrings

A small library to give some prettiness to your project strings!
Java
6
star
10

AB-Testing

A standalone A/B testing library for Android
Kotlin
6
star
11

quitnow-website

The lovely QuitNow! public website
JavaScript
5
star
12

quitnow-arduino-usercount

Get the QuitNow! users via internet and print them into an LCD screen
Arduino
5
star
13

PhraseKit

PhraseKit - string placeholders formatting for ObjetiveC
Shell
4
star
14

social-stalker

Like your Twitter interactions with this lovely bot
Java
4
star
15

http-monitor

A simple way to monitor an URL
Java
4
star
16

CreditsKit

A simple way to list third-party licenses in your iOS apps
Objective-C
4
star
17

similar-words

ROC and R0C are not the same... but they are similar!
Java
2
star
18

finappsparty-2014

The project created at the Finappsparty Hackathon at Barcelona
Java
2
star
19

slim-jpg-demo-web

A website to see how SlimJPG behaves with your own pictures
Kotlin
1
star