@nrk/nodecache-as-promised
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -9,3 +9,3 @@ { | ||
"homepage": "https://github.com/nrkno/nodecache-as-promised#readme", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "NodeJs in-memory cache with Promise support. Extendable with middlewares. Middlewares provided: Distributed invalidation and persistence of cache misses", | ||
@@ -12,0 +12,0 @@ "main": "lib/index.js", |
125
README.md
@@ -9,5 +9,5 @@ # @nrk/nodecache-as-promised | ||
- [Examples](#examples) | ||
- [Distributed capabilites](#distributed-capabilites) | ||
- [Middlewares](#middlewares) | ||
- [Creating your own middlewares](#creating-your-own-middleware) | ||
- [Local development](#local-development) | ||
- [Creating your own middleware](#creating-your-own-middleware) | ||
- [Building and committing](#building-and-committing) | ||
@@ -24,3 +24,3 @@ | ||
`nodecache-as-promised` is inspired by how [Varnish](https://varnish-cache.org/) works. It is not intended to replace Varnish (but works great in combination). Whereas Varnish is a high-performant edge/burst/failover cache, working as a reverse proxy and loadbalancer, it depends on a fast backend when configured with short a cache window (ie. TTL ~1s). It uses URLs in combination with cookies as keys for its cached content. Since there are no restrictions on conformant URLs/cookies for clients requesting content, it is quite easy to bust it's cache without any security measures. `nodecache-as-promised` on the other hand is running at application level for more strict handling of cache keys, and may use many different caches and policies on how the web page is built. Look [below](#advanced-usage) for more advanced use cases. | ||
`nodecache-as-promised` is inspired by how [Varnish](https://varnish-cache.org/) works. It is not intended to replace Varnish (but works great in combination). Whereas Varnish is a high-performant edge/burst/failover cache, working as a reverse proxy and loadbalancer, it depends on a fast backend when configured with short a cache window (ie. TTL ~1s). It uses URLs in combination with cookies as keys for its cached content. Since there are no restrictions on conformant URLs/cookies for clients requesting content, it is quite easy to bust it's cache without any security measures. `nodecache-as-promised` on the other hand is running at application level for more strict handling of cache keys, and may use many different caches and policies on how the web page is built. | ||
@@ -71,36 +71,2 @@ ### Features | ||
### distCache factory | ||
Creating a new distCache middleware instance. The distCache middleware is extending the inMemoryCache instance by making a publish call to Redis using the provided `namespace` when the `.expire`-method is called. A subscription to the `namespace` ensures calls to `.expire` is distributed to all instances of the inMemoryCache using the same distCache middleware with the same `namespace`. It adds a couple of parameters to the `.debug`-method. | ||
```js | ||
import cache, {distCache} from '@nrk/nodecache-as-promised' | ||
const cache = inMemoryCache() | ||
cache.use(distCache(redisFactory, namespace)) | ||
``` | ||
#### Parameters | ||
Parameters that must be provided upon creation: | ||
- redisFactory - `Function`. A function that returns an ioredis compatible redisClient. | ||
- namespace - `String`. Pub/sub-namespace used for distributed expiries | ||
### persistentCache factory | ||
Creating a new persistentCache middleware instance. The persistentCache middleware is extending the inMemoryCache instance by serializing and storing any new values recieved via workers in `.get` or in `.set`-calls to Redis. In addition it deletes values from Redis when the `.del` and `.clear`-methods are called. Cache values evicted by the LRU-cache are also deleted. On creation it will load and set initial cache values by doing a search for stored keys on the provided `keySpace` (may be disabled using the option `bootLoad: false` - so that loading may be done afterwards using the provided `.load`-method). It adds a couple of parameters to the `.debug`-method. | ||
```js | ||
import cache, {persistentCache} from '@nrk/nodecache-as-promised' | ||
const cache = inMemoryCache() | ||
cache.use(persistentCache(redisFactory, options)) | ||
``` | ||
#### Parameters | ||
Parameters that must be provided upon creation: | ||
- redisFactory - `Function`. A function that returns an ioredis compatible redisClient. | ||
#### options | ||
- doNotPersist - `RegExp`. Keys matching this regexp is not persisted to cache. Default `null` | ||
- keySpace - `String`. Prefix used when storing keys in redis. | ||
- grace - `Number`. Used to calculate TTL in redis (before auto removal), ie. object.TTL + grace. Default `86400000` (24h) | ||
- bootload - `Boolean`. Flag to choose if persisted cache is loaded from redis on middleware creation. Default `true` | ||
### Instance methods | ||
@@ -255,7 +221,20 @@ When the factory is created (with or without middlewares), the following methods may be used. | ||
### Distributed capabilites | ||
Distributed expire and persisting of cache misses to Redis are provided as middlewares, ie. extending the in-memory cache interceptors. Writing your own middlewares using pub/sub from rabbitMQ, zeroMQ, persisting to a NAS, counting hit/miss-ratios should be easy. | ||
## Middlewares | ||
#### Distributed expire | ||
### distCache middleware | ||
Creating a new distCache middleware instance. The distCache middleware is extending the inMemoryCache instance by making a publish call to Redis using the provided `namespace` when the `.expire`-method is called. A subscription to the `namespace` ensures calls to `.expire` is distributed to all instances of the inMemoryCache using the same distCache middleware with the same `namespace`. It adds a couple of parameters to the `.debug`-method. | ||
```js | ||
import cache, {distCache} from '@nrk/nodecache-as-promised' | ||
const cache = inMemoryCache() | ||
cache.use(distCache(redisFactory, namespace)) | ||
``` | ||
#### Parameters | ||
Parameters that must be provided upon creation: | ||
- redisFactory - `Function`. A function that returns an ioredis compatible redisClient. | ||
- namespace - `String`. Pub/sub-namespace used for distributed expiries | ||
#### Example | ||
```js | ||
import inMemoryCache, {distCache} from '@nrk/nodecache-as-promised' | ||
@@ -277,4 +256,24 @@ import Redis from 'ioredis' | ||
#### Persisting cache misses | ||
### persistentCache middleware | ||
Creating a new persistentCache middleware instance. The persistentCache middleware is extending the inMemoryCache instance by serializing and storing any new values recieved via workers in `.get` or in `.set`-calls to Redis. In addition it deletes values from Redis when the `.del` and `.clear`-methods are called. Cache values evicted by the LRU-cache are also deleted. On creation it will load and set initial cache values by doing a search for stored keys on the provided `keySpace` (may be disabled using the option `bootLoad: false` - so that loading may be done afterwards using the provided `.load`-method). It adds a couple of parameters to the `.debug`-method. | ||
```js | ||
import cache, {persistentCache} from '@nrk/nodecache-as-promised' | ||
const cache = inMemoryCache() | ||
cache.use(persistentCache(redisFactory, options)) | ||
``` | ||
#### Parameters | ||
Parameters that must be provided upon creation: | ||
- redisFactory - `Function`. A function that returns an ioredis compatible redisClient. | ||
#### options | ||
- doNotPersist - `RegExp`. Keys matching this regexp is not persisted to cache. Default `null` | ||
- keySpace - `String`. Prefix used when storing keys in redis. | ||
- grace - `Number`. Used to calculate TTL in redis (before auto removal), ie. object.TTL + grace. Default `86400000` (24h) | ||
- bootload - `Boolean`. Flag to choose if persisted cache is loaded from redis on middleware creation. Default `true` | ||
#### Example | ||
```js | ||
import inMemoryCache, {persistentCache} from '@nrk/nodecache-as-promised' | ||
@@ -298,3 +297,6 @@ import Redis from 'ioredis' | ||
#### Persisting cache misses __and__ distributed expire | ||
#### Combining middlewares | ||
Example in combining persistentCache __and__ distCache | ||
```js | ||
@@ -326,28 +328,2 @@ import inMemoryCache, {distCache, persistentCache} from '@nrk/nodecache-as-promised' | ||
## Advanced usage | ||
The application cache may be used in various ways, as a standalone inMemoryCache, a cache backed by Redis persistence and pub/sub expiry, or with an edge cache in front. Below is a table one way to configure different caches. | ||
|Type |Level 1 |Level 2 |Level 3 | | ||
|:--------------|:---------------------|:----------------------|:-------------------------| | ||
|Technology |Varnish, Nginx |`nodecache-as-promised`|Redis | | ||
|Lifespan |Short age (1s) |Medium age (>1s) |Long age | | ||
|Distribution |Singular |Singular/dist. expire |Shared | | ||
|Invalidation |TTL |TTL or on-demand expiry|TTL or on-demand deletions| | ||
|Stale support |stale-while-revalidate|stale-if-error |N/A | | ||
--- | ||
## Local development | ||
First clone the repo and install its dependencies: | ||
```bash | ||
git clone git@github.com:nrkno/nodecache-as-promised.git | ||
git checkout -b feature/my-changes | ||
cd nodecache-as-promised | ||
npm install && npm run build && npm run test | ||
``` | ||
## Creating your own middleware | ||
@@ -381,2 +357,15 @@ A middleware consists of three parts: | ||
--- | ||
## Local development | ||
First clone the repo and install its dependencies: | ||
```bash | ||
git clone git@github.com:nrkno/nodecache-as-promised.git | ||
git checkout -b feature/my-changes | ||
cd nodecache-as-promised | ||
npm install && npm run build && npm run test | ||
``` | ||
## Building and committing | ||
@@ -383,0 +372,0 @@ After having applied changes, remember to build and run/fix tests before pushing the changes upstream. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
622448
384