Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
65 lines (55 sloc) 3.5 KB

cactoos-cache

Caching primitives for Cactoos library.

EO principles respected here DevOps By Rultor.com

Bintray Build Status Build status PDD status License Test Coverage

Install

Add maven dependency:

<dependency>
  <groupId>com.g4s8</groupId>
  <artifactId>cactoos-cache</artifactId>
</dependency>

latest version on bintray is Bintray.

Caches

There are few different cache types and Cactoos primitives with this caches.
It is SoftReference based caches, WeakReference key caching, LRU caches and expired caches.

SoftReference based caches

From Java documentation:

Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand. Soft references are most often used to implement memory-sensitive caches.

This kind of caches wraps results in SoftReference which can be cleared on demand if JVM will need more memory. There are SoftBiFunc, SoftFunc, SoftScalar and SoftText.For example to cache lazy initialization you can use SoftScalar:

final Scalar<Value> scalar = new SoftScalar(() -> value());
assert scalar.value() == scalar.value(); // same references here

To build a cache use SoftFunc or SoftBiFunc or SoftFunc:

final Func<Argument, Value> func = new SoftFunc(arg -> value(arg));
assert func.apply(arg) == func.apply(arg); // same references for one argument

WeakReference caches

Weak reference are used when you want to keep value until you have a string reference for key somewere. There are only WeakFunc implementation, it will keep func result in memory func argument is present:

Argument arg = argument();
final Func<Argument, Value> func = new WeakFunc(arg -> value(arg));
final Value value = func.apply(arg);
assert value == func.apply(arg); // same references for one argument
arg = null;
System.gc(); // now func.apply may return new value if arg was garbage-collected. 

LRU caches

LRU (Least Recently Used) caches keeps only values which are used more than others and clear least used values, there are LruFunc and LruBiFunc implementations.

Expired caches

Expired caches are similar to LRU caches, but they use last access time instead of access count. (not implemented yet)

You can’t perform that action at this time.