Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
/ cactoos-cache Public archive

Caching primitives for Cactoos library

License

Notifications You must be signed in to change notification settings

g4s8/cactoos-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WARNING

It was an experiment repository, it contains POC library code. It's not producation ready. It'll be not supported.


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)

About

Caching primitives for Cactoos library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published