-
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.
Implement simple
UnboundedCache
(#6535)
This introduces a simple Map-backed, unbounded, in-memory cache which implements TTLs. This lets us remove the dependency on keyv and @apollo/utils.keyvadapter for a thing that we're going to actively tell people not to use anyway.
- Loading branch information
1 parent
29bb2f7
commit 67d9036
Showing
7 changed files
with
75 additions
and
130 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
35 changes: 35 additions & 0 deletions
35
packages/apollo-server-core/src/__tests__/UnboundedCache.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,35 @@ | ||
import { UnboundedCache } from '../utils/UnboundedCache'; | ||
|
||
describe('UnboundedCache', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('basic get, set, delete', async () => { | ||
const cache = new UnboundedCache(); | ||
|
||
await cache.set('key', 'value'); | ||
expect(await cache.get('key')).toBe('value'); | ||
|
||
await cache.delete('key'); | ||
expect(await cache.get('key')).toBeUndefined(); | ||
}); | ||
|
||
it('get with ttl', async () => { | ||
const cache = new UnboundedCache(); | ||
|
||
// 1s, or 1000ms | ||
await cache.set('key', 'value', { ttl: 1 }); | ||
|
||
// check that it's there at 999ms | ||
jest.advanceTimersByTime(999); | ||
expect(await cache.get('key')).toBe('value'); | ||
|
||
// expire | ||
jest.advanceTimersByTime(1); | ||
expect(await cache.get('key')).toBeUndefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { KeyValueCache } from '@apollo/utils.keyvaluecache'; | ||
|
||
export class UnboundedCache<T = string> implements KeyValueCache<T> { | ||
constructor( | ||
private cache: Map< | ||
string, | ||
{ value: T; deadline: number | null } | ||
> = new Map(), | ||
) {} | ||
|
||
async get(key: string) { | ||
const entry = this.cache.get(key); | ||
if (!entry) return undefined; | ||
if (entry.deadline && entry.deadline <= Date.now()) { | ||
await this.delete(key); | ||
return undefined; | ||
} | ||
return entry.value; | ||
} | ||
|
||
async set( | ||
key: string, | ||
value: T, | ||
{ ttl }: { ttl: number | null } = { ttl: null }, | ||
) { | ||
this.cache.set(key, { | ||
value, | ||
deadline: ttl ? Date.now() + ttl * 1000 : null, | ||
}); | ||
} | ||
|
||
async delete(key: string) { | ||
this.cache.delete(key); | ||
} | ||
} |