-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest-datasource): add apollo-server-cache-redis-cluster package
- Loading branch information
Showing
9 changed files
with
1,408 additions
and
1,163 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
* | ||
!src/**/* | ||
!dist/**/* | ||
dist/**/*.test.* | ||
!package.json | ||
!README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
## RedisClusterCache | ||
|
||
[![npm version](https://badge.fury.io/js/apollo-server-cache-redis-cluster.svg)](https://badge.fury.io/js/apollo-server-cache-redis-cluster) | ||
[![Build Status](https://circleci.com/gh/apollographql/apollo-server.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server) | ||
|
||
This package exports an implementation of `KeyValueCache` that allows using Redis as a backing store for resource caching in [Data Sources](https://www.apollographql.com/docs/apollo-server/v2/features/data-sources.html). | ||
|
||
## Usage | ||
|
||
### Single instance or sentinels | ||
|
||
```js | ||
const { RedisCache } = require('apollo-server-cache-redis-cluster'); | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
cache: new RedisClusterCache({ | ||
host: 'redis-server', | ||
// Options are passed through to the Redis client | ||
}), | ||
dataSources: () => ({ | ||
moviesAPI: new MoviesAPI(), | ||
}), | ||
}); | ||
``` | ||
|
||
### Cluster | ||
|
||
```js | ||
const { RedisClusterCache } = require('apollo-server-cache-redis-cluster'); | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
cache: new RedisClusterCache( | ||
[{ | ||
host: 'redis-server', | ||
// Options are passed through to the Redis cluster client | ||
}], | ||
{ | ||
// Cluster options are passed through to the Redis cluster client | ||
} | ||
), | ||
dataSources: () => ({ | ||
moviesAPI: new MoviesAPI(), | ||
}), | ||
}); | ||
``` | ||
|
||
For documentation of the options you can pass to the underlying redis client, look [here](https://github.com/luin/ioredis). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "apollo-server-cache-redis-cluster", | ||
"version": "0.0.1", | ||
"author": "[email protected]", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-cache-redis-cluster" | ||
}, | ||
"homepage": "https://github.com/apollographql/apollo-server#readme", | ||
"bugs": { | ||
"url": "https://github.com/apollographql/apollo-server/issues" | ||
}, | ||
"scripts": { | ||
"clean": "rm -rf dist", | ||
"compile": "tsc", | ||
"prepare": "npm run clean && npm run compile" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"dependencies": { | ||
"apollo-server-caching": "file:../apollo-server-caching", | ||
"apollo-server-env": "file:../apollo-server-env", | ||
"dataloader": "^1.4.0", | ||
"ioredis": "^4.0.0" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/apollo-server-cache-redis-cluster/src/__tests__/Redis.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// use mock implementations for underlying databases | ||
jest.mock('ioredis', () => require('ioredis-mock')); | ||
jest.useFakeTimers(); // mocks out setTimeout that is used in ioredis-mock | ||
|
||
import { RedisCache } from '../index'; | ||
import { testKeyValueCache } from '../../../apollo-server-caching/src/__tests__/testsuite'; | ||
|
||
testKeyValueCache(new RedisCache({ host: 'localhost' })); |
10 changes: 10 additions & 0 deletions
10
packages/apollo-server-cache-redis-cluster/src/__tests__/RedisCluster.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// use mock implementations for underlying databases | ||
jest.mock('ioredis', () => require('ioredis-mock')); | ||
jest.useFakeTimers(); // mocks out setTimeout that is used in ioredis-mock | ||
|
||
import { RedisClusterCache } from '../index'; | ||
import { testKeyValueCache } from '../../../apollo-server-caching/src/__tests__/testsuite'; | ||
|
||
testKeyValueCache( | ||
new RedisClusterCache([{ host: 'localhost' }], { scaleReads: 'master' }), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { KeyValueCache } from 'apollo-server-caching'; | ||
import Redis, { ClusterOptions, ClusterNode, RedisOptions } from 'ioredis'; | ||
import { promisify } from 'util'; | ||
import DataLoader from 'dataloader'; | ||
|
||
export class RedisCache implements KeyValueCache { | ||
protected client: any; | ||
readonly defaultSetOptions = { | ||
ttl: 300, | ||
}; | ||
|
||
protected loader: DataLoader<string, string>; | ||
|
||
constructor(options?: RedisOptions) { | ||
const client = new Redis(options); | ||
client.mget = promisify(client.mget).bind(client); | ||
client.set = promisify(client.set).bind(client); | ||
client.flushdb = promisify(client.flushdb).bind(client); | ||
client.quit = promisify(client.quit).bind(client); | ||
|
||
this.client = client; | ||
|
||
this.loader = new DataLoader(keys => this.client.mget(keys), { | ||
cache: false, | ||
}); | ||
} | ||
|
||
async set( | ||
key: string, | ||
data: string, | ||
options?: { ttl?: number }, | ||
): Promise<void> { | ||
const { ttl } = Object.assign({}, this.defaultSetOptions, options); | ||
await this.client.set(key, data, 'EX', ttl); | ||
} | ||
|
||
async get(key: string): Promise<string | undefined> { | ||
const reply = await this.loader.load(key); | ||
// reply is null if key is not found | ||
if (reply !== null) { | ||
return reply; | ||
} | ||
return; | ||
} | ||
|
||
async flush(): Promise<void> { | ||
await this.client.flushdb(); | ||
} | ||
|
||
async close(): Promise<void> { | ||
await this.client.quit(); | ||
return; | ||
} | ||
} | ||
|
||
export class RedisClusterCache extends RedisCache { | ||
constructor(nodes: ClusterNode[], options?: ClusterOptions) { | ||
super(); | ||
const client = new Redis.Cluster(nodes, options); | ||
|
||
client.mget = promisify(client.mget).bind(client); | ||
client.set = promisify(client.set).bind(client); | ||
client.flushdb = promisify(client.flushdb).bind(client); | ||
client.quit = promisify(client.quit).bind(client); | ||
|
||
this.client = client; | ||
|
||
this.loader = new DataLoader(keys => this.client.mget(keys), { | ||
cache: false, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["**/__tests__", "**/__mocks__"] | ||
} |