josuFramework - Caching

The caching system in josuFramework provides a simple and lightweight way to track the validity of expensive calculations or state-dependent values. The core component is the Cached class (found in osu.framework.caching).

Note

This section refers to general value caching. For dependency injection caching, see josuFramework - Allocation.

The Cached Class

The Cached class is essentially a state tracker that holds a boolean isValid flag. It is useful for scenarios where you only want to perform an operation (like layout calculations or resource loading) when something has changed.

Basic Usage

Typically, you store a Cached instance alongside the value you want to cache.

private final Cached layoutCache = new Cached();
private float calculatedWidth;

public float getWidth() {
    if (!layoutCache.isValid()) {
        calculatedWidth = performExpensiveCalculation();
        layoutCache.validate();
    }
    return calculatedWidth;
}

public void invalidateLayout() {
    layoutCache.invalidate();
}

The ensureValid Helper

To simplify the pattern above, you can use the ensureValid(Runnable) method.

public float getWidth() {
    layoutCache.ensureValid(() -> {
        calculatedWidth = performExpensiveCalculation();
    });
    return calculatedWidth;
}

Methods

  • isValid(): Returns whether the cache is currently valid.

  • invalidate(): Marks the cache as invalid (needs recalculation).

  • validate(): Marks the cache as valid (up-to-date).

  • ensureValid(Runnable recalculate): Checks if the cache is valid; if not, runs the provided action and then validates the cache. Returns true if recalculation was performed.

Common Patterns

Invalidation on Change

A common pattern is to invalidate a cache whenever a dependent property (like a Bindable) changes.

private final Cached sizeCache = new Cached();

public MyComponent() {
    someBindable.bindValueChanged(e -> sizeCache.invalidate());
}