diff --git a/src/index.ts b/src/index.ts index cb1df2c..4886768 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,7 +67,7 @@ class TryCache { * If it succeeds it will return the cached value, and update the cache in the background. * @param key - the key to get. * @param retrieveFunction - the function to call if the key is not found in cache. <() => func(...params)>. - * @param opts - the options to use: { expire: number, callbackFunction: Function }. + * @param opts - the options to use: { expire: number, callbackFunction: Function, forceDB: boolean }. * @returns the requested cache value, or the result of the retrieveFunction * if no cache value is found. */ @@ -81,10 +81,16 @@ class TryCache { callbackFunction: opts?.callbackFunction ? opts.callbackFunction : defaults.defaultCallbackFunction, + forceDB: opts?.forceDB ? opts.forceDB : false, }; try { - const cachedValue = await this.safeGetFromCache(key); - // If no value found, retrieve from DB and set + let cachedValue; + + if (!operationOpts.forceDB) { + cachedValue = await this.safeGetFromCache(key); + } + + // If no value found or forceDB activated, retrieve from DB and update cache if (!cachedValue) { const result = await retrieveFunction(); await this.safeSetCache(key, result, operationOpts.expire); diff --git a/src/paramTypes.ts b/src/paramTypes.ts index 1a6b589..e514044 100644 --- a/src/paramTypes.ts +++ b/src/paramTypes.ts @@ -6,8 +6,10 @@ export type TCOptions = { /** * expire - the expiration time in seconds. * callbackFunction - the function to call if the retrieveFunction throws an error after cache failed. + * forceDB - if true, will force the retrieveFunction to be called even if the key is found in cache. Defaults to false. */ export type OperationOptions = { expire: number; callbackFunction: Function; + forceDB: boolean; };