green_copper_runtime::modules::util

Module cache

source
Expand description

§Cache module

this module can be used as a machine local cache (caches are shared between runtimes (threads))

§Example

§cacheMod.getRegion(id: string, options?: object): greco.util.cache.Region

Gets or initializes a region

The options object may contain the following params (please note that if the region for the id already exists these are ignored)

  • items: number // default = 100.000 maximum number of items to cache (when more items become present the least recently used will be removed even if withing its ttl)
  • idle: number // default = 3.600.000 (one hour) max idle (unused) time for an object in milliseconds
  • ttl: number // default = 86.400.000 (one day) max age for an object (the entry will be invalidated after this time even if recently used)

§Example

import * as grecoCache from 'greco://cache';
const options = {
    items: 100000
};
const cacheRegion = grecoCache.getRegion('my_cache_region_id', options);

§cacheRegion.get(key: string, init: Function<string | Promise>): string | Promise

gets or returns an item based on a key it may return the result (as string) directly or it may return a Promise if an item does not exist in the cache the init function is invoked

import * as grecoCache from 'greco://cache';
const cacheRegion = grecoCache.getRegion('my_cache_region_id');
export async function load(key) {
     return cacheRegion.get(key, async () => {
         return "largeLoadedThing_" + key;
     });
}

§cacheRegion.remove(key: string): void

removes an item from the cache