Skip to content
Branch: master
Find file History
glasser apollo-caching: add PrefixingKeyValueCache, TestableKeyValueCache (#2433
)

apollo-server's caching uses the pattern of sharing a single KeyValueCache
object across multiple "features", using a prefix to ensure that the features'
data doesn't conflict. This change makes this pattern explicit rather than
implicit by introducing a PrefixingKeyValueCache class which handles the
prefixing for you.

In addition, it pulls the dangerous reset() operation out of KeyValueCache and
doesn't implement it in PrefixingKeyValueCache. Because reset is typically
implemented by sending a cache server a "drop all data" operation, and servers
may not implement "drop all data with a given prefix", we would not want actual
production code for a particular apollo-server cache-related feature to ever
call reset(). This PR moves that method (and close()) to a TestableKeyValueCache
interface, which is used by the test suite but not by the particular features
that need caches.
Latest commit 875944e Mar 12, 2019
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src apollo-caching: add PrefixingKeyValueCache, TestableKeyValueCache (#2433 Mar 12, 2019
.gitignore
.npmignore Add support for Redis and Memcached with ttls (#1191) Jun 19, 2018
README.md
jest.config.js
package.json Publish Feb 14, 2019
tsconfig.json Add root level tsconfig.json as entry point Oct 5, 2018

README.md

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

You can’t perform that action at this time.