Bakhtina Sofya b84cb877be 1st comm | 3 nedēļas atpakaļ | |
---|---|---|
.. | ||
dist | 3 nedēļas atpakaļ | |
node_modules | 3 nedēļas atpakaļ | |
src | 3 nedēļas atpakaļ | |
LICENSE | 3 nedēļas atpakaļ | |
README.md | 3 nedēļas atpakaļ | |
package.json | 3 nedēļas atpakaļ |
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>;
}
The
ttl
value for theset
method'soptions
is specified in seconds.
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
.
Run tests with jest --verbose