Skip to content

Commit

Permalink
Merge pull request #5 from Shahar-Y/develop
Browse files Browse the repository at this point in the history
Version 1.0.10 - forceDB
  • Loading branch information
Shahar-Y authored Mar 23, 2022
2 parents 2c30ad2 + ccaa890 commit 6cfe58d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/paramTypes.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 6cfe58d

Please sign in to comment.