Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

website - updating caching documentation to use Cacheable with Keyv #1209

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions packages/website/site/docs/caching/caching-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,24 @@ A cache is a short-term, high-speed data storage layer that stores a subset of d
## Caching Support in Keyv
Caching will work in memory by default. However, users can also install a Keyv storage adapter that is initialized with a connection string or any other storage that implements the Map API.

## Extend your own Module with Keyv to Add Cache Support
- Keyv can be easily embedded into other modules to add cache support.
- You should also set a namespace for your module to safely call `.clear()` without clearing unrelated app data.
## Caching Support in Keyv via Cacheable

We can use Keyv to implement caching using [Cacheable](https://npmjs.org/package/cacheable) which is a high performance layer 1 / layer 2 caching framework built on Keyv. It supports multiple storage backends and provides a simple, consistent API for caching.


>**Note**:
> The recommended pattern is to expose a cache option in your module's options which is passed through to Keyv.

### Example - Add Cache Support to a Module

1. Install whichever storage adapter you will be using, `@keyv/redis` in this example
```sh
npm install --save @keyv/redis
npm install --save @keyv/redis cacheable
```
2. Declare the Module with the cache controlled by a Keyv instance
```js
class AwesomeModule {
constructor(opts) {
this.cache = new Keyv({
uri: typeof opts.cache === 'string' && opts.cache,
store: typeof opts.cache !== 'string' && opts.cache,
namespace: 'awesome-module'
});
}
}
```
import { Cacheable } from 'cacheable';
import KeyvRedis from '@keyv/redis';

3. Create an Instance of the Module with caching support
```js
const AwesomeModule = require('awesome-module');
const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });
// by default layer 1 cache is in-memory. If you want to add a layer 2 cache, you can use KeyvRedis
const secondary = new KeyvRedis('redis://user:pass@localhost:6379');
const cache = new Cacheable({ secondary, ttl: '4h' }); // default time to live set to 4 hours
```
Loading