Cache - MTDdk/jawn GitHub Wiki

A standard caching mechanism is part of the core and can be used by injection.

The standard cache has a default eviction limit of 10 minutes, which can be individually overridden on a per key -basis.

Using the cache

public class CacheController extends Controller {
    @Inject
    Cache cache;
    
    @Inject
    MoviesDB movies;

    public void index() {
        Integer id = getId().asInt();
        String key = "movies"+id;
        
        // caching for the default of 10 minutes
        Movie movie = cache.computeIfAbsent(key, () -> movies.fetch(id));
        respond().json(movie); // respond in json
    }
}

Methods of the cache

// Only adds the value, if the key is not already a part of the cache
void add(String key, T value);
void add(String key, T value, int seconds); // Set the eviction manually

T cache.get(String key);

// Overrides any previously set key
void set(String key, T value);
void set(String key, T value, int seconds);

boolean isSet(String key);
void delete(String key);
void clear();

Additional methods

// Tries to compute the value, if the key is not already associated with a value
// (or it is null)
T computeIfAbsent(String key, Function<String, T> mappingFunction);
T computeIfAbsent(String key, int seconds, Function<String, T> mappingFunction);

T computeIfAbsent(String key, Supplier<T> supplier);
T computeIfAbsent(String key, int seconds, Supplier<T> supplier);

Overriding the standard cache implementation

In jawn.properties:

cache.implementation=<fully qualified class name>
cache.default_expiration=<time> // 3h or 4m or 10s
⚠️ **GitHub.com Fallback** ⚠️