diff --git a/README.md b/README.md index 850ec2c..cec19ac 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,37 @@ allowing automatic cache update and expiration according to the user's needs. `npm i --save try-cache` +## Options + +### Initiation options + +``` +/** + * @param silent - if true, don't log anything to the console. Defaults to false. + * @param expire - the default expiration time in seconds. Defaults to 5 mins. + */ +export type TCOptions = { + silent: boolean; + expire: number; +}; +``` + +### Operation Options + +``` +/** + * @param expire - the expiration time in seconds for the specific operation. Default to TCOptions' expire. + * @param callbackFunction - the function to call if the retrieveFunction throws an error after cache failed. Defaults to "do nothing". + * @param 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; +}; + +``` + ## Usage Example ``` diff --git a/package-lock.json b/package-lock.json index 78b1cd6..3b484f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "try-cache", - "version": "1.0.9", + "version": "1.0.10", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2353d85..11cfd05 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "try-cache", "description": "An auto-caching npm package for super-fast retrieval of less-consistant data", - "version": "1.0.9", + "version": "1.0.10", "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { 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..5ab0978 100644 --- a/src/paramTypes.ts +++ b/src/paramTypes.ts @@ -1,13 +1,19 @@ +/** + * @param silent - if true, don't log anything to the console + * @param expire - the default expiration time in seconds + */ export type TCOptions = { silent: boolean; expire: number; }; /** - * expire - the expiration time in seconds. - * callbackFunction - the function to call if the retrieveFunction throws an error after cache failed. + * @param expire - the expiration time in seconds. + * @param callbackFunction - the function to call if the retrieveFunction throws an error after cache failed. + * @param 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; };