Skip to content

Commit

Permalink
Add Redis cache plugin (#50)
Browse files Browse the repository at this point in the history
* Redis cache (#49)

---------

Co-authored-by: Campbell <campbellmc@users.noreply.github.com>
Co-authored-by: Dilraj Sachdev <dilrajsachdev@gmail.com>
  • Loading branch information
3 people authored Jun 29, 2024
1 parent e324e05 commit de42561
Show file tree
Hide file tree
Showing 16 changed files with 2,546 additions and 7,370 deletions.
45 changes: 38 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ jobs:
image: kennethreitz/httpbin
ports:
- 3000:80

env:
HTTP_BIN_BASE_URL: http://localhost:3000
redis:
image: redis
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2
- name: Use Node.js 20.x
uses: actions/setup-node@v1
with:
node-version: 20.x
- run: npm ci --prefix plugins/redis
- run: npm ci
- run: npm run tsc
- run: npm run buildcjs
Expand All @@ -30,29 +32,58 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

test_node_14_14_0:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.14.0]

services:
httpbin:
image: kennethreitz/httpbin
ports:
- 3000:80

steps:
- uses: actions/checkout@v2
- name: Use Node.js 20.x
uses: actions/setup-node@v1
with:
node-version: 20.x
- run: npm ci
- run: npm run tsc
- run: npm run buildcjs
- name: Use Node.js Version Under Test
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm run test:core

test_node_other:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.14.0, 14.x, 16.x, 18.x, 21.x]
node-version: [14.x, 16.x, 18.x, 22.x]

services:
httpbin:
image: kennethreitz/httpbin
ports:
- 3000:80

env:
HTTP_BIN_BASE_URL: http://localhost:3000
redis:
image: redis
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2
- name: Use Node.js 20.x
uses: actions/setup-node@v1
with:
node-version: 20.x
- run: npm ci --prefix plugins/redis
- run: npm ci
- run: npm run tsc
- run: npm run buildcjs
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,29 @@ Options:
// automatically ejected from the cache.
ttl: 1000,
}

```
### Cache with Redis

Use the [@node-fetch-cache/redis](https://www.npmjs.com/package/@node-fetch-cache/redis) package to cache in Redis.

### Cache in Memory with a TTL

If you would like to cache in memory and automatically eject responses after a certain amount of time (in ms), you can create a custom instance of the `MemoryCache` class and use that:

```js
import NodeFetchCache, { MemoryCache } from 'node-fetch-cache';
const fetch = NodeFetchCache.create({ cache: new MemoryCache({ ttl: 1000 }) });

const fetch = NodeFetchCache.create({
cache: new MemoryCache({ ttl: 1000 })
});
```

Note that the default cache is a globally shared instance of `MemoryCache` with no TTL.

### Implement your Own Cache

If neither `MemoryCache` nor `FileSystemCache` meet your needs, you can implement your own cache. You can use any object that implements the following interface:
If none of the existing caching options meet your needs, you can implement your own cache. You can use any object that implements the following interface:

```ts
type INodeFetchCacheCache = {
Expand Down Expand Up @@ -365,4 +372,11 @@ For bug reports, please file an issue on [the issues page on GitHub](https://git

Contributions welcome! Please open a [pull request on GitHub](https://github.com/mistval/node-fetch-cache/pulls) with your changes. You can run them by me first on [the discussions page](https://github.com/mistval/node-fetch-cache/discussions) if you'd like. Please add tests for any changes.

To accelerate the tests, run `docker run -p 3000:80 kennethreitz/httpbin` and set an environment variable: `HTTP_BIN_BASE_URL=http://localhost:3000` (`.env` file is supported) before running the tests with `npm test`.
To run the tests, first start an httpbin and Redis container:

```sh
docker run -p 3000:80 -d kennethreitz/httpbin
docker run -p 6379:6379 -d redis
```

Then `npm test`.
Loading

0 comments on commit de42561

Please sign in to comment.