-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
11 changed files
with
119 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/apollo-server-caching/src/PrefixingKeyValueCache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { KeyValueCache } from './KeyValueCache'; | ||
|
||
// PrefixingKeyValueCache wraps another cache and adds a prefix to all keys used | ||
// by all operations. This allows multiple features to share the same | ||
// underlying cache without conflicts. | ||
// | ||
// 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. | ||
export class PrefixingKeyValueCache<V = string> implements KeyValueCache<V> { | ||
constructor(private wrapped: KeyValueCache<V>, private prefix: string) {} | ||
|
||
get(key: string) { | ||
return this.wrapped.get(this.prefix + key); | ||
} | ||
set(key: string, value: V, options?: { ttl?: number }) { | ||
return this.wrapped.set(this.prefix + key, value, options); | ||
} | ||
delete(key: string) { | ||
return this.wrapped.delete(this.prefix + key); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/apollo-server-caching/src/__tests__/PrefixingKeyValueCache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { InMemoryLRUCache } from '../InMemoryLRUCache'; | ||
import { PrefixingKeyValueCache } from '../PrefixingKeyValueCache'; | ||
|
||
describe('PrefixingKeyValueCache', () => { | ||
it('prefixes', async () => { | ||
const inner = new InMemoryLRUCache(); | ||
const prefixing = new PrefixingKeyValueCache(inner, 'prefix:'); | ||
await prefixing.set('foo', 'bar'); | ||
expect(await prefixing.get('foo')).toBe('bar'); | ||
expect(await inner.get('prefix:foo')).toBe('bar'); | ||
await prefixing.delete('foo'); | ||
expect(await prefixing.get('foo')).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { KeyValueCache } from './KeyValueCache'; | ||
export { KeyValueCache, TestableKeyValueCache } from './KeyValueCache'; | ||
export { InMemoryLRUCache } from './InMemoryLRUCache'; | ||
export { PrefixingKeyValueCache } from './PrefixingKeyValueCache'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters