Skip to content

Commit

Permalink
Merge pull request #3 from Shahar-Y/develop
Browse files Browse the repository at this point in the history
Version 1.0.8
  • Loading branch information
Shahar-Y authored Mar 21, 2022
2 parents 941dfb3 + 37bc330 commit a235ffa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
# try-cache

An auto-caching npm package for super-fast retrieval of data that doesn't have to be consistant.
![npm](https://img.shields.io/npm/v/try-cache?color=green)
![NPM](https://img.shields.io/npm/l/try-cache)
![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/try-cache)
![npm](https://img.shields.io/npm/dt/try-cache)

An auto-caching npm package for super-fast retrieval of data that doesn't have to be atomicly consistant,
allowing automatic cache update and expiration according to the user's needs.

## Installation

`npm i --save try-cache`

## Usage Example

```
import { TryCache } from 'try-cache';
// Sets the expiration to 20 seconds, and alerts on calls
const cb = new TryCache('redis://localhost:6379', { silent: false, expire: 20 });
// Initiates the connection to redis
await cb.initTryCache();
// Should take 3 seconds
const res1 = await cb.tryCache('myKey', () => dummyDB(2, 7));
console.log('First time result:', res1);
// Should take a few ms because cahe is saved
const res2 = await cb.tryCache('myKey', () => dummyDB(2, 7));
console.log('Second time result:', res2);
// dummyDB simulates a database call
async function dummyDB(x: number, y: number) {
await sleep(3000);
return { x, y };
}
```

## To run the package locally with the example:

1. Run the docker-compose (or run redis locally manually): `docker-compose -f "src\miscellaneous\docker-compose.yml" up -d --build`
2. Run the example: `npm run example`

The output should show the different use-cases of the package (with and without cache at the beginning)

- Notice! The third and fourth runs should successfully fail as they simulate the failure of the original db retrieval function.
7 changes: 0 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,4 @@ services:
- '6379:6379'
expose:
- 6379

try-cache:
container_name: try-cache
build: .
ports:
- '3000:3000'
restart: always
# docker-compose -f "src\miscellaneous\docker-compose.yml" up -d --build
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.

9 changes: 6 additions & 3 deletions 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.5",
"version": "1.0.7",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand All @@ -16,10 +16,13 @@
"url": "git+https://github.com/Shahar-Y/try-cache.git"
},
"keywords": [
"redis",
"try-cache",
"try",
"cache",
"node",
"redis",
"async",
"efficiency",
"performance",
"db",
"database"
],
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defaults } from './defaults';

export let logger: Logger;

export class TryCache {
class TryCache {
options: TCOptions;
redisConnectionString: string;

Expand Down Expand Up @@ -109,3 +109,5 @@ export class TryCache {
}
}
}

export { TryCache, TCOptions, OperationOptions };

0 comments on commit a235ffa

Please sign in to comment.