@envelop/response-cache 6.1.2 → 6.2.0-alpha-20240520005117-b223e41e

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/cjs/plugin.js CHANGED
@@ -101,6 +101,7 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
101
101
  ? // eslint-disable-next-line dot-notation
102
102
  process.env['NODE_ENV'] === 'development' || !!process.env['DEBUG']
103
103
  : false, }) {
104
+ const cacheFactory = typeof cache === 'function' ? (0, utils_1.memoize1)(cache) : () => cache;
104
105
  const ignoredTypesMap = new Set(ignoredTypes);
105
106
  const typePerSchemaCoordinateMap = new Map();
106
107
  enabled = enabled ? (0, utils_1.memoize1)(enabled) : enabled;
@@ -236,7 +237,8 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
236
237
  }
237
238
  function invalidateCache(result, setResult) {
238
239
  processResult(result.data);
239
- cache.invalidate(identifier.values());
240
+ const cacheInstance = cacheFactory(onExecuteParams.args.contextValue);
241
+ cacheInstance.invalidate(identifier.values());
240
242
  if (includeExtensionMetadata) {
241
243
  setResult(resultWithMetadata(result, {
242
244
  invalidatedEntities: Array.from(identifier.values()),
@@ -267,7 +269,8 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
267
269
  sessionId,
268
270
  context: onExecuteParams.args.contextValue,
269
271
  });
270
- const cachedResponse = (await cache.get(cacheKey));
272
+ const cacheInstance = cacheFactory(onExecuteParams.args.contextValue);
273
+ const cachedResponse = (await cacheInstance.get(cacheKey));
271
274
  if (cachedResponse != null) {
272
275
  return setExecutor({
273
276
  execute: () => includeExtensionMetadata
@@ -285,7 +288,8 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
285
288
  }
286
289
  return;
287
290
  }
288
- cache.set(cacheKey, result, identifier.values(), finalTtl);
291
+ const cacheInstance = cacheFactory(onExecuteParams.args.contextValue);
292
+ cacheInstance.set(cacheKey, result, identifier.values(), finalTtl);
289
293
  if (includeExtensionMetadata) {
290
294
  setResult(resultWithMetadata(result, { hit: false, didCache: true, ttl: finalTtl }));
291
295
  }
package/esm/plugin.js CHANGED
@@ -94,6 +94,7 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
94
94
  ? // eslint-disable-next-line dot-notation
95
95
  process.env['NODE_ENV'] === 'development' || !!process.env['DEBUG']
96
96
  : false, }) {
97
+ const cacheFactory = typeof cache === 'function' ? memoize1(cache) : () => cache;
97
98
  const ignoredTypesMap = new Set(ignoredTypes);
98
99
  const typePerSchemaCoordinateMap = new Map();
99
100
  enabled = enabled ? memoize1(enabled) : enabled;
@@ -229,7 +230,8 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
229
230
  }
230
231
  function invalidateCache(result, setResult) {
231
232
  processResult(result.data);
232
- cache.invalidate(identifier.values());
233
+ const cacheInstance = cacheFactory(onExecuteParams.args.contextValue);
234
+ cacheInstance.invalidate(identifier.values());
233
235
  if (includeExtensionMetadata) {
234
236
  setResult(resultWithMetadata(result, {
235
237
  invalidatedEntities: Array.from(identifier.values()),
@@ -260,7 +262,8 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
260
262
  sessionId,
261
263
  context: onExecuteParams.args.contextValue,
262
264
  });
263
- const cachedResponse = (await cache.get(cacheKey));
265
+ const cacheInstance = cacheFactory(onExecuteParams.args.contextValue);
266
+ const cachedResponse = (await cacheInstance.get(cacheKey));
264
267
  if (cachedResponse != null) {
265
268
  return setExecutor({
266
269
  execute: () => includeExtensionMetadata
@@ -278,7 +281,8 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
278
281
  }
279
282
  return;
280
283
  }
281
- cache.set(cacheKey, result, identifier.values(), finalTtl);
284
+ const cacheInstance = cacheFactory(onExecuteParams.args.contextValue);
285
+ cacheInstance.set(cacheKey, result, identifier.values(), finalTtl);
282
286
  if (includeExtensionMetadata) {
283
287
  setResult(resultWithMetadata(result, { hit: false, didCache: true, ttl: finalTtl }));
284
288
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/response-cache",
3
- "version": "6.1.2",
3
+ "version": "6.2.0-alpha-20240520005117-b223e41e",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^5.0.0",
@@ -22,7 +22,7 @@ export type ShouldCacheResultFunction = (params: {
22
22
  result: ExecutionResult;
23
23
  }) => boolean;
24
24
  export type UseResponseCacheParameter<PluginContext extends Record<string, any> = {}> = {
25
- cache?: Cache;
25
+ cache?: Cache | ((ctx: Record<string, any>) => Cache);
26
26
  /**
27
27
  * Maximum age in ms. Defaults to `Infinity`. Set it to 0 for disabling the global TTL.
28
28
  */
@@ -22,7 +22,7 @@ export type ShouldCacheResultFunction = (params: {
22
22
  result: ExecutionResult;
23
23
  }) => boolean;
24
24
  export type UseResponseCacheParameter<PluginContext extends Record<string, any> = {}> = {
25
- cache?: Cache;
25
+ cache?: Cache | ((ctx: Record<string, any>) => Cache);
26
26
  /**
27
27
  * Maximum age in ms. Defaults to `Infinity`. Set it to 0 for disabling the global TTL.
28
28
  */