with-simple-caching
Version:
A wrapper that makes it simple to add caching to any function
102 lines (101 loc) • 3.52 kB
TypeScript
import { SimpleSyncCache } from '../../domain/SimpleCache';
import { WithSimpleCachingOptions } from './withSimpleCaching';
/**
* enumerates the extendable methods which can trigger cache operations
*/
export declare enum WithExtendableCachingTrigger {
EXECUTE = "EXECUTE",
INVALIDATE = "INVALIDATE",
UPDATE = "UPDATE"
}
/**
* a simple typeguard which checks if an object has a property named `forInput`
*/
export declare const hasForInputProperty: (obj: any) => obj is {
forInput: any;
};
/**
* the shape of logic that was wrapped with extendable caching for a sync cache
*/
export interface LogicWithExtendableCaching<
/**
* the logic we are caching the responses for
*/
L extends (...args: any) => any,
/**
* the type of cache being used
*/
C extends SimpleSyncCache<any>> {
/**
* execute the logic with caching
*/
execute: L;
/**
* invalidate the cached value for a given input
*
* note
* - applies key serialization on the key input, just like execute
*/
invalidate: (args: {
/**
* invalidate the cache for this input
*/
forInput: Parameters<L>;
} | {
/**
* invalidate the cache for this key
*/
forKey: string;
/**
* the cache to use, if the cache must be was defined from input parameters at runtime
*/
cache?: C;
}) => void;
/**
* update the cached value for a given input
*
* note
* - applies key serialization on the key input, just like execute
* - applies value serialization on the value output, just like execute
*/
update: (args: {
/**
* update the cache for this input
*/
forInput: Parameters<L>;
/**
* update the cache to this value
*/
toValue: ReturnType<L> | ((args: {
fromCachedOutput: ReturnType<L> | undefined;
}) => ReturnType<L>);
} | {
/**
* update the cache for this string
*/
forKey: string;
/**
* update the cache to this value
*/
toValue: ReturnType<L> | ((args: {
fromCachedOutput: ReturnType<L> | undefined;
}) => ReturnType<L>);
/**
* the cache to use, if the cache must be was defined from input parameters at runtime
*/
cache?: C;
}) => void;
}
/**
* exposes the cache-wrapped method along with some primitives which enable extending the caching logic
*
* specifically
* - exposes a way to `invalidate` the cache, for a given input (e.g., to support external triggers for invalidation)
* - exposes a way to `update` the cache, for a given input (e.g., to support write-through caching and optimistic caching)
*
* relevance
* - when wrapping logic to cache the user is able to specify several caching options (e.g., key serialization method, value serialization method, etc)
* - in order to define their own `invalidation` and `update` methods, without this function, the user would need to access these caching options per function elsewhere
* - this function makes it easy to utilize and extend cache invalidation + update commands for the wrapped logic, by managing the references to the caching options on behalf of the user
*/
export declare const withExtendableCaching: <L extends (...args: any) => any, C extends SimpleSyncCache<any>>(logic: L, options: WithSimpleCachingOptions<L, C>) => LogicWithExtendableCaching<L, C>;