mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
Move in memory cache to apollo-server-caching
This commit is contained in:
parent
3f3b0eb1f8
commit
0928d79efb
8 changed files with 11 additions and 17 deletions
|
@ -65,7 +65,6 @@ To give resolvers access to data sources, you pass them as options to the `Apoll
|
|||
const server = new ApolloServer({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
cache: new InMemoryKeyValueCache({ maxSize: 1000000 }),
|
||||
dataSources: () => {
|
||||
return {
|
||||
moviesAPI: new MoviesAPI(),
|
||||
|
@ -133,7 +132,7 @@ class PersonalizationAPI extends RESTDataSource {
|
|||
|
||||
## 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
|
||||
const { MemcachedCache } = require('apollo-server-memcached');
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"apollo-server-env": "^2.0.0-rc.0",
|
||||
"apollo-server-caching": "^2.0.0-rc.0",
|
||||
"http-cache-semantics": "^4.0.0",
|
||||
"lru-cache": "^4.1.3"
|
||||
},
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
import CachePolicy from 'http-cache-semantics';
|
||||
|
||||
import { KeyValueCache } from './KeyValueCache';
|
||||
import { InMemoryKeyValueCache } from './InMemoryKeyValueCache';
|
||||
import { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
|
||||
|
||||
export class HTTPCache {
|
||||
constructor(
|
||||
private keyValueCache: KeyValueCache = new InMemoryKeyValueCache(),
|
||||
) {}
|
||||
constructor(private keyValueCache: KeyValueCache = new InMemoryLRUCache()) {}
|
||||
|
||||
async fetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
|
||||
const request = new Request(input, init);
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
export interface KeyValueCache {
|
||||
get(key: string): Promise<string | undefined>;
|
||||
set(key: string, value: string): Promise<void>;
|
||||
}
|
|
@ -2,5 +2,4 @@ import 'apollo-server-env';
|
|||
|
||||
export { RESTDataSource } from './RESTDataSource';
|
||||
export { HTTPCache } from './HTTPCache';
|
||||
export { KeyValueCache } from './KeyValueCache';
|
||||
export { InMemoryKeyValueCache } from './InMemoryKeyValueCache';
|
||||
export { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import LRU from 'lru-cache';
|
||||
import { KeyValueCache } from './KeyValueCache';
|
||||
|
||||
export class InMemoryKeyValueCache implements KeyValueCache {
|
||||
export class InMemoryLRUCache implements KeyValueCache {
|
||||
private store: LRU.Cache<string, string>;
|
||||
|
||||
// FIXME: Define reasonable default max size of the cache
|
||||
constructor(maxSize: number = Infinity) {
|
||||
constructor({ maxSize = Infinity }: { maxSize?: number } = {}) {
|
||||
this.store = LRU({
|
||||
max: maxSize,
|
||||
length: item => item.length,
|
|
@ -1,2 +1,4 @@
|
|||
export { KeyValueCache } from './KeyValueCache';
|
||||
export { testKeyValueCache } from './tests';
|
||||
|
||||
export { InMemoryLRUCache } from './InMemoryLRUCache';
|
||||
|
|
|
@ -16,7 +16,7 @@ import {
|
|||
} from 'graphql';
|
||||
import { GraphQLExtension } from 'graphql-extensions';
|
||||
import { EngineReportingAgent } from 'apollo-engine-reporting';
|
||||
import { InMemoryKeyValueCache } from 'apollo-datasource-rest';
|
||||
import { InMemoryLRUCache } from 'apollo-datasource-rest';
|
||||
|
||||
import {
|
||||
SubscriptionServer,
|
||||
|
@ -123,7 +123,7 @@ export class ApolloServerBase {
|
|||
}
|
||||
|
||||
if (!requestOptions.cache) {
|
||||
requestOptions.cache = new InMemoryKeyValueCache();
|
||||
requestOptions.cache = new InMemoryLRUCache();
|
||||
}
|
||||
|
||||
this.requestOptions = requestOptions as GraphQLOptions;
|
||||
|
|
Loading…
Add table
Reference in a new issue