Skip to content

Commit

Permalink
rebase from main
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Dec 12, 2022
1 parent 459673a commit fb47e92
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/plugins/response-cache-upstash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@envelop/response-cache": "^2.3.2",
"@upstash/redis": "1.3.0"
"@upstash/redis": "^1.18.0"
},
"peerDependencies": {},
"buildOptions": {
Expand Down
32 changes: 16 additions & 16 deletions packages/plugins/response-cache-upstash/src/upstash-cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Redis } from '@upstash/redis';
import { Redis } from '@upstash/redis';
import type { Cache } from '@envelop/response-cache';
import { Maybe, PromiseOrValue } from '@envelop/types';
import type { ExecutionResult } from 'graphql';
import 'isomorphic-fetch';

export type BuildRedisEntityId = (typename: string, id: number | string) => string;
export type BuildRedisOperationResultCacheKey = (responseId: string) => string;
Expand All @@ -28,7 +27,8 @@ export const createUpstashCache = (params: RedisCacheParameter): Cache => {
const store = params.redis;

const buildRedisEntityId = params?.buildRedisEntityId ?? defaultBuildRedisEntityId;
const buildRedisOperationResultCacheKey = params?.buildRedisOperationResultCacheKey ?? defaultBuildRedisOperationResultCacheKey;
const buildRedisOperationResultCacheKey =
params?.buildRedisOperationResultCacheKey ?? defaultBuildRedisOperationResultCacheKey;

async function buildEntityInvalidationsKeys(entity: string): Promise<string[]> {
const keysToInvalidate: string[] = [entity];
Expand Down Expand Up @@ -64,44 +64,44 @@ export const createUpstashCache = (params: RedisCacheParameter): Cache => {

return {
async set(responseId, result, collectedEntities, ttl) {
const pipeline = store.pipeline();
const tx = store.multi();

if (ttl === Infinity) {
pipeline.set(responseId, result);
tx.set(responseId, result);
} else {
// set the ttl in milliseconds
pipeline.set(responseId, result, { px: ttl });
tx.set(responseId, result, { px: ttl });
}

const responseKey = buildRedisOperationResultCacheKey(responseId);

for (const { typename, id } of collectedEntities) {
// Adds a key for the typename => response
pipeline.sadd(typename, responseId);
tx.sadd(typename, responseId);
// Adds a key for the operation => typename
pipeline.sadd(responseKey, typename);
tx.sadd(responseKey, typename);

if (id) {
const entityId = buildRedisEntityId(typename, id);
// Adds a key for the typename:id => response
pipeline.sadd(entityId, responseId);
tx.sadd(entityId, responseId);
// Adds a key for the operation => typename:id
pipeline.sadd(responseKey, entityId);
tx.sadd(responseKey, entityId);
}
}

await pipeline.exec();
await tx.exec();
},
async get(responseId) {
const result = await store.get<PromiseOrValue<Maybe<ExecutionResult>>>(responseId);

return result;
return store.get(responseId);
},
async invalidate(entitiesToRemove) {
const invalidationKeys: string[][] = [];

for (const { typename, id } of entitiesToRemove) {
invalidationKeys.push(await buildEntityInvalidationsKeys(id != null ? buildRedisEntityId(typename, id) : typename));
invalidationKeys.push(
await buildEntityInvalidationsKeys(id != null ? buildRedisEntityId(typename, id) : typename)
);
}

const keys = invalidationKeys.flat();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { createTestkit } from '@envelop/testing';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { Redis } from '@upstash/redis';
import 'isomorphic-fetch';

import { createUpstashCache, defaultBuildRedisEntityId, defaultBuildRedisOperationResultCacheKey } from '../src';
import { useResponseCache } from '@envelop/response-cache';

describe('useResponseCache with Upstash cache', () => {
describe('useResponseCache with Redis cache', () => {
const redis = Redis.fromEnv();
const cache = createUpstashCache({ redis });

Expand Down Expand Up @@ -72,7 +70,7 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache })], schema);
const testInstance = createTestkit([useResponseCache({ session: () => null, cache })], schema);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -150,7 +148,7 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache })], schema);
const testInstance = createTestkit([useResponseCache({ session: () => null, cache })], schema);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -245,7 +243,7 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache })], schema);
const testInstance = createTestkit([useResponseCache({ session: () => null, cache })], schema);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -379,7 +377,7 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache })], schema);
const testInstance = createTestkit([useResponseCache({ session: () => null, cache })], schema);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -492,7 +490,10 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache, includeExtensionMetadata: true })], schema);
const testInstance = createTestkit(
[useResponseCache({ session: () => null, cache, includeExtensionMetadata: true })],
schema
);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -595,7 +596,10 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache, includeExtensionMetadata: true })], schema);
const testInstance = createTestkit(
[useResponseCache({ session: () => null, cache, includeExtensionMetadata: true })],
schema
);

const result = await testInstance.execute(
/* GraphQL */ `
Expand Down Expand Up @@ -672,7 +676,7 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache })], schema);
const testInstance = createTestkit([useResponseCache({ session: () => null, cache })], schema);

const query = /* GraphQL */ `
query test($limit: Int!) {
Expand Down Expand Up @@ -755,7 +759,7 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache, ttl: 100 })], schema);
const testInstance = createTestkit([useResponseCache({ session: () => null, cache, ttl: 100 })], schema);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -941,7 +945,10 @@ describe('useResponseCache with Upstash cache', () => {
},
});

const testInstance = createTestkit([useResponseCache({ cache, ignoredTypes: ['Comment'] })], schema);
const testInstance = createTestkit(
[useResponseCache({ session: () => null, cache, ignoredTypes: ['Comment'] })],
schema
);

const query = /* GraphQL */ `
query test {
Expand Down Expand Up @@ -1030,6 +1037,7 @@ describe('useResponseCache with Upstash cache', () => {
const testInstance = createTestkit(
[
useResponseCache({
session: () => null,
cache,
ttl: 500,
ttlPerType: {
Expand Down Expand Up @@ -1120,6 +1128,7 @@ describe('useResponseCache with Upstash cache', () => {
const testInstance = createTestkit(
[
useResponseCache({
session: () => null,
cache,
ttl: 500,
ttlPerSchemaCoordinate: {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3638,10 +3638,10 @@
resolved "https://registry.yarnpkg.com/@tyriar/fibonacci-heap/-/fibonacci-heap-2.0.9.tgz#df3dcbdb1b9182168601f6318366157ee16666e9"
integrity sha512-bYuSNomfn4hu2tPiDN+JZtnzCpSpbJ/PNeulmocDy3xN2X5OkJL65zo6rPZp65cPPhLF9vfT/dgE+RtFRCSxOA==

"@upstash/redis@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@upstash/redis/-/redis-1.3.0.tgz#702350d5f0650f17eeeaeb409a162d3d8238a6b8"
integrity sha512-mxoPic6eQ6nibOUcSrzaG75BWjrgGH46rVBJLxWicZvAJFHJIC95JMxm/OAh6xPDGCsA8Ilkn8fZ/N6kHeuDng==
"@upstash/redis@^1.18.0":
version "1.18.1"
resolved "https://registry.yarnpkg.com/@upstash/redis/-/redis-1.18.1.tgz#c586b267869dfee9c843ad04c7b13284f7337fdc"
integrity sha512-wk0KTRBW1VENdkJY62Oh0RZWSzrXOHQh6nvVs5fes/zYZzB9byUDN7oB2uylh+YjPHtAxbdrjZ68MA6g2mRQow==
dependencies:
isomorphic-fetch "^3.0.0"

Expand Down

0 comments on commit fb47e92

Please sign in to comment.