Move in memory cache to apollo-server-caching

This commit is contained in:
Martijn Walraven 2018-06-20 11:06:24 +02:00
parent 3f3b0eb1f8
commit 0928d79efb
8 changed files with 11 additions and 17 deletions

View file

@ -65,7 +65,6 @@ To give resolvers access to data sources, you pass them as options to the `Apoll
const server = new ApolloServer({ const server = new ApolloServer({
typeDefs, typeDefs,
resolvers, resolvers,
cache: new InMemoryKeyValueCache({ maxSize: 1000000 }),
dataSources: () => { dataSources: () => {
return { return {
moviesAPI: new MoviesAPI(), moviesAPI: new MoviesAPI(),
@ -133,7 +132,7 @@ class PersonalizationAPI extends RESTDataSource {
## Using Memcached/Redis as a cache storage backend ## Using Memcached/Redis as a cache storage backend
By default, data sources use an in-memory LRU cache to store responses from REST calls. When running multiple server instances, you'll want to use a shared cache backend instead. That's why Apollo Server also includes support for using [Memcached](../../../packages/apollo-server-memcached) or [Redis](../../../packages/apollo-server-redis) as your backing store. You can specify which one to use by creating an instance and passing it into the Apollo Server constructor: By default, resource caching will use an in memory LRU cache. When running multiple server instances, you'll want to use a shared cache backend instead. That's why Apollo Server also includes support for using [Memcached](../../../packages/apollo-server-memcached) or [Redis](../../../packages/apollo-server-redis) as your backing store. You can specify which one to use by creating an instance and passing it into the Apollo Server constructor:
```js ```js
const { MemcachedCache } = require('apollo-server-memcached'); const { MemcachedCache } = require('apollo-server-memcached');

View file

@ -24,6 +24,7 @@
}, },
"dependencies": { "dependencies": {
"apollo-server-env": "^2.0.0-rc.0", "apollo-server-env": "^2.0.0-rc.0",
"apollo-server-caching": "^2.0.0-rc.0",
"http-cache-semantics": "^4.0.0", "http-cache-semantics": "^4.0.0",
"lru-cache": "^4.1.3" "lru-cache": "^4.1.3"
}, },

View file

@ -1,12 +1,9 @@
import CachePolicy from 'http-cache-semantics'; import CachePolicy from 'http-cache-semantics';
import { KeyValueCache } from './KeyValueCache'; import { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
import { InMemoryKeyValueCache } from './InMemoryKeyValueCache';
export class HTTPCache { export class HTTPCache {
constructor( constructor(private keyValueCache: KeyValueCache = new InMemoryLRUCache()) {}
private keyValueCache: KeyValueCache = new InMemoryKeyValueCache(),
) {}
async fetch(input: RequestInfo, init?: RequestInit): Promise<Response> { async fetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
const request = new Request(input, init); const request = new Request(input, init);

View file

@ -1,4 +0,0 @@
export interface KeyValueCache {
get(key: string): Promise<string | undefined>;
set(key: string, value: string): Promise<void>;
}

View file

@ -2,5 +2,4 @@ import 'apollo-server-env';
export { RESTDataSource } from './RESTDataSource'; export { RESTDataSource } from './RESTDataSource';
export { HTTPCache } from './HTTPCache'; export { HTTPCache } from './HTTPCache';
export { KeyValueCache } from './KeyValueCache'; export { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
export { InMemoryKeyValueCache } from './InMemoryKeyValueCache';

View file

@ -1,11 +1,11 @@
import LRU from 'lru-cache'; import LRU from 'lru-cache';
import { KeyValueCache } from './KeyValueCache'; import { KeyValueCache } from './KeyValueCache';
export class InMemoryKeyValueCache implements KeyValueCache { export class InMemoryLRUCache implements KeyValueCache {
private store: LRU.Cache<string, string>; private store: LRU.Cache<string, string>;
// FIXME: Define reasonable default max size of the cache // FIXME: Define reasonable default max size of the cache
constructor(maxSize: number = Infinity) { constructor({ maxSize = Infinity }: { maxSize?: number } = {}) {
this.store = LRU({ this.store = LRU({
max: maxSize, max: maxSize,
length: item => item.length, length: item => item.length,

View file

@ -1,2 +1,4 @@
export { KeyValueCache } from './KeyValueCache'; export { KeyValueCache } from './KeyValueCache';
export { testKeyValueCache } from './tests'; export { testKeyValueCache } from './tests';
export { InMemoryLRUCache } from './InMemoryLRUCache';

View file

@ -16,7 +16,7 @@ import {
} from 'graphql'; } from 'graphql';
import { GraphQLExtension } from 'graphql-extensions'; import { GraphQLExtension } from 'graphql-extensions';
import { EngineReportingAgent } from 'apollo-engine-reporting'; import { EngineReportingAgent } from 'apollo-engine-reporting';
import { InMemoryKeyValueCache } from 'apollo-datasource-rest'; import { InMemoryLRUCache } from 'apollo-datasource-rest';
import { import {
SubscriptionServer, SubscriptionServer,
@ -123,7 +123,7 @@ export class ApolloServerBase {
} }
if (!requestOptions.cache) { if (!requestOptions.cache) {
requestOptions.cache = new InMemoryKeyValueCache(); requestOptions.cache = new InMemoryLRUCache();
} }
this.requestOptions = requestOptions as GraphQLOptions; this.requestOptions = requestOptions as GraphQLOptions;