Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/apostrophecms/cache-redis


https://github.com/apostrophecms/cache-redis

Last synced: 9 days ago
JSON representation

Awesome Lists containing this project

README

        


ApostropheCMS logo

Apostrophe Redis Cache







GitHub Workflow Status (branch)








This module enhances the core caching module, `@apostrophecms/cache`, to use [Redis](https://redis.io/) rather than MongoDB. This module does not set up the actual Redis store, but instead allows Apostrophe to access an existing Redis store through the standard Apostrophe caching API and an internal Redis client.

All normal Apostrophe cache API features are maintained in addition to the Redis-specific features described below.

## Installation

To install the module, use the command line to run this command in an Apostrophe project's root directory:

```
npm install @apostrophecms/cache-redis
```

## Usage

Configure the Redis cache module in the `app.js` file:

```javascript
require('apostrophe')({
shortName: 'my-project',
modules: {
'@apostrophecms/cache-redis': {}
}
});
```

## Configuring Redis options

**All options for this module should be applied to `@apostrophecms/cache` in project code.** This module simply "improves" that core module (updates its features). The main caching module still does all the work.

Configure the underlying Redis client by adding a configuration object on the cache module's `redis` option.

```javascript
// modules/@apostrophecms/cache/index.js
module.exports = {
options: {
redis: {
url: 'redis://alice:[email protected]:6380'
}
}
};
```

By default, the client will look for a Redis server running on localhost port 6379. See [all the client configuration options](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md) on the Node-Redis documentation.

Note that this module uses the 4.x version of Node-Redis, which changed its configuration options from earlier major versions.

## Cache keys

The Apostrophe cache `get` and `set` methods take two arguments that are used for individual cache item keys:

| Argument | Description |
| -------- | ----------- |
| `namespace` | A namespace for related data. Apostrophe core often uses the active module's name for the namespace, e.g., `@apostrophecms/oembed`. |
| `key` | The unique cache item key within a namespace. |

```javascript
await apos.cache.get(namespace, key)
await apos.cache.set(namespace, key, value)
```

Example:

```javascript
const fetch = node
// modules/api-connect/index.js
module.exports = {
apiRoutes(self) {
return {
get: {
// GET /api/v1/api-connect/set-cache-info
async setCacheInfo(req) {
const info = await myApiClient.get({ latest: true });

// 👇 This status will be "OK" if successful. This is due to the Redis
// API, not a common Apostrophe pattern.
const status = await self.apos.cache.set('api-connect', 'latest', info);

return { status };
},
// GET /api/v1/api-connect/get-cache-info
async getCacheInfo(req) {
// 👇 This will return the stored information or `undefined` if not
// set.
const latest = await self.apos.cache.get('api-connect', 'latest');

return { latest };
}
}
};
}
};
```

### Using the `prefix` for multiple sites

By default, this module applies a prefix to cache keys in the store. This prefix is the shortname of the website, which automatically separates cache items if multiple Apostrophe sites share the same Redis store.

To disable the cache key prefix, set the cache module's `prefix` option to `false`.

```javascript
// modules/@apostrophecms/cache/index.js
module.exports = {
options: {
prefix: false
}
};
```

To customize the prefix, set the `prefix` option to the desired string value.

```javascript
// modules/@apostrophecms/cache/index.js
module.exports = {
options: {
prefix: 'project-alpha'
}
};
```