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:
<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.
Next steps
- Download Fox Cache from Maven Central if you haven't done so yet.
- There is no javadoc documentation yet. See source code for now.
- Report an issue or submit PR on GitHub or Bitbucket if something is wrong or poorly documented.