import ballerina/cache;
import ballerina/io;

public function main() returns error? {

    // This creates a new cache with the advanced configuration.
    cache:Cache cache = new ({
        // The maximum size of the cache is 10.
        capacity: 10,
        // The eviction factor is set to 0.2, which means at the
        // time of eviction 10*0.2=2 entries get removed from the cache.
        evictionFactor: 0.2,
        // The default max age of the cache entry is set to 2 seconds.
        defaultMaxAge: 2,
        // The cache cleanup task runs every 3 seconds and clears all
        // the expired entries.
        cleanupInterval: 3
    });

    // Adds the new entries to the cache.
    check cache.put("key1", "value1");
    check cache.put("key2", "value2");
    // Adds a new entry to the cache by overriding the default max age.
    check cache.put("key3", "value3", 3600);

    // Gets the keys of the cache entries.
    string[] keys = cache.keys();
    io:println("The existing keys in the cache: ", keys);

    // Discards the given cache entry.
    _ = check cache.invalidate("key2");

    // Gets the keys of the cache entries.
    io:println("The existing keys in after invalidating a given key: ",
                cache.keys());

    // Discards all the cache entries of the cache.
    _ = check cache.invalidateAll();

    // Gets the keys of the cache entries after all the keys are invalidated.
    io:println("The keys after invalidating all the keys: ", cache.keys());
}

Cache Invalidation

The Ballerina Cache API provides an in-memory cache implementation by default with the Least Recently Used algorithm-based eviction policy. For more information on the underlying module, see the Cache module.

import ballerina/cache;
import ballerina/io;
public function main() returns error? {
    cache:Cache cache = new ({

This creates a new cache with the advanced configuration.

        capacity: 10,

The maximum size of the cache is 10.

        evictionFactor: 0.2,

The eviction factor is set to 0.2, which means at the time of eviction 10*0.2=2 entries get removed from the cache.

        defaultMaxAge: 2,

The default max age of the cache entry is set to 2 seconds.

        cleanupInterval: 3
    });

The cache cleanup task runs every 3 seconds and clears all the expired entries.

    check cache.put("key1", "value1");
    check cache.put("key2", "value2");

Adds the new entries to the cache.

    check cache.put("key3", "value3", 3600);

Adds a new entry to the cache by overriding the default max age.

    string[] keys = cache.keys();
    io:println("The existing keys in the cache: ", keys);

Gets the keys of the cache entries.

    _ = check cache.invalidate("key2");

Discards the given cache entry.

    io:println("The existing keys in after invalidating a given key: ",
                cache.keys());

Gets the keys of the cache entries.

    _ = check cache.invalidateAll();

Discards all the cache entries of the cache.

    io:println("The keys after invalidating all the keys: ", cache.keys());
}

Gets the keys of the cache entries after all the keys are invalidated.

bal run cache_invalidation.bal
The existing keys in the cache: ["key1","key2","key3"]
The existing keys in after invalidating a given key: ["key1","key3"]
The keys in after invalidating all the keys: []