Skip to content

Commit 6cfe58d

Browse files
authored
Merge pull request #5 from Shahar-Y/develop
Version 1.0.10 - forceDB
2 parents 2c30ad2 + ccaa890 commit 6cfe58d

5 files changed

Lines changed: 50 additions & 7 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ allowing automatic cache update and expiration according to the user's needs.
1212

1313
`npm i --save try-cache`
1414

15+
## Options
16+
17+
### Initiation options
18+
19+
```
20+
/**
21+
* @param silent - if true, don't log anything to the console. Defaults to false.
22+
* @param expire - the default expiration time in seconds. Defaults to 5 mins.
23+
*/
24+
export type TCOptions = {
25+
silent: boolean;
26+
expire: number;
27+
};
28+
```
29+
30+
### Operation Options
31+
32+
```
33+
/**
34+
* @param expire - the expiration time in seconds for the specific operation. Default to TCOptions' expire.
35+
* @param callbackFunction - the function to call if the retrieveFunction throws an error after cache failed. Defaults to "do nothing".
36+
* @param forceDB - if true, will force the retrieveFunction to be called even if the key is found in cache. Defaults to false.
37+
*/
38+
export type OperationOptions = {
39+
expire: number;
40+
callbackFunction: Function;
41+
forceDB: boolean;
42+
};
43+
44+
```
45+
1546
## Usage Example
1647

1748
```

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "try-cache",
33
"description": "An auto-caching npm package for super-fast retrieval of less-consistant data",
4-
"version": "1.0.9",
4+
"version": "1.0.10",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TryCache {
6767
* If it succeeds it will return the cached value, and update the cache in the background.
6868
* @param key - the key to get.
6969
* @param retrieveFunction - the function to call if the key is not found in cache. <() => func(...params)>.
70-
* @param opts - the options to use: { expire: number, callbackFunction: Function }.
70+
* @param opts - the options to use: { expire: number, callbackFunction: Function, forceDB: boolean }.
7171
* @returns the requested cache value, or the result of the retrieveFunction
7272
* if no cache value is found.
7373
*/
@@ -81,10 +81,16 @@ class TryCache {
8181
callbackFunction: opts?.callbackFunction
8282
? opts.callbackFunction
8383
: defaults.defaultCallbackFunction,
84+
forceDB: opts?.forceDB ? opts.forceDB : false,
8485
};
8586
try {
86-
const cachedValue = await this.safeGetFromCache(key);
87-
// If no value found, retrieve from DB and set
87+
let cachedValue;
88+
89+
if (!operationOpts.forceDB) {
90+
cachedValue = await this.safeGetFromCache(key);
91+
}
92+
93+
// If no value found or forceDB activated, retrieve from DB and update cache
8894
if (!cachedValue) {
8995
const result = await retrieveFunction();
9096
await this.safeSetCache(key, result, operationOpts.expire);

src/paramTypes.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
/**
2+
* @param silent - if true, don't log anything to the console
3+
* @param expire - the default expiration time in seconds
4+
*/
15
export type TCOptions = {
26
silent: boolean;
37
expire: number;
48
};
59

610
/**
7-
* expire - the expiration time in seconds.
8-
* callbackFunction - the function to call if the retrieveFunction throws an error after cache failed.
11+
* @param expire - the expiration time in seconds.
12+
* @param callbackFunction - the function to call if the retrieveFunction throws an error after cache failed.
13+
* @param forceDB - if true, will force the retrieveFunction to be called even if the key is found in cache. Defaults to false.
914
*/
1015
export type OperationOptions = {
1116
expire: number;
1217
callbackFunction: Function;
18+
forceDB: boolean;
1319
};

0 commit comments

Comments
 (0)