UNPKG

@kuflow/kuflow-temporal-worker

Version:

Worker library used by KuFlow SDKs and Temporal.

109 lines (108 loc) 4.35 kB
/** * The MIT License * Copyright © 2021-present KuFlow S.L. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ type CacheLoader<V> = () => Promise<V> | V; type TimeUnit = 'hour' | 'hours' | 'minute' | 'minutes' | 'second' | 'seconds' | 'millisecond' | 'milliseconds'; export interface CacheOptions<K> { expireAfterAccess: number; expireAfterWrite: number; removalListener: (key: K) => void; } export declare class Cache<K, V> { private readonly cache; private readonly expireAfterAccess; private readonly expireAfterWrite; private readonly removalListener; private readonly inProgressLoads; constructor(opts: CacheOptions<K>); /** * Retrieve a value from the cache, or load it using the provided loader if absent or expired. * Automatically refreshes the TTL upon access. * @param key - The key of the value to retrieve. * @param loader - A loader function to fetch the value if it's not in cache. * @returns The value associated with the key. */ get(key: K, loader: CacheLoader<V>): Promise<V>; /** * Add a key-value pair to the cache with an expiration time. * @param key - The key to store. * @param value - The value to store. */ put(key: K, value: V): void; /** * Invalidate a specific key from the cache. * @param key - The key to remove. */ invalidate(key: K): void; /** * Invalidate all items from the cache. */ invalidateAll(): void; /** * Create or Update the cache cached entry. * @param key - The key of the entry. * @param value - The value of the entry. * @param ttl - The ttl of the entry. */ private createOrUpdateCacheEntry; /** * Deletes a cache entry identified by the specified key. * This will also clear any associated timeout for the cache entry. * * @param key - The key of the cache entry to be deleted. * @return No return value. */ private deleteCacheEntry; private clearTimeoutCacheEntry; } export declare class CacheBuilder<K, V> { private expireAfterAccess; private expireAfterWrite; private removalListener; static builder<K, V>(): CacheBuilder<K, V>; /** * Set the expiration time for the cache after accessing the element. * @param time - The time-to-live duration. * @param unit - The time unit (such as milliseconds, seconds, minutes, etc.). */ withExpireAfterAccess(time: number, unit: TimeUnit): this; /** * Set the expiration time for the cache after write the element. * @param time - The time-to-live duration. * @param unit - The time unit (such as milliseconds, seconds, minutes, etc.). */ withExpireAfterWrite(time: number, unit: TimeUnit): this; /** * Sets a removal listener function to be called when an entry is removed. * * @param {Function} removalListener - A function that gets called with the key of the removed entry. * @return {this} Returns the current instance to allow method chaining. */ withRemovalListener(removalListener: (key: K) => void): this; private toMillis; private multiplier; /** * Build and return the Cache instance configured with the specified options. */ build(): Cache<K, V>; } export {};