transitory
Version:
In-memory cache with high hit rates via LFU eviction. Supports time-based expiration, automatic loading and metrics.
141 lines (140 loc) • 4.32 kB
TypeScript
import { Cache } from '../cache/Cache';
import { MaxAgeDecider } from '../cache/expiration/MaxAgeDecider';
import { KeyType } from '../cache/KeyType';
import { Loader } from '../cache/loading/Loader';
import { LoadingCache } from '../cache/loading/LoadingCache';
import { RemovalListener } from '../cache/RemovalListener';
import { Weigher } from '../cache/Weigher';
export interface CacheBuilder<K extends KeyType, V> {
/**
* Set a listener that will be called every time something is removed
* from the cache.
*/
withRemovalListener(listener: RemovalListener<K, V>): this;
/**
* Set the maximum number of items to keep in the cache before evicting
* something.
*/
maxSize(size: number): this;
/**
* Set a function to use to determine the size of a cached object.
*/
withWeigher(weigher: Weigher<K, V>): this;
/**
* Change to a cache where get can also resolve values if provided with
* a function as the second argument.
*/
loading(): LoadingCacheBuilder<K, V>;
/**
* Change to a loading cache, where the get-method will return instances
* of Promise and automatically load unknown values.
*/
withLoader(loader: Loader<K, V>): LoadingCacheBuilder<K, V>;
/**
* Set that the cache should expire items some time after they have been
* written to the cache.
*/
expireAfterWrite(time: number | MaxAgeDecider<K, V>): this;
/**
* Set that the cache should expire items some time after they have been
* read from the cache.
*/
expireAfterRead(time: number | MaxAgeDecider<K, V>): this;
/**
* Activate tracking of metrics for this cache.
*/
metrics(): this;
/**
* Build the cache.
*/
build(): Cache<K, V>;
}
export interface LoadingCacheBuilder<K extends KeyType, V> extends CacheBuilder<K, V> {
/**
* Build the cache.
*/
build(): LoadingCache<K, V>;
}
/**
* Builder for cache instances.
*/
export declare class CacheBuilderImpl<K extends KeyType, V> implements CacheBuilder<K, V> {
private optRemovalListener?;
private optMaxSize?;
private optWeigher?;
private optMaxWriteAge?;
private optMaxNoReadAge?;
private optMetrics;
/**
* Set a listener that will be called every time something is removed
* from the cache.
*
* @param listener -
* removal listener to use
* @returns self
*/
withRemovalListener(listener: RemovalListener<K, V>): this;
/**
* Set the maximum number of items to keep in the cache before evicting
* something.
*
* @param size -
* number of items to keep
* @returns self
*/
maxSize(size: number): this;
/**
* Set a function to use to determine the size of a cached object.
*
* @param weigher -
* function used to weight objects
* @returns self
*/
withWeigher(weigher: Weigher<K, V>): this;
/**
* Change to a cache where get can also resolve values if provided with
* a function as the second argument.
*
* @returns self
*/
loading(): LoadingCacheBuilder<K, V>;
/**
* Change to a loading cache, where the get-method will return instances
* of Promise and automatically load unknown values.
*
* @param loader -
* function used to load objects
* @returns self
*/
withLoader(loader: Loader<K, V>): LoadingCacheBuilder<K, V>;
/**
* Set that the cache should expire items after some time.
*
* @param time -
* max time in milliseconds, or function that will be asked per key/value
* for expiration time
* @returns self
*/
expireAfterWrite(time: number | MaxAgeDecider<K, V>): this;
/**
* Set that the cache should expire items some time after they have been read.
*
* @param time -
* max time in milliseconds, or function will be asked per key/value
* for expiration time
* @returns self
*/
expireAfterRead(time: number | MaxAgeDecider<K, V>): this;
/**
* Activate tracking of metrics for this cache.
*
* @returns self
*/
metrics(): this;
/**
* Build and return the cache.
*
* @returns cache
*/
build(): Cache<K, V>;
}