Fox Cache

Fox Cache is a reactive persistent cache for applications based on Hookless reactive library and especially PushMode web apps. The cache tracks dependencies recursively and automatically refreshes stale cache entries. Cache API is declarative, functional, and reactive.

Download

Get Fox Cache from Maven Central:

Maven tool
<dependency>
    <groupId>com.machinezoo.foxcache</groupId>
    <artifactId>foxcache</artifactId>
    <version>0.1.6</version>
</dependency>

Or clone sources from GitHub or Bitbucket. Don't forget to configure your build for Java 17+. Sources and binaries are distributed under Apache License 2.0.

If your project is a Java module, add the following declaration to your module-info.java:

requires com.machinezoo.foxcache;

Usage

Every cache entry has a key, usually a record. Cache key can have parameters (record components). Cache key defines compute() method that describes how to refresh the value associated with the key. There is also a fast link() method that pings dependencies to enable efficient dependency discovery without having to call the potentially expensive compute() method.

Here's a trivial example that implements cached computation of Fibonacci numbers.

record Fibonacci(int rank) implements KryoCache<Integer> {
    @Override public void link() {
        if (rank >= 2) {
            new Fibonacci(rank - 1).touch();
            new Fibonacci(rank - 2).touch();
        }
    }
    @Override public Integer compute() {
        if (rank < 2)
            return rank;
        return new Fibonacci(rank - 1).get() + new Fibonacci(rank - 2).get();
    }
}
// StaticContent is just some reactive widget from Meerkat Widgets.
StaticContent.show("Output", IntStream.rangeClosed(0, 10)
    .mapToObj(n -> new Fibonacci(n).get())
    .toList());

When executed, it produces the following output.

Output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Next steps