UNPKG

use-cache-helper

Version:

use-cache-helper provides helper functions to easily manage and scale your redis and database caching strategies.

133 lines (132 loc) 6.46 kB
import { IAppInitParams, IGetOrRefreshDataInPaginatedListParams, IGetOrRefreshParams, IGetOrRefreshReturnValue, IGetPaginatedListByPageParams, IInsertPaginatedListItemParams, IRemoveItemFromPaginatedListParams, ISetParams, IUpdateItemScoreFromPaginatedList } from "./types"; /** * Get latest cache data or force refresh before returning the value * @param {Object} params * @param {string} params.key - Data cache key * @param {number} params.expiry - (optional) Expiry in seconds * @param {boolean} params.forceRefresh - (optional) Force refresh * @param {boolean} params.parseResult - (optional) Call JSON.parse on resulting data * @param {function} params.cacheRefreshHandler - (optional) Refresh function * @returns */ export declare const getOrRefresh: <T>(params: IGetOrRefreshParams<T>) => IGetOrRefreshReturnValue<T>; /** * Retrieve the formatted default cache key for an item in a paginated list. * @param {string} listKey - Your list's cache key * @param {string} itemId - Your item id * @returns {string} */ export declare const getDefaulItemCacheKeyForPaginatedList: (listKey: string, itemId: string) => string; /** * Get latest cache data of 'key'. * Wraps the `getOrRefresh` function and enables you to update the score of an item in the list. * @param {Object} params * @param {string} params.listKey - Your list's cache key * @param {string} params.id - Item ID * @param {string} params.key - (optional) Data cache key * @param {number} params.expiry - (optional) Expiry in seconds * @param {boolean} params.forceRefresh - (optional) Force refresh * @param {boolean} params.parseResult - (optional) Call JSON.parse on resulting data * @param {number} params.score - (optional) Determines the new score of the cache data. Update LRU score. * @param {boolean} params.updateScoreInPaginatedList - (optional) If set to true, thn apply new score. * @param {function} params.cacheRefreshHandler - (optional) Refresh function * @returns {Object} IGetOrRefreshReturnValue */ export declare const getOrRefreshDataInPaginatedList: <T>(params: IGetOrRefreshDataInPaginatedListParams<T>) => IGetOrRefreshReturnValue<T>; /** * Set redis cache. * Object data will be sanitzed with JSON.stringify() * @param {Object} params * @param {string} params.key - Data cache key * @param {string | number | Object} params.value - Your data for the cache key * @param {number} expiry - (optional) Expiry in seconds. No expiry set by default. * @returns {string} 'OK' | 'Error' */ export declare const set: <T>(params: ISetParams<T>) => Promise<string | "OK">; /** * Required initial function to run from the start of your app * @param params * @param {Object} params.redis - (optional) Your ioredis instance * @param {Object} params.upstashRedis - (optional) Your @upstash/redis instance * @param {number} params.maxPaginatedItems - (optional) Maximum number of paginated items before it starts evicting data. */ export declare const init: (params: IAppInitParams) => void; /** * Get paginated list by page. * Always in ascending order. * @param {Object} params * @param {string} params.key - Your list's cache key * @param {number} params.page - Target page * @param {number} params.sizePerPage - Total items in a single page * @param {boolean} params.ascendingOrder - (optional) Fetch list in ascending order. High to low by default. * @returns {Object} IGetPaginatedListByPageParams */ export declare const getPaginatedListByPage: (params: IGetPaginatedListByPageParams) => Promise<string[]>; /** * Fetch total items in a list * @param {string} key - Your list's cache key * @returns */ export declare const getPaginatedListTotalItems: (key: string) => Promise<number>; /** * Automatically insert ID data from your array of objects. * Use non-zero & non-negative scores. * Each payload will be cac * @param {Object} params * @param {string} params.listKey - Your list's cache key * @param {Array} params.listData - Your list data in array form. * @param {string} params.cacheDataPrefix - Prefix cache to your data. Example: `users:${id}` * @param {boolean} params.cachePayload - If set to true, each payload in the list will be cached. * @param {number} params.cachePayloadExpiry - Expiry for each payload cache, unit in seconds. * @returns {string} - 'OK' | 'Error' */ export declare const insertRecordsToPaginatedList: <T>(params: { listKey: string; listData: T & { score: number; id: string; }[]; cacheDataPrefix?: string; cachePayload?: boolean; cachePayloadExpiry?: number; }) => Promise<string | "OK">; /** * Insert an item to the list using the item ID. * Use non-zero & non-negative scores. * @param {Object} params * @param {string} params.key - Your list's cache key * @param {string} params.id - Data id * @param {number} params.score - (optional) Score order of the item in the paginated list, determining its placement. * @returns {string} */ export declare const insertToPaginatedList: (params: IInsertPaginatedListItemParams) => Promise<string | "OK">; /** * Remove item from the list. * @param {Object} params * @param {string} params.key - Your list's cache key * @param {string} params.id - Item ID * @returns {string} 'OK' | 'Error' */ export declare const removeItemFromPaginatedList: (params: IRemoveItemFromPaginatedListParams) => Promise<string | "OK">; /** * Update the score of an item in the paginated list to move it up or down in the order. * @param {Object} params * @param {string} params.key - Your list's cache key * @param {string} params.id - Item ID* * @param {number} params.score - Score order of the item in the paginated list, determining its placement. * @returns */ export declare const updateItemScoreFromPaginatedList: (params: IUpdateItemScoreFromPaginatedList) => Promise<string | "OK">; /** * Generate a string key for your cache based on the formatted filter properties of your database query. * {"limit" : 1 , "team" : "team-id" } => "limit1Teamteamid" * @param filters * @returns */ export declare const generateKeyFromQueryFilters: (filters: Record<string, any>) => string; /** * Hard delete paginated list and items * @param {string} listKey * @returns {string | number} Status from upstash redis or ioredis */ export declare const deletePaginatedList: (listKey: string) => Promise<string | number>;