B4J Library Caffeine Cache

Caches can be critical for servers and other apps where you cannot store all data in memory and still need good performance.
A simple and cross platform cache implementation is available in the B4XCollections library: https://www.b4x.com/android/forum/t...e-and-useful-cache-collection.136292/#content

This library, based on caffeine open source project: https://github.com/ben-manes/caffeine, is much more powerful. It is a B4J only library and requires OpenJDK 11+.

The open source project is well documented and quite interesting: https://github.com/ben-manes/caffeine/wiki
I will explain how to use it.

1. Define the cache settings and initialize the cache.
2. Implement the Load event. This will be called whenever a value is requested for a key that isn't in the cache.
3. Use the cache.

Here are the cache settings, note that they are all optional:
MaximumSize - maximum number of cached items
ExpireAfterWriteMs - If >0 then items will be removed after X milliseconds passed since the insertion point. This is useful for cases where the data becomes stale after some time.
ExpireAfterAccessMs - If > 0 then items will be removed after X milliseconds passed since the last access (read or write).
RecordStats - If True then the cache will track hit/miss statistics.

There is a nice separation between the consumers and the supplier implementations. The consumers request a value and if needed the Load event is raised.

B4X:
Private Sub Cache_Load (Key As Object) As Object
The value returned from this event will be stored in the cache and returned to the consumer.
Note that it will be raised on the same thread that called the Get method.
Return Null if no value is available. The cache doesn't store Null values.

Other methods:
GetIfPresent - Return the value if it is in the cache or Null. Doesn't raise the Load event.
Invalidate / InvalidateAll - Removes items from the cache.
Put - Adds an item to the cache.
Stats - Returns a CaffeineStats object with cache statistics. Will be empty if Settings.RecordStats is false.
EstimatedSize - estimated number of cached items.

A simple UI example is attached.

Library + dependencies (600kb) - https://www.b4x.com/b4j/files/caffeine.zip
 

Attachments

  • CaffeineExample.zip
    3.6 KB · Views: 201
Last edited:

udg

Expert
Licensed User
Longtime User
Correct me if I'm wrong; this makes for a superb companion to a jserver-based or RDC2-based middle-layer solution.
I mean, on a first request for a value we execute a regular SELECT statement and store the returned value in the cache, then on subsequent requests that same value is read from the cache saving the DB operation. Having potentiatly thousands of clients accessing the middle-layer for some common values that cache is really valuable.

Thank you for this great addition to our toolbox.
 
Top