import type { AllConfig } from '../../../config/types' ; import * as memCache from '../memory' ; import * as fileCache from './file' ; import * as redisCache from './redis' ; import type { PackageCache } from './types' ; let cacheProxy ; function getGlobalKey ( namespace , key ) { return `global%%${ namespace }%%${ key }` ; } export async function get < T = any > ( namespace , key ) { if ( ! cacheProxy ) { return undefined ; } const globalKey = getGlobalKey ( namespace , key ) ; if ( memCache . get ( globalKey ) === undefined ) { memCache . set ( globalKey , cacheProxy . get ( namespace , key ) ) ; } const result = await memCache . get ( globalKey ) ; return result ; } export async function set ( namespace , key , value , minutes ) { if ( ! cacheProxy ) { return ; } const globalKey = getGlobalKey ( namespace , key ) ; memCache . set ( globalKey , value ) ; await cacheProxy . set ( namespace , key , value , minutes ) ; } export function init ( config ) { if ( config . redisUrl ) { redisCache . init ( config . redisUrl ) ; cacheProxy = { get : redisCache . get , set : redisCache . set , } ; } else if ( config . cacheDir ) { fileCache . init ( config . cacheDir ) ; cacheProxy = { get : fileCache . get , set : fileCache . set , } ; } } export function cleanup ( config ) { if ( config ?. redisUrl ) { redisCache . end ( ) ; } }