It was an experiment repository, it contains POC library code. It's not producation ready. It'll be not supported.
Caching primitives for Cactoos library.
Add maven dependency:
<dependency>
<groupId>com.g4s8</groupId>
<artifactId>cactoos-cache</artifactId>
</dependency>
latest version on bintray is .
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.
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
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 (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 are similar to LRU caches, but they use last access time instead of access count. (not implemented yet)