From 03a3a7cbc8a99b5ec85871c0d785810f3714160b Mon Sep 17 00:00:00 2001 From: shahar-y Date: Wed, 23 Mar 2022 14:47:13 +0300 Subject: [PATCH 1/3] added forceDB option --- src/index.ts | 12 +++++++++--- src/paramTypes.ts | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) 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; }; From 6c83b206481781159339628b7fec2f287c648a68 Mon Sep 17 00:00:00 2001 From: shahar-y Date: Wed, 23 Mar 2022 14:54:05 +0300 Subject: [PATCH 2/3] updated README --- README.md | 31 +++++++++++++++++++++++++++++++ src/paramTypes.ts | 10 +++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) 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/src/paramTypes.ts b/src/paramTypes.ts index e514044..5ab0978 100644 --- a/src/paramTypes.ts +++ b/src/paramTypes.ts @@ -1,12 +1,16 @@ +/** + * @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. - * forceDB - if true, will force the retrieveFunction to be called even if the key is found in cache. Defaults to false. + * @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; From ccaa8907d9521ca9ac477956550b0f22557aa6cb Mon Sep 17 00:00:00 2001 From: shahar-y Date: Wed, 23 Mar 2022 14:54:19 +0300 Subject: [PATCH 3/3] 1.0.10 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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": {