apollo-server/packages/apollo-server-caching
Jesse Rosenberger 2b50e149b0
caching: InMemoryLRUCache support for ttl, much like Redis/Memcached.
This also re-enables the Expiration tests for this library, though I think I
will continue to leave the testsuite decomposed into more granular methods
for readability and future growth.

Note that this doesn't implement the same default `300` second TTL as the
other Memcache and Redis implementations currently do (a very debatable
default we should re-consider in the future).
2018-12-18 10:55:38 +02:00
..
src caching: InMemoryLRUCache support for ttl, much like Redis/Memcached. 2018-12-18 10:55:38 +02:00
.gitignore Add support for Redis and Memcached with ttls (#1191) 2018-06-18 19:36:51 -07:00
.npmignore Add support for Redis and Memcached with ttls (#1191) 2018-06-18 19:36:51 -07:00
jest.config.js Replace {} with Object.create(null) in jest.config.js 2018-10-12 13:00:13 +02:00
package.json Publish 2018-11-26 20:43:29 +02:00
README.md tests: Change apollo-server-caching test-suite to support InMemoryLRUCache. 2018-12-18 10:55:36 +02:00
tsconfig.json Add root level tsconfig.json as entry point 2018-10-05 21:48:49 +02:00

apollo-server-caching

npm version Build Status

Implementing your own Cache

Internally, Apollo Server uses the KeyValueCache interface to provide a caching store for the Data Sources. An in-memory LRU cache is used by default, and we provide connectors for Memcached/Redis backends.

Built with extensibility in mind, you can also implement your own cache to use with Apollo Server, in a way that best suits your application needs. It needs to implement the following interface that can be exported from apollo-server-caching:

export interface KeyValueCache {
  get(key: string): Promise<string | undefined>;
  set(key: string, value: string, options?: { ttl?: number }): Promise<void>;
}

Testing cache implementations

Test helpers

You can export and run a jest test suite from apollo-server-caching to test your implementation:

// ../__tests__/YourKeyValueCache.test.ts

import YourKeyValueCache from '../src/YourKeyValueCache';
import { testKeyValueCache } from 'apollo-server-caching';
testKeyValueCache(new MemcachedCache('localhost'));

The default testKeyValueCache helper will run all key-value store tests on the specified store, including basic get and set functionality, along with time-based expunging rules.

Some key-value cache implementations may not be able to support the full suite of tests (for example, some tests might not be able to expire based on time). For those cases, there are more granular implementations which can be used:

  • testKeyValueCache_Basic
  • testKeyValueCache_Expiration

For more details, consult the source for apollo-server-caching.

Running tests

Run tests with jest --verbose