drizzle-orm 0.40.0-bbf78ae → 0.40.0-ca1033e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache/upstash/cache.cjs +42 -26
- package/cache/upstash/cache.cjs.map +1 -1
- package/cache/upstash/cache.d.cts +2 -2
- package/cache/upstash/cache.d.ts +2 -2
- package/cache/upstash/cache.js +42 -26
- package/cache/upstash/cache.js.map +1 -1
- package/package.json +1 -1
- package/version.cjs +1 -1
- package/version.d.cts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
package/cache/upstash/cache.cjs
CHANGED
|
@@ -47,39 +47,55 @@ class UpstashCache extends import_core.Cache {
|
|
|
47
47
|
keepTtl: config.keepTtl
|
|
48
48
|
} : void 0;
|
|
49
49
|
}
|
|
50
|
-
async get(key,
|
|
51
|
-
const
|
|
52
|
-
|
|
50
|
+
async get(key, tables, isTag = false) {
|
|
51
|
+
const compositeKey = tables.sort().join(",");
|
|
52
|
+
if (isTag) {
|
|
53
|
+
const tagCompositeKey = await this.redis.hget("tagsMap", key);
|
|
54
|
+
if (tagCompositeKey) {
|
|
55
|
+
return await this.redis.hget(tagCompositeKey, key);
|
|
56
|
+
}
|
|
57
|
+
return void 0;
|
|
58
|
+
}
|
|
59
|
+
return (await this.redis.hget(compositeKey, key)) ?? void 0;
|
|
53
60
|
}
|
|
54
|
-
async put(key, response, tables, isTag,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
await this.redis.
|
|
61
|
+
async put(key, response, tables, isTag = false, _config) {
|
|
62
|
+
if (isTag) {
|
|
63
|
+
const compositeKey = tables.sort().join(",");
|
|
64
|
+
await this.redis.hset(compositeKey, { [key]: response });
|
|
65
|
+
await this.redis.hset("tagsMap", { [key]: compositeKey });
|
|
66
|
+
for (const table of tables) {
|
|
67
|
+
await this.redis.sadd(`prefix_${table}`, compositeKey);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
const compositeKey = tables.sort().join(",");
|
|
71
|
+
await this.redis.hset(compositeKey, { [key]: response });
|
|
58
72
|
}
|
|
59
73
|
}
|
|
60
74
|
async onMutate(params) {
|
|
61
|
-
const
|
|
62
|
-
const
|
|
75
|
+
const tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];
|
|
76
|
+
const tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];
|
|
77
|
+
const mappedTables = tables.map((table) => (0, import_entity.is)(table, import__.Table) ? table[import__.OriginalName] : table);
|
|
78
|
+
const prefixedMappedTables = tables.map((table) => `prefix_${table}`);
|
|
63
79
|
const keysToDelete = /* @__PURE__ */ new Set();
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const pipeline = this.redis.pipeline();
|
|
72
|
-
for (const tag of tagsArray) {
|
|
73
|
-
pipeline.del(tag);
|
|
80
|
+
if (tables.length > 0) {
|
|
81
|
+
const compositeKeys = await this.redis.sunion(...prefixedMappedTables);
|
|
82
|
+
for (const composite of compositeKeys) {
|
|
83
|
+
const keys = await this.redis.hkeys(composite);
|
|
84
|
+
for (const key of keys)
|
|
85
|
+
keysToDelete.add(key);
|
|
86
|
+
await this.redis.del(composite);
|
|
74
87
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
await this.redis.del(...prefixedMappedTables, ...mappedTables);
|
|
89
|
+
}
|
|
90
|
+
for (const tag of tags) {
|
|
91
|
+
const compositeKey = await this.redis.hget("tagsMap", tag);
|
|
92
|
+
if (compositeKey) {
|
|
93
|
+
await this.redis.hdel(compositeKey, tag);
|
|
94
|
+
await this.redis.hdel("tagsMap", tag);
|
|
81
95
|
}
|
|
82
|
-
|
|
96
|
+
}
|
|
97
|
+
if (keysToDelete.size > 0) {
|
|
98
|
+
await this.redis.del(...keysToDelete);
|
|
83
99
|
}
|
|
84
100
|
}
|
|
85
101
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import type { SetCommandOptions } from '@upstash/redis';\nimport { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\n\tprivate internalConfig?: SetCommandOptions;\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig) {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tex: config.ex,\n\t\t\t\texat: config.exat,\n\t\t\t\tpx: config.px,\n\t\t\t\tpxat: config.pxat,\n\t\t\t\tkeepTtl: config.keepTtl,\n\t\t\t} as SetCommandOptions\n\t\t\t: undefined;\n\t}\n\n\toverride async get(key: string,
|
|
1
|
+
{"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import type { SetCommandOptions } from '@upstash/redis';\nimport { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\n\tprivate internalConfig?: SetCommandOptions;\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig) {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tex: config.ex,\n\t\t\t\texat: config.exat,\n\t\t\t\tpx: config.px,\n\t\t\t\tpxat: config.pxat,\n\t\t\t\tkeepTtl: config.keepTtl,\n\t\t\t} as SetCommandOptions\n\t\t\t: undefined;\n\t}\n\n\toverride async get(key: string, tables: string[], isTag: boolean = false): Promise<any[] | undefined> {\n\t\tconst compositeKey = tables.sort().join(','); // Generate the composite key for the query\n\n\t\tif (isTag) {\n\t\t\t// Handle cache lookup for tags\n\t\t\tconst tagCompositeKey = await this.redis.hget<string>('tagsMap', key); // Retrieve composite key associated with the tag\n\t\t\tif (tagCompositeKey) {\n\t\t\t\treturn await this.redis.hget(tagCompositeKey, key) as any[]; // Retrieve the cached result for the tag\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\t// Normal cache lookup for the composite key\n\t\treturn await this.redis.hget(compositeKey, key) ?? undefined; // Retrieve result for normal query\n\t}\n\n\toverride async put(\n\t\tkey: string,\n\t\tresponse: any,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\t_config?: CacheConfig,\n\t): Promise<void> {\n\t\tif (isTag) {\n\t\t\t// When it's a tag, store the response in the composite key\n\t\t\tconst compositeKey = tables.sort().join(',');\n\t\t\tawait this.redis.hset(compositeKey, { [key]: response }); // Store the result with the tag under the composite key\n\t\t\tawait this.redis.hset('tagsMap', { [key]: compositeKey }); // Store the tag and its composite key in the map\n\t\t\tfor (const table of tables) {\n\t\t\t\tawait this.redis.sadd(`prefix_${table}`, compositeKey);\n\t\t\t}\n\t\t} else {\n\t\t\t// Normal cache store\n\t\t\tconst compositeKey = tables.sort().join(',');\n\t\t\tawait this.redis.hset(compositeKey, { [key]: response }); // Store the result with the composite key\n\t\t}\n\t}\n\n\toverride async onMutate(params: MutationOption) {\n\t\tconst tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];\n\t\tconst tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];\n\t\tconst mappedTables = tables.map((table) => is(table, Table) ? table[OriginalName] : table as string);\n\t\tconst prefixedMappedTables = tables.map((table) => `prefix_${table}`);\n\n\t\tconst keysToDelete = new Set<string>();\n\n\t\t// Invalidate by table\n\t\tif (tables.length > 0) {\n\t\t\t// @ts-expect-error\n\t\t\tconst compositeKeys: string[] = await this.redis.sunion(...prefixedMappedTables);\n\t\t\tfor (const composite of compositeKeys) {\n\t\t\t\tconst keys = await this.redis.hkeys(composite);\n\t\t\t\tfor (const key of keys) keysToDelete.add(key);\n\t\t\t\tawait this.redis.del(composite); // Remove composite entry\n\t\t\t}\n\t\t\tawait this.redis.del(...prefixedMappedTables, ...mappedTables); // Remove table mappings\n\t\t}\n\n\t\t// Invalidate by tag (WITHOUT invalidating the entire table)\n\t\tfor (const tag of tags) {\n\t\t\tconst compositeKey = await this.redis.hget<string>('tagsMap', tag);\n\t\t\tif (compositeKey) {\n\t\t\t\tawait this.redis.hdel(compositeKey, tag); // Only remove the tag-related entry\n\t\t\t\tawait this.redis.hdel('tagsMap', tag); // Remove tag reference\n\t\t\t}\n\t\t}\n\n\t\t// Delete affected cache entries\n\t\tif (keysToDelete.size > 0) {\n\t\t\tawait this.redis.del(...keysToDelete);\n\t\t}\n\t}\n}\n\nexport function upstashCache(\n\t{ url, token, config, global = false }: { url: string; token: string; config?: CacheConfig; global?: boolean },\n): UpstashCache {\n\tconst redis = new Redis({\n\t\turl,\n\t\ttoken,\n\t});\n\n\treturn new UpstashCache(redis, config, global);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAsB;AAEtB,kBAAsB;AACtB,oBAA+B;AAC/B,eAAoC;AAG7B,MAAM,qBAAqB,kBAAM;AAAA,EAKvC,YAAmB,OAAc,QAAgC,aAAuB;AACvF,UAAM;AADY;AAA8C;AAE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[]}
|
|
@@ -11,8 +11,8 @@ export declare class UpstashCache extends Cache {
|
|
|
11
11
|
constructor(redis: Redis, config?: CacheConfig, useGlobally?: boolean | undefined);
|
|
12
12
|
strategy(): "all" | "explicit";
|
|
13
13
|
private toInternalConfig;
|
|
14
|
-
get(key: string,
|
|
15
|
-
put(key: string, response: any, tables: string[], isTag
|
|
14
|
+
get(key: string, tables: string[], isTag?: boolean): Promise<any[] | undefined>;
|
|
15
|
+
put(key: string, response: any, tables: string[], isTag?: boolean, _config?: CacheConfig): Promise<void>;
|
|
16
16
|
onMutate(params: MutationOption): Promise<void>;
|
|
17
17
|
}
|
|
18
18
|
export declare function upstashCache({ url, token, config, global }: {
|
package/cache/upstash/cache.d.ts
CHANGED
|
@@ -11,8 +11,8 @@ export declare class UpstashCache extends Cache {
|
|
|
11
11
|
constructor(redis: Redis, config?: CacheConfig, useGlobally?: boolean | undefined);
|
|
12
12
|
strategy(): "all" | "explicit";
|
|
13
13
|
private toInternalConfig;
|
|
14
|
-
get(key: string,
|
|
15
|
-
put(key: string, response: any, tables: string[], isTag
|
|
14
|
+
get(key: string, tables: string[], isTag?: boolean): Promise<any[] | undefined>;
|
|
15
|
+
put(key: string, response: any, tables: string[], isTag?: boolean, _config?: CacheConfig): Promise<void>;
|
|
16
16
|
onMutate(params: MutationOption): Promise<void>;
|
|
17
17
|
}
|
|
18
18
|
export declare function upstashCache({ url, token, config, global }: {
|
package/cache/upstash/cache.js
CHANGED
|
@@ -23,39 +23,55 @@ class UpstashCache extends Cache {
|
|
|
23
23
|
keepTtl: config.keepTtl
|
|
24
24
|
} : void 0;
|
|
25
25
|
}
|
|
26
|
-
async get(key,
|
|
27
|
-
const
|
|
28
|
-
|
|
26
|
+
async get(key, tables, isTag = false) {
|
|
27
|
+
const compositeKey = tables.sort().join(",");
|
|
28
|
+
if (isTag) {
|
|
29
|
+
const tagCompositeKey = await this.redis.hget("tagsMap", key);
|
|
30
|
+
if (tagCompositeKey) {
|
|
31
|
+
return await this.redis.hget(tagCompositeKey, key);
|
|
32
|
+
}
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
return (await this.redis.hget(compositeKey, key)) ?? void 0;
|
|
29
36
|
}
|
|
30
|
-
async put(key, response, tables, isTag,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
await this.redis.
|
|
37
|
+
async put(key, response, tables, isTag = false, _config) {
|
|
38
|
+
if (isTag) {
|
|
39
|
+
const compositeKey = tables.sort().join(",");
|
|
40
|
+
await this.redis.hset(compositeKey, { [key]: response });
|
|
41
|
+
await this.redis.hset("tagsMap", { [key]: compositeKey });
|
|
42
|
+
for (const table of tables) {
|
|
43
|
+
await this.redis.sadd(`prefix_${table}`, compositeKey);
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
const compositeKey = tables.sort().join(",");
|
|
47
|
+
await this.redis.hset(compositeKey, { [key]: response });
|
|
34
48
|
}
|
|
35
49
|
}
|
|
36
50
|
async onMutate(params) {
|
|
37
|
-
const
|
|
38
|
-
const
|
|
51
|
+
const tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];
|
|
52
|
+
const tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];
|
|
53
|
+
const mappedTables = tables.map((table) => is(table, Table) ? table[OriginalName] : table);
|
|
54
|
+
const prefixedMappedTables = tables.map((table) => `prefix_${table}`);
|
|
39
55
|
const keysToDelete = /* @__PURE__ */ new Set();
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const pipeline = this.redis.pipeline();
|
|
48
|
-
for (const tag of tagsArray) {
|
|
49
|
-
pipeline.del(tag);
|
|
56
|
+
if (tables.length > 0) {
|
|
57
|
+
const compositeKeys = await this.redis.sunion(...prefixedMappedTables);
|
|
58
|
+
for (const composite of compositeKeys) {
|
|
59
|
+
const keys = await this.redis.hkeys(composite);
|
|
60
|
+
for (const key of keys)
|
|
61
|
+
keysToDelete.add(key);
|
|
62
|
+
await this.redis.del(composite);
|
|
50
63
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
await this.redis.del(...prefixedMappedTables, ...mappedTables);
|
|
65
|
+
}
|
|
66
|
+
for (const tag of tags) {
|
|
67
|
+
const compositeKey = await this.redis.hget("tagsMap", tag);
|
|
68
|
+
if (compositeKey) {
|
|
69
|
+
await this.redis.hdel(compositeKey, tag);
|
|
70
|
+
await this.redis.hdel("tagsMap", tag);
|
|
57
71
|
}
|
|
58
|
-
|
|
72
|
+
}
|
|
73
|
+
if (keysToDelete.size > 0) {
|
|
74
|
+
await this.redis.del(...keysToDelete);
|
|
59
75
|
}
|
|
60
76
|
}
|
|
61
77
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import type { SetCommandOptions } from '@upstash/redis';\nimport { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\n\tprivate internalConfig?: SetCommandOptions;\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig) {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tex: config.ex,\n\t\t\t\texat: config.exat,\n\t\t\t\tpx: config.px,\n\t\t\t\tpxat: config.pxat,\n\t\t\t\tkeepTtl: config.keepTtl,\n\t\t\t} as SetCommandOptions\n\t\t\t: undefined;\n\t}\n\n\toverride async get(key: string,
|
|
1
|
+
{"version":3,"sources":["../../../src/cache/upstash/cache.ts"],"sourcesContent":["import type { SetCommandOptions } from '@upstash/redis';\nimport { Redis } from '@upstash/redis';\nimport type { MutationOption } from '~/cache/core/index.ts';\nimport { Cache } from '~/cache/core/index.ts';\nimport { entityKind, is } from '~/entity.ts';\nimport { OriginalName, Table } from '~/index.ts';\nimport type { CacheConfig } from '../core/types.ts';\n\nexport class UpstashCache extends Cache {\n\tstatic override readonly [entityKind]: string = 'UpstashCache';\n\n\tprivate internalConfig?: SetCommandOptions;\n\n\tconstructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {\n\t\tsuper();\n\t\tthis.internalConfig = this.toInternalConfig(config);\n\t}\n\n\tpublic strategy() {\n\t\treturn this.useGlobally ? 'all' : 'explicit';\n\t}\n\n\tprivate toInternalConfig(config?: CacheConfig) {\n\t\treturn config\n\t\t\t? {\n\t\t\t\tex: config.ex,\n\t\t\t\texat: config.exat,\n\t\t\t\tpx: config.px,\n\t\t\t\tpxat: config.pxat,\n\t\t\t\tkeepTtl: config.keepTtl,\n\t\t\t} as SetCommandOptions\n\t\t\t: undefined;\n\t}\n\n\toverride async get(key: string, tables: string[], isTag: boolean = false): Promise<any[] | undefined> {\n\t\tconst compositeKey = tables.sort().join(','); // Generate the composite key for the query\n\n\t\tif (isTag) {\n\t\t\t// Handle cache lookup for tags\n\t\t\tconst tagCompositeKey = await this.redis.hget<string>('tagsMap', key); // Retrieve composite key associated with the tag\n\t\t\tif (tagCompositeKey) {\n\t\t\t\treturn await this.redis.hget(tagCompositeKey, key) as any[]; // Retrieve the cached result for the tag\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\t// Normal cache lookup for the composite key\n\t\treturn await this.redis.hget(compositeKey, key) ?? undefined; // Retrieve result for normal query\n\t}\n\n\toverride async put(\n\t\tkey: string,\n\t\tresponse: any,\n\t\ttables: string[],\n\t\tisTag: boolean = false,\n\t\t_config?: CacheConfig,\n\t): Promise<void> {\n\t\tif (isTag) {\n\t\t\t// When it's a tag, store the response in the composite key\n\t\t\tconst compositeKey = tables.sort().join(',');\n\t\t\tawait this.redis.hset(compositeKey, { [key]: response }); // Store the result with the tag under the composite key\n\t\t\tawait this.redis.hset('tagsMap', { [key]: compositeKey }); // Store the tag and its composite key in the map\n\t\t\tfor (const table of tables) {\n\t\t\t\tawait this.redis.sadd(`prefix_${table}`, compositeKey);\n\t\t\t}\n\t\t} else {\n\t\t\t// Normal cache store\n\t\t\tconst compositeKey = tables.sort().join(',');\n\t\t\tawait this.redis.hset(compositeKey, { [key]: response }); // Store the result with the composite key\n\t\t}\n\t}\n\n\toverride async onMutate(params: MutationOption) {\n\t\tconst tags = Array.isArray(params.tags) ? params.tags : params.tags ? [params.tags] : [];\n\t\tconst tables = Array.isArray(params.tables) ? params.tables : params.tables ? [params.tables] : [];\n\t\tconst mappedTables = tables.map((table) => is(table, Table) ? table[OriginalName] : table as string);\n\t\tconst prefixedMappedTables = tables.map((table) => `prefix_${table}`);\n\n\t\tconst keysToDelete = new Set<string>();\n\n\t\t// Invalidate by table\n\t\tif (tables.length > 0) {\n\t\t\t// @ts-expect-error\n\t\t\tconst compositeKeys: string[] = await this.redis.sunion(...prefixedMappedTables);\n\t\t\tfor (const composite of compositeKeys) {\n\t\t\t\tconst keys = await this.redis.hkeys(composite);\n\t\t\t\tfor (const key of keys) keysToDelete.add(key);\n\t\t\t\tawait this.redis.del(composite); // Remove composite entry\n\t\t\t}\n\t\t\tawait this.redis.del(...prefixedMappedTables, ...mappedTables); // Remove table mappings\n\t\t}\n\n\t\t// Invalidate by tag (WITHOUT invalidating the entire table)\n\t\tfor (const tag of tags) {\n\t\t\tconst compositeKey = await this.redis.hget<string>('tagsMap', tag);\n\t\t\tif (compositeKey) {\n\t\t\t\tawait this.redis.hdel(compositeKey, tag); // Only remove the tag-related entry\n\t\t\t\tawait this.redis.hdel('tagsMap', tag); // Remove tag reference\n\t\t\t}\n\t\t}\n\n\t\t// Delete affected cache entries\n\t\tif (keysToDelete.size > 0) {\n\t\t\tawait this.redis.del(...keysToDelete);\n\t\t}\n\t}\n}\n\nexport function upstashCache(\n\t{ url, token, config, global = false }: { url: string; token: string; config?: CacheConfig; global?: boolean },\n): UpstashCache {\n\tconst redis = new Redis({\n\t\turl,\n\t\ttoken,\n\t});\n\n\treturn new UpstashCache(redis, config, global);\n}\n"],"mappings":"AACA,SAAS,aAAa;AAEtB,SAAS,aAAa;AACtB,SAAS,YAAY,UAAU;AAC/B,SAAS,cAAc,aAAa;AAG7B,MAAM,qBAAqB,MAAM;AAAA,EAKvC,YAAmB,OAAc,QAAgC,aAAuB;AACvF,UAAM;AADY;AAA8C;AAEhE,SAAK,iBAAiB,KAAK,iBAAiB,MAAM;AAAA,EACnD;AAAA,EAPA,QAA0B,UAAU,IAAY;AAAA,EAExC;AAAA,EAOD,WAAW;AACjB,WAAO,KAAK,cAAc,QAAQ;AAAA,EACnC;AAAA,EAEQ,iBAAiB,QAAsB;AAC9C,WAAO,SACJ;AAAA,MACD,IAAI,OAAO;AAAA,MACX,MAAM,OAAO;AAAA,MACb,IAAI,OAAO;AAAA,MACX,MAAM,OAAO;AAAA,MACb,SAAS,OAAO;AAAA,IACjB,IACE;AAAA,EACJ;AAAA,EAEA,MAAe,IAAI,KAAa,QAAkB,QAAiB,OAAmC;AACrG,UAAM,eAAe,OAAO,KAAK,EAAE,KAAK,GAAG;AAE3C,QAAI,OAAO;AAEV,YAAM,kBAAkB,MAAM,KAAK,MAAM,KAAa,WAAW,GAAG;AACpE,UAAI,iBAAiB;AACpB,eAAO,MAAM,KAAK,MAAM,KAAK,iBAAiB,GAAG;AAAA,MAClD;AACA,aAAO;AAAA,IACR;AAGA,WAAO,MAAM,KAAK,MAAM,KAAK,cAAc,GAAG,KAAK;AAAA,EACpD;AAAA,EAEA,MAAe,IACd,KACA,UACA,QACA,QAAiB,OACjB,SACgB;AAChB,QAAI,OAAO;AAEV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