Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new KeyValueCache and friends from @apollo/utils.keyvaluecache #6522

Merged
merged 11 commits into from Jun 7, 2022
18 changes: 12 additions & 6 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Expand Up @@ -2267,16 +2267,20 @@ export function testApolloServer<AS extends ApolloServerBase>(

it('basic caching', async () => {
class TTLTestingCache implements KeyValueCache<string> {
private fakeTime = 0;
constructor(
private cache: Map<
string,
{ value: string; ttl: number | null }
{ value: string; ttl: number | null; fakeTimeOnSet: number }
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
> = new Map(),
) {}

async get(key: string) {
const entry = this.cache.get(key);
if (entry?.ttl && entry.ttl <= 0) {
if (
entry?.ttl &&
entry.ttl <= this.fakeTime - entry.fakeTimeOnSet
) {
await this.delete(key);
return undefined;
}
Expand All @@ -2288,17 +2292,19 @@ export function testApolloServer<AS extends ApolloServerBase>(
value: string,
{ ttl }: { ttl: number | null } = { ttl: null },
) {
this.cache.set(key, { value, ttl: ttl ? ttl * 1000 : null });
this.cache.set(key, {
value,
ttl: ttl ? ttl * 1000 : null,
fakeTimeOnSet: this.fakeTime,
});
}

async delete(key: string) {
this.cache.delete(key);
}

advanceTime(ms: number) {
[...this.cache.values()].forEach(
(value) => value.ttl && (value.ttl -= ms),
);
this.fakeTime += ms;
}
}
const typeDefs = gql`
Expand Down