apollo-server-caching
Advanced tools
Comparing version 0.300.0-alpha.3 to 0.300.0-alpha.4
@@ -1,4 +0,5 @@ | ||
export { KeyValueCache, TestableKeyValueCache, KeyValueCacheSetOptions, } from './KeyValueCache'; | ||
export { KeyValueCache, KeyValueCacheSetOptions } from './KeyValueCache'; | ||
export { InMemoryLRUCache } from './InMemoryLRUCache'; | ||
export { PrefixingKeyValueCache } from './PrefixingKeyValueCache'; | ||
export { runKeyValueCacheTests } from './testsuite'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runKeyValueCacheTests = exports.PrefixingKeyValueCache = exports.InMemoryLRUCache = void 0; | ||
var InMemoryLRUCache_1 = require("./InMemoryLRUCache"); | ||
@@ -7,2 +8,4 @@ Object.defineProperty(exports, "InMemoryLRUCache", { enumerable: true, get: function () { return InMemoryLRUCache_1.InMemoryLRUCache; } }); | ||
Object.defineProperty(exports, "PrefixingKeyValueCache", { enumerable: true, get: function () { return PrefixingKeyValueCache_1.PrefixingKeyValueCache; } }); | ||
var testsuite_1 = require("./testsuite"); | ||
Object.defineProperty(exports, "runKeyValueCacheTests", { enumerable: true, get: function () { return testsuite_1.runKeyValueCacheTests; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
import { TestableKeyValueCache } from './KeyValueCache'; | ||
export declare class InMemoryLRUCache<V = string> implements TestableKeyValueCache<V> { | ||
import { KeyValueCache } from './KeyValueCache'; | ||
export declare class InMemoryLRUCache<V = string> implements KeyValueCache<V> { | ||
private store; | ||
@@ -4,0 +4,0 @@ constructor({ maxSize, sizeCalculator, onDispose, }?: { |
@@ -9,6 +9,2 @@ export interface KeyValueCacheSetOptions { | ||
} | ||
export interface TestableKeyValueCache<V = string> extends KeyValueCache<V> { | ||
flush?(): Promise<void>; | ||
close?(): Promise<void>; | ||
} | ||
//# sourceMappingURL=KeyValueCache.d.ts.map |
{ | ||
"name": "apollo-server-caching", | ||
"version": "0.300.0-alpha.3", | ||
"version": "0.300.0-alpha.4", | ||
"author": "Apollo <opensource@apollographql.com>", | ||
@@ -18,3 +18,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=12.0 <15.0" | ||
"node": ">=12.0" | ||
}, | ||
@@ -24,3 +24,3 @@ "dependencies": { | ||
}, | ||
"gitHead": "15b90eec22523e32e00d97a51b28cbfba3f39c78" | ||
"gitHead": "97073f33d30f2d682655eb0e4dce1d7d43721807" | ||
} |
# apollo-server-caching | ||
[![npm version](https://badge.fury.io/js/apollo-server-caching.svg)](https://badge.fury.io/js/apollo-server-caching) | ||
[![Build Status](https://circleci.com/gh/apollographql/apollo-server.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server) | ||
[![Build Status](https://circleci.com/gh/apollographql/apollo-server/tree/main.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server) | ||
@@ -25,3 +25,3 @@ ## Implementing your own Cache | ||
You can export and run a jest test suite from `apollo-server-caching` to test your implementation: | ||
`apollo-server-caching` exports a function that you can run within a test suite to validate your implementation. It throws on failure. If you want to test expiration, then mock out `Date` and `setTimeout` (probably with `@sinonjs/fake-timers`) and pass a `tick` can be called to advance the fake time. (If you don't pass `tick`, it won't test expiration.) Other than that, it has no dependencies and can work in any test system and shouldn't require any particular build configuration to use from jest. Here's an example of how to use it with jest: | ||
@@ -32,17 +32,23 @@ ```typescript | ||
import YourKeyValueCache from '../src/YourKeyValueCache'; | ||
import { testKeyValueCache } from 'apollo-server-caching'; | ||
testKeyValueCache(new MemcachedCache('localhost')); | ||
import { runKeyValueCacheTests } from 'apollo-server-caching'; | ||
import FakeTimers from '@sinonjs/fake-timers'; | ||
describe('YourKeyValueCache', () => { | ||
it('run apollo-server-caching test suite', async () => { | ||
const cache = new YourKeyValueCache(); | ||
const clock = FakeTimers.install(); | ||
try { | ||
await runKeyValueCacheTests(cache, (ms: number) => clock.tick(ms)); | ||
} finally { | ||
clock.uninstall(); | ||
await cache.close(); | ||
} | ||
}); | ||
}); | ||
``` | ||
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. | ||
For more details, consult the [source for `apollo-server-caching`](./src/testsuite.ts). | ||
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`](./src/__tests__/testsuite.ts). | ||
### Running tests | ||
Run tests with `jest --verbose` |
@@ -1,11 +0,15 @@ | ||
import { | ||
testKeyValueCache_Basics, | ||
testKeyValueCache_Expiration, | ||
} from '../../../apollo-server-caching/src/__tests__/testsuite'; | ||
import { InMemoryLRUCache } from '../InMemoryLRUCache'; | ||
import { runKeyValueCacheTests } from 'apollo-server-caching'; | ||
import FakeTimers from '@sinonjs/fake-timers'; | ||
describe('InMemoryLRUCache', () => { | ||
const cache = new InMemoryLRUCache(); | ||
testKeyValueCache_Basics(cache); | ||
testKeyValueCache_Expiration(cache); | ||
it('run apollo-server-caching test suite', async () => { | ||
const cache = new InMemoryLRUCache(); | ||
const clock = FakeTimers.install(); | ||
try { | ||
await runKeyValueCacheTests(cache, (ms: number) => clock.tick(ms)); | ||
} finally { | ||
clock.uninstall(); | ||
} | ||
}); | ||
}); |
{ | ||
"extends": "../../../../tsconfig.test.base", | ||
"include": ["**/*", "../../../../__mocks__"], | ||
"include": ["**/*"], | ||
"references": [ | ||
@@ -5,0 +5,0 @@ { "path": "../../" }, |
@@ -1,7 +0,4 @@ | ||
export { | ||
KeyValueCache, | ||
TestableKeyValueCache, | ||
KeyValueCacheSetOptions, | ||
} from './KeyValueCache'; | ||
export { KeyValueCache, KeyValueCacheSetOptions } from './KeyValueCache'; | ||
export { InMemoryLRUCache } from './InMemoryLRUCache'; | ||
export { PrefixingKeyValueCache } from './PrefixingKeyValueCache'; | ||
export { runKeyValueCacheTests } from './testsuite'; |
import LRUCache from 'lru-cache'; | ||
import { TestableKeyValueCache } from './KeyValueCache'; | ||
import { KeyValueCache } from './KeyValueCache'; | ||
@@ -14,6 +14,6 @@ function defaultLengthCalculation(item: any) { | ||
export class InMemoryLRUCache<V = string> implements TestableKeyValueCache<V> { | ||
export class InMemoryLRUCache<V = string> implements KeyValueCache<V> { | ||
private store: LRUCache<string, V>; | ||
// FIXME: Define reasonable default max size of the cache | ||
// TODO: Define reasonable default max size of the cache | ||
constructor({ | ||
@@ -20,0 +20,0 @@ maxSize = Infinity, |
@@ -15,10 +15,1 @@ /** Options for {@link KeyValueCache.set} */ | ||
} | ||
export interface TestableKeyValueCache<V = string> extends KeyValueCache<V> { | ||
// Drops all data from the cache. This should only be used by test suites --- | ||
// production code should never drop all data from an end user cache (and | ||
// notably, PrefixingKeyValueCache intentionally doesn't implement this). | ||
flush?(): Promise<void>; | ||
// Close connections associated with this cache. | ||
close?(): Promise<void>; | ||
} |
@@ -7,8 +7,8 @@ import { KeyValueCache, KeyValueCacheSetOptions } from './KeyValueCache'; | ||
// | ||
// Note that PrefixingKeyValueCache explicitly does not implement | ||
// TestableKeyValueCache, and notably does not implement the flush() | ||
// method. Most implementations of TestableKeyValueCache.flush() send a simple | ||
// command that wipes the entire backend cache system, which wouldn't support | ||
// "only wipe the part of the cache with this prefix", so trying to provide a | ||
// flush() method here could be confusingly dangerous. | ||
// Note that PrefixingKeyValueCache explicitly does not implement methods like | ||
// flush() that aren't part of KeyValueCache, even though most KeyValueCache | ||
// implementations also have a flush() method. Most implementations of flush() | ||
// send a simple command that wipes the entire backend cache system, which | ||
// wouldn't support "only wipe the part of the cache with this prefix", so | ||
// trying to provide a flush() method here could be confusingly dangerous. | ||
export class PrefixingKeyValueCache<V = string> implements KeyValueCache<V> { | ||
@@ -15,0 +15,0 @@ constructor(private wrapped: KeyValueCache<V>, private prefix: string) {} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
140622
32
318
53