@envelop/response-cache-redis 0.5.0-alpha-26c4ae2.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
## `@envelop/response-cache-redis`
|
|
2
|
+
|
|
3
|
+
- Supports redis cache for `@envelop/response-cache` plugin
|
|
4
|
+
- Suitable for serveless deployments where the LRU In-Memory Cache is not possible
|
|
5
|
+
|
|
6
|
+
[Check out the GraphQL Response Cache Guide for more information](https://envelop.dev/docs/guides/adding-a-graphql-response-cache)
|
|
7
|
+
|
|
8
|
+
## Getting Started
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
yarn add @envelop/response-cache
|
|
12
|
+
yarn add @envelop/response-cache-redis
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage Example
|
|
16
|
+
|
|
17
|
+
In order to use the Redis cache, you need to:
|
|
18
|
+
|
|
19
|
+
- Create a Redis database
|
|
20
|
+
- Collect the connection settings (or its connection string), e.g., `host`, `port`, `username`, `password`, `tls`, etc.
|
|
21
|
+
- Create and configure a [Redis client](https://github.com/luin/ioredis) with your [connection settings](https://github.com/luin/ioredis/blob/master/API.md#Redis) and any [additional options](https://github.com/luin/ioredis/blob/master/API.md#new_Redis_new)
|
|
22
|
+
- Create an instance of the Redis Cache and set to the `useResponseCache` plugin options
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
|
|
26
|
+
import { envelop } from '@envelop/core';
|
|
27
|
+
import { useResponseCache } from '@envelop/response-cache';
|
|
28
|
+
import { createRedisCache } from '@envelop/response-cache-redis';
|
|
29
|
+
|
|
30
|
+
import Redis from 'ioredis';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* For additional Redis options to create the ioredis client
|
|
34
|
+
* @see: https://github.com/luin/ioredis/blob/master/API.md#new_Redis_new
|
|
35
|
+
*
|
|
36
|
+
**/
|
|
37
|
+
const redis = new Redis({
|
|
38
|
+
host: 'my-redis-db.example.com',
|
|
39
|
+
port: '30652',
|
|
40
|
+
password: '1234567890',
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// or, you can also specify connection options as a redis:// URL or rediss:// URL when using TLS encryption
|
|
44
|
+
const redis = new Redis(("rediss://:1234567890@my-redis-db.example.com':30652");
|
|
45
|
+
|
|
46
|
+
const cache = createRedisCache({ redis });
|
|
47
|
+
|
|
48
|
+
const getEnveloped = envelop({
|
|
49
|
+
plugins: [
|
|
50
|
+
// ... other plugins ...
|
|
51
|
+
useResponseCache({ cache }),
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Invalidate Cache based on custom logic
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { envelop } from '@envelop/core';
|
|
60
|
+
import { useResponseCache } from '@envelop/response-cache';
|
|
61
|
+
import { createRedisCache } from '@envelop/response-cache-redis';
|
|
62
|
+
|
|
63
|
+
import { emitter } from './eventEmitter';
|
|
64
|
+
|
|
65
|
+
// we create our cache instance, which allows calling all methods on it
|
|
66
|
+
const redis = new Redis(("rediss://:1234567890@my-redis-db.example.com':30652");
|
|
67
|
+
|
|
68
|
+
const cache = createRedisCache({ redis });
|
|
69
|
+
|
|
70
|
+
const getEnveloped = envelop({
|
|
71
|
+
plugins: [
|
|
72
|
+
// ... other plugins ...
|
|
73
|
+
useResponseCache({
|
|
74
|
+
ttl: 2000,
|
|
75
|
+
// we pass the cache instance to the request.
|
|
76
|
+
cache,
|
|
77
|
+
}),
|
|
78
|
+
],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
emitter.on('invalidate', resource => {
|
|
82
|
+
cache.invalidate([
|
|
83
|
+
{
|
|
84
|
+
typename: resource.type,
|
|
85
|
+
id: resource.id,
|
|
86
|
+
},
|
|
87
|
+
]);
|
|
88
|
+
});
|
|
89
|
+
```
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/response-cache-redis",
|
|
3
|
-
"version": "0.5.0
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@envelop/response-cache": "2.4.0
|
|
7
|
+
"@envelop/response-cache": "^2.4.0",
|
|
8
8
|
"ioredis": "^4.27.9"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|