Giter Site home page Giter Site logo

cactoos-cache's Introduction

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)

cactoos-cache's People

Contributors

g4s8 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

andreoss

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.