@medusajs/caching 2.10.4-preview-20250930180200
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/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/loaders/hash.d.ts +5 -0
- package/dist/loaders/hash.d.ts.map +1 -0
- package/dist/loaders/hash.js +9 -0
- package/dist/loaders/hash.js.map +1 -0
- package/dist/loaders/providers.d.ts +5 -0
- package/dist/loaders/providers.d.ts.map +1 -0
- package/dist/loaders/providers.js +57 -0
- package/dist/loaders/providers.js.map +1 -0
- package/dist/providers/memory-cache.d.ts +53 -0
- package/dist/providers/memory-cache.d.ts.map +1 -0
- package/dist/providers/memory-cache.js +158 -0
- package/dist/providers/memory-cache.js.map +1 -0
- package/dist/services/cache-module.d.ts +90 -0
- package/dist/services/cache-module.d.ts.map +1 -0
- package/dist/services/cache-module.js +204 -0
- package/dist/services/cache-module.js.map +1 -0
- package/dist/services/cache-provider.d.ts +13 -0
- package/dist/services/cache-provider.d.ts.map +1 -0
- package/dist/services/cache-provider.js +53 -0
- package/dist/services/cache-provider.js.map +1 -0
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +11 -0
- package/dist/services/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/index.d.ts +45 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +8 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/parser.d.ts +44 -0
- package/dist/utils/parser.d.ts.map +1 -0
- package/dist/utils/parser.js +161 -0
- package/dist/utils/parser.js.map +1 -0
- package/dist/utils/strategy.d.ts +17 -0
- package/dist/utils/strategy.d.ts.map +1 -0
- package/dist/utils/strategy.js +98 -0
- package/dist/utils/strategy.js.map +1 -0
- package/package.json +49 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import CachingModuleService from "./services/cache-module";
|
|
2
|
+
declare const _default: import("@medusajs/types").ModuleExports<typeof CachingModuleService> & {
|
|
3
|
+
linkable: Record<string, any>;
|
|
4
|
+
};
|
|
5
|
+
export default _default;
|
|
6
|
+
export { CachingModuleOptions } from "./types";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,oBAAoB,MAAM,yBAAyB,CAAA;;;;AAE1D,wBAGE;AAGF,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const utils_1 = require("@medusajs/framework/utils");
|
|
7
|
+
const hash_1 = __importDefault(require("./loaders/hash"));
|
|
8
|
+
const providers_1 = __importDefault(require("./loaders/providers"));
|
|
9
|
+
const cache_module_1 = __importDefault(require("./services/cache-module"));
|
|
10
|
+
exports.default = (0, utils_1.Module)(utils_1.Modules.CACHING, {
|
|
11
|
+
service: cache_module_1.default,
|
|
12
|
+
loaders: [hash_1.default, providers_1.default],
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,qDAA2D;AAC3D,0DAAoD;AACpD,oEAA8D;AAC9D,2EAA0D;AAE1D,kBAAe,IAAA,cAAM,EAAC,eAAO,CAAC,OAAO,EAAE;IACrC,OAAO,EAAE,sBAAoB;IAC7B,OAAO,EAAE,CAAC,cAAQ,EAAE,mBAAa,CAAC;CACnC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/loaders/hash.ts"],"names":[],"mappings":";;;AAEA,wBAKC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const awilix_1 = require("awilix");
|
|
4
|
+
exports.default = async ({ container }) => {
|
|
5
|
+
const xxhashhWasm = await import("xxhash-wasm");
|
|
6
|
+
const { h32ToString } = await xxhashhWasm.default();
|
|
7
|
+
container.register("hasher", (0, awilix_1.asValue)(h32ToString));
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=hash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/loaders/hash.ts"],"names":[],"mappings":";;AAAA,mCAAgC;AAEhC,kBAAe,KAAK,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE;IACrC,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAA;IAC/C,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,WAAW,CAAC,OAAO,EAAE,CAAA;IAEnD,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,IAAA,gBAAO,EAAC,WAAW,CAAC,CAAC,CAAA;AACpD,CAAC,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { LoaderOptions, ModulesSdkTypes } from "@medusajs/framework/types";
|
|
2
|
+
import { CachingModuleOptions } from "../types";
|
|
3
|
+
declare const _default: ({ container, options, }: LoaderOptions<(ModulesSdkTypes.ModuleServiceInitializeOptions | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions) & CachingModuleOptions>) => Promise<void>;
|
|
4
|
+
export default _default;
|
|
5
|
+
//# sourceMappingURL=providers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../../src/loaders/providers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAM1E,OAAO,EAGL,oBAAoB,EAErB,MAAM,QAAQ,CAAA;kDA2BZ,aAAa,CACd,CACI,eAAe,CAAC,8BAA8B,GAC9C,eAAe,CAAC,6CAA6C,CAChE,GACC,oBAAoB,CACvB,KAAG,OAAO,CAAC,IAAI,CAAC;AATjB,wBAyDC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const modules_sdk_1 = require("@medusajs/framework/modules-sdk");
|
|
4
|
+
const utils_1 = require("@medusajs/framework/utils");
|
|
5
|
+
const _services_1 = require("../services");
|
|
6
|
+
const _types_1 = require("../types");
|
|
7
|
+
const awilix_1 = require("awilix");
|
|
8
|
+
const memory_cache_1 = require("../providers/memory-cache");
|
|
9
|
+
const strategy_1 = require("../utils/strategy");
|
|
10
|
+
const registrationFn = async (klass, container, { id }) => {
|
|
11
|
+
const key = _services_1.CachingProviderService.getRegistrationIdentifier(klass);
|
|
12
|
+
if (!id) {
|
|
13
|
+
throw new Error(`No "id" provided for provider ${key}`);
|
|
14
|
+
}
|
|
15
|
+
const regKey = (0, utils_1.getProviderRegistrationKey)({
|
|
16
|
+
providerId: id,
|
|
17
|
+
providerIdentifier: key,
|
|
18
|
+
});
|
|
19
|
+
container.register({
|
|
20
|
+
[_types_1.CachingProviderRegistrationPrefix + id]: (0, awilix_1.aliasTo)(regKey),
|
|
21
|
+
});
|
|
22
|
+
container.registerAdd(_types_1.CachingIdentifiersRegistrationName, (0, awilix_1.asValue)(key));
|
|
23
|
+
};
|
|
24
|
+
exports.default = async ({ container, options, }) => {
|
|
25
|
+
container.registerAdd(_types_1.CachingIdentifiersRegistrationName, (0, awilix_1.asValue)(undefined));
|
|
26
|
+
const strategy = strategy_1.DefaultCacheStrategy; // Re enable custom strategy another time
|
|
27
|
+
container.register("strategy", (0, awilix_1.asValue)(strategy));
|
|
28
|
+
// MemoryCachingProvider - default provider
|
|
29
|
+
container.register({
|
|
30
|
+
[_types_1.CachingProviderRegistrationPrefix + memory_cache_1.MemoryCachingProvider.identifier]: (0, awilix_1.asFunction)(() => new memory_cache_1.MemoryCachingProvider(), {
|
|
31
|
+
lifetime: awilix_1.Lifetime.SINGLETON,
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
container.registerAdd(_types_1.CachingIdentifiersRegistrationName, (0, awilix_1.asValue)(memory_cache_1.MemoryCachingProvider.identifier));
|
|
35
|
+
container.register(_types_1.CachingDefaultProvider, (0, awilix_1.asValue)(memory_cache_1.MemoryCachingProvider.identifier));
|
|
36
|
+
// Load other providers
|
|
37
|
+
await (0, modules_sdk_1.moduleProviderLoader)({
|
|
38
|
+
container,
|
|
39
|
+
providers: options?.providers || [],
|
|
40
|
+
registerServiceFn: registrationFn,
|
|
41
|
+
});
|
|
42
|
+
const isSingleProvider = options?.providers?.length === 1;
|
|
43
|
+
let hasDefaultProvider = false;
|
|
44
|
+
for (const provider of options?.providers || []) {
|
|
45
|
+
if (provider.is_default || isSingleProvider) {
|
|
46
|
+
if (provider.is_default) {
|
|
47
|
+
hasDefaultProvider = true;
|
|
48
|
+
}
|
|
49
|
+
container.register(_types_1.CachingDefaultProvider, (0, awilix_1.asValue)(provider.id));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const logger = container.resolve(utils_1.ContainerRegistrationKeys.LOGGER);
|
|
53
|
+
if (!hasDefaultProvider) {
|
|
54
|
+
logger.warn(`[caching-module]: No default caching provider defined. Using "${container.resolve(_types_1.CachingDefaultProvider)}" as default.`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=providers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file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/types";
|
|
3
|
+
export interface MemoryCacheModuleOptions {
|
|
4
|
+
/**
|
|
5
|
+
* TTL in seconds
|
|
6
|
+
*/
|
|
7
|
+
ttl?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Maximum number of keys to store (see node-cache documentation)
|
|
10
|
+
*/
|
|
11
|
+
maxKeys?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Check period for expired keys in seconds (see node-cache documentation)
|
|
14
|
+
*/
|
|
15
|
+
checkPeriod?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Use clones for cached data (see node-cache documentation)
|
|
18
|
+
*/
|
|
19
|
+
useClones?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare class MemoryCachingProvider implements ICachingProviderService {
|
|
22
|
+
static identifier: string;
|
|
23
|
+
protected cacheClient: NodeCache;
|
|
24
|
+
protected tagIndex: Map<string, Set<string>>;
|
|
25
|
+
protected keyTags: Map<string, Set<string>>;
|
|
26
|
+
protected entryOptions: Map<string, {
|
|
27
|
+
autoInvalidate?: boolean;
|
|
28
|
+
}>;
|
|
29
|
+
protected options: MemoryCacheModuleOptions;
|
|
30
|
+
constructor();
|
|
31
|
+
private cleanupTagReferences;
|
|
32
|
+
get({ key, tags }: {
|
|
33
|
+
key?: string;
|
|
34
|
+
tags?: string[];
|
|
35
|
+
}): Promise<any>;
|
|
36
|
+
set({ key, data, ttl, tags, options, }: {
|
|
37
|
+
key: string;
|
|
38
|
+
data: object;
|
|
39
|
+
ttl?: number;
|
|
40
|
+
tags?: string[];
|
|
41
|
+
options?: {
|
|
42
|
+
autoInvalidate?: boolean;
|
|
43
|
+
};
|
|
44
|
+
}): Promise<void>;
|
|
45
|
+
clear({ key, tags, options, }: {
|
|
46
|
+
key?: string;
|
|
47
|
+
tags?: string[];
|
|
48
|
+
options?: {
|
|
49
|
+
autoInvalidate?: boolean;
|
|
50
|
+
};
|
|
51
|
+
}): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=memory-cache.d.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,GAAG,CAAC,EAAE,MAAM,CAAA;QACZ,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;QACf,OAAO,CAAC,EAAE;YACR,cAAc,CAAC,EAAE,OAAO,CAAA;SACzB,CAAA;KACF,GAAG,OAAO,CAAC,IAAI,CAAC;IA+BX,KAAK,CAAC,EACV,GAAG,EACH,IAAI,EACJ,OAAO,GACR,EAAE;QACD,GAAG,CAAC,EAAE,MAAM,CAAA;QACZ,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;QACf,OAAO,CAAC,EAAE;YACR,cAAc,CAAC,EAAE,OAAO,CAAA;SACzB,CAAA;KACF,GAAG,OAAO,CAAC,IAAI,CAAC;CA+DlB"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MemoryCachingProvider = void 0;
|
|
7
|
+
const node_cache_1 = __importDefault(require("node-cache"));
|
|
8
|
+
class MemoryCachingProvider {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.tagIndex = new Map(); // tag -> keys
|
|
11
|
+
this.keyTags = new Map(); // key -> tags
|
|
12
|
+
this.entryOptions = new Map(); // key -> options
|
|
13
|
+
this.options = {
|
|
14
|
+
ttl: 3600,
|
|
15
|
+
maxKeys: 25000,
|
|
16
|
+
checkPeriod: 60, // 10 minutes
|
|
17
|
+
useClones: false, // Default to false for speed, true would be slower but safer. we can discuss
|
|
18
|
+
};
|
|
19
|
+
const cacheClient = new node_cache_1.default({
|
|
20
|
+
stdTTL: this.options.ttl,
|
|
21
|
+
maxKeys: this.options.maxKeys,
|
|
22
|
+
checkperiod: this.options.checkPeriod,
|
|
23
|
+
useClones: this.options.useClones,
|
|
24
|
+
});
|
|
25
|
+
this.cacheClient = cacheClient;
|
|
26
|
+
// Clean up tag indices when keys expire
|
|
27
|
+
this.cacheClient.on("expired", (key, value) => {
|
|
28
|
+
this.cleanupTagReferences(key);
|
|
29
|
+
});
|
|
30
|
+
this.cacheClient.on("del", (key, value) => {
|
|
31
|
+
this.cleanupTagReferences(key);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
cleanupTagReferences(key) {
|
|
35
|
+
const tags = this.keyTags.get(key);
|
|
36
|
+
if (tags) {
|
|
37
|
+
tags.forEach((tag) => {
|
|
38
|
+
const keysForTag = this.tagIndex.get(tag);
|
|
39
|
+
if (keysForTag) {
|
|
40
|
+
keysForTag.delete(key);
|
|
41
|
+
if (keysForTag.size === 0) {
|
|
42
|
+
this.tagIndex.delete(tag);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
this.keyTags.delete(key);
|
|
47
|
+
}
|
|
48
|
+
// Also clean up entry options
|
|
49
|
+
this.entryOptions.delete(key);
|
|
50
|
+
}
|
|
51
|
+
async get({ key, tags }) {
|
|
52
|
+
if (key) {
|
|
53
|
+
return this.cacheClient.get(key) ?? null;
|
|
54
|
+
}
|
|
55
|
+
if (tags && tags.length) {
|
|
56
|
+
const allKeys = new Set();
|
|
57
|
+
tags.forEach((tag) => {
|
|
58
|
+
const keysForTag = this.tagIndex.get(tag);
|
|
59
|
+
if (keysForTag) {
|
|
60
|
+
keysForTag.forEach((key) => allKeys.add(key));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
if (allKeys.size === 0) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
const results = [];
|
|
67
|
+
allKeys.forEach((key) => {
|
|
68
|
+
const value = this.cacheClient.get(key);
|
|
69
|
+
if (value !== undefined) {
|
|
70
|
+
results.push(value);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return results;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
async set({ key, data, ttl, tags, options, }) {
|
|
78
|
+
// Set the cache entry
|
|
79
|
+
const effectiveTTL = ttl ?? this.options.ttl ?? 3600;
|
|
80
|
+
this.cacheClient.set(key, data, effectiveTTL);
|
|
81
|
+
// Handle tags if provided
|
|
82
|
+
if (tags && tags.length) {
|
|
83
|
+
// Clean up any existing tag references for this key
|
|
84
|
+
this.cleanupTagReferences(key);
|
|
85
|
+
const tagSet = new Set(tags);
|
|
86
|
+
this.keyTags.set(key, tagSet);
|
|
87
|
+
// Add this key to each tag's index
|
|
88
|
+
tags.forEach((tag) => {
|
|
89
|
+
if (!this.tagIndex.has(tag)) {
|
|
90
|
+
this.tagIndex.set(tag, new Set());
|
|
91
|
+
}
|
|
92
|
+
this.tagIndex.get(tag).add(key);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// Store entry options if provided
|
|
96
|
+
if (Object.keys(options ?? {}).length &&
|
|
97
|
+
!Object.values(options ?? {}).every((value) => value === undefined)) {
|
|
98
|
+
this.entryOptions.set(key, options);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async clear({ key, tags, options, }) {
|
|
102
|
+
if (key) {
|
|
103
|
+
this.cacheClient.del(key);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (tags && tags.length) {
|
|
107
|
+
// Handle wildcard tag to clear all cache data
|
|
108
|
+
if (tags.includes("*")) {
|
|
109
|
+
this.cacheClient.flushAll();
|
|
110
|
+
this.tagIndex.clear();
|
|
111
|
+
this.keyTags.clear();
|
|
112
|
+
this.entryOptions.clear();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const allKeys = new Set();
|
|
116
|
+
tags.forEach((tag) => {
|
|
117
|
+
const keysForTag = this.tagIndex.get(tag);
|
|
118
|
+
if (keysForTag) {
|
|
119
|
+
keysForTag.forEach((key) => allKeys.add(key));
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
if (allKeys.size) {
|
|
123
|
+
// If no options provided (user explicit call), clear everything
|
|
124
|
+
if (!options) {
|
|
125
|
+
const keysToDelete = Array.from(allKeys);
|
|
126
|
+
this.cacheClient.del(keysToDelete);
|
|
127
|
+
// Clean up ALL tag references for deleted keys
|
|
128
|
+
keysToDelete.forEach((key) => {
|
|
129
|
+
this.cleanupTagReferences(key);
|
|
130
|
+
});
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// If autoInvalidate is true (strategy call), only clear entries with autoInvalidate=true (default)
|
|
134
|
+
if (options.autoInvalidate === true) {
|
|
135
|
+
const keysToDelete = [];
|
|
136
|
+
allKeys.forEach((key) => {
|
|
137
|
+
const entryOptions = this.entryOptions.get(key);
|
|
138
|
+
// Delete if entry has autoInvalidate=true or no setting (default true)
|
|
139
|
+
const shouldAutoInvalidate = entryOptions?.autoInvalidate ?? true;
|
|
140
|
+
if (shouldAutoInvalidate) {
|
|
141
|
+
keysToDelete.push(key);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
if (keysToDelete.length) {
|
|
145
|
+
this.cacheClient.del(keysToDelete);
|
|
146
|
+
// Clean up ALL tag references for deleted keys
|
|
147
|
+
keysToDelete.forEach((key) => {
|
|
148
|
+
this.cleanupTagReferences(key);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.MemoryCachingProvider = MemoryCachingProvider;
|
|
157
|
+
MemoryCachingProvider.identifier = "cache-memory";
|
|
158
|
+
//# sourceMappingURL=memory-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-cache.js","sourceRoot":"","sources":["../../src/providers/memory-cache.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAkC;AAsBlC,MAAa,qBAAqB;IAShC;QALU,aAAQ,GAA6B,IAAI,GAAG,EAAE,CAAA,CAAC,cAAc;QAC7D,YAAO,GAA6B,IAAI,GAAG,EAAE,CAAA,CAAC,cAAc;QAC5D,iBAAY,GAA8C,I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AY,CAAC,CAAA;oBAElC,+CAA+C;oBAC/C,YAAY,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;wBAC3B,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAA;oBAChC,CAAC,CAAC,CAAA;oBACF,OAAM;gBACR,CAAC;gBAED,mGAAmG;gBACnG,IAAI,OAAO,CAAC,cAAc,KAAK,IAAI,EAAE,CAAC;oBACpC,MAAM,YAAY,GAAa,EAAE,CAAA;oBAEjC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;wBACtB,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;wBAC/C,uEAAuE;wBACvE,MAAM,oBAAoB,GAAG,YAAY,EAAE,cAAc,IAAI,IAAI,CAAA;wBACjE,IAAI,oBAAoB,EAAE,CAAC;4BACzB,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;wBACxB,CAAC;oBACH,CAAC,CAAC,CAAA;oBAEF,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;wBACxB,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;wBAElC,+CAA+C;wBAC/C,YAAY,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;4BAC3B,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAA;wBAChC,CAAC,CAAC,CAAA;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;;AA5MH,sDA6MC;AA5MQ,gCAAU,GAAG,cAAc,AAAjB,CAAiB"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ICachingModuleService, ICachingStrategy, Logger } from "@medusajs/framework/types";
|
|
2
|
+
import { InjectedDependencies } from "../types";
|
|
3
|
+
import CacheProviderService from "./cache-provider";
|
|
4
|
+
export default class CachingModuleService implements ICachingModuleService {
|
|
5
|
+
protected readonly moduleDeclaration: {
|
|
6
|
+
options: {
|
|
7
|
+
ttl?: number;
|
|
8
|
+
};
|
|
9
|
+
} | {
|
|
10
|
+
ttl?: number;
|
|
11
|
+
};
|
|
12
|
+
protected container: InjectedDependencies;
|
|
13
|
+
protected providerService: CacheProviderService;
|
|
14
|
+
protected strategyCtr: new (...args: any[]) => ICachingStrategy;
|
|
15
|
+
protected strategy: ICachingStrategy;
|
|
16
|
+
protected defaultProviderId: string;
|
|
17
|
+
protected logger: Logger;
|
|
18
|
+
protected ongoingRequests: Map<string, Promise<any>>;
|
|
19
|
+
protected ttl: number;
|
|
20
|
+
static traceGet?: (cacheGetFn: () => Promise<any>, key: string, tags: string[]) => Promise<any>;
|
|
21
|
+
static traceSet?: (cacheSetFn: () => Promise<any>, key: string, tags: string[], options: {
|
|
22
|
+
autoInvalidate?: boolean;
|
|
23
|
+
}) => Promise<any>;
|
|
24
|
+
static traceClear?: (cacheClearFn: () => Promise<any>, key: string, tags: string[], options: {
|
|
25
|
+
autoInvalidate?: boolean;
|
|
26
|
+
}) => Promise<any>;
|
|
27
|
+
constructor(container: InjectedDependencies, moduleDeclaration: {
|
|
28
|
+
options: {
|
|
29
|
+
ttl?: number;
|
|
30
|
+
};
|
|
31
|
+
} | {
|
|
32
|
+
ttl?: number;
|
|
33
|
+
});
|
|
34
|
+
__hooks: {
|
|
35
|
+
onApplicationStart: () => Promise<void>;
|
|
36
|
+
onApplicationShutdown: () => Promise<void>;
|
|
37
|
+
onApplicationPrepareShutdown: () => Promise<void>;
|
|
38
|
+
};
|
|
39
|
+
protected onApplicationStart(): void;
|
|
40
|
+
protected onApplicationShutdown(): void;
|
|
41
|
+
protected onApplicationPrepareShutdown(): void;
|
|
42
|
+
protected static normalizeProviders(providers: string[] | {
|
|
43
|
+
id: string;
|
|
44
|
+
ttl?: number;
|
|
45
|
+
}[]): {
|
|
46
|
+
id: string;
|
|
47
|
+
ttl?: number;
|
|
48
|
+
}[];
|
|
49
|
+
protected getRequestKey(key?: string, tags?: string[], providers?: string[]): string;
|
|
50
|
+
protected getClearRequestKey(key?: string, tags?: string[], providers?: string[]): string;
|
|
51
|
+
get(options: {
|
|
52
|
+
key?: string;
|
|
53
|
+
tags?: string[];
|
|
54
|
+
providers?: string[];
|
|
55
|
+
}): Promise<any>;
|
|
56
|
+
private get_;
|
|
57
|
+
protected performCacheGet(key?: string, tags?: string[], providers?: string[]): Promise<any>;
|
|
58
|
+
set(options: {
|
|
59
|
+
key: string;
|
|
60
|
+
data: object;
|
|
61
|
+
ttl?: number;
|
|
62
|
+
tags?: string[];
|
|
63
|
+
providers?: string[];
|
|
64
|
+
options?: {
|
|
65
|
+
autoInvalidate?: boolean;
|
|
66
|
+
};
|
|
67
|
+
}): Promise<any>;
|
|
68
|
+
private set_;
|
|
69
|
+
protected performCacheSet(key: string, tags: string[], data: object, ttl?: number, providers?: {
|
|
70
|
+
id: string;
|
|
71
|
+
ttl?: number;
|
|
72
|
+
}[], options?: {
|
|
73
|
+
autoInvalidate?: boolean;
|
|
74
|
+
}): Promise<void>;
|
|
75
|
+
clear(options: {
|
|
76
|
+
key?: string;
|
|
77
|
+
tags?: string[];
|
|
78
|
+
options?: {
|
|
79
|
+
autoInvalidate?: boolean;
|
|
80
|
+
};
|
|
81
|
+
providers?: string[];
|
|
82
|
+
}): Promise<any>;
|
|
83
|
+
private clear_;
|
|
84
|
+
protected performCacheClear(key?: string, tags?: string[], options?: {
|
|
85
|
+
autoInvalidate?: boolean;
|
|
86
|
+
}, providers?: string[]): Promise<void>;
|
|
87
|
+
computeKey(input: object): Promise<string>;
|
|
88
|
+
computeTags(input: object, options?: Record<string, any>): Promise<string[]>;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=cache-module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-module.d.ts","sourceRoot":"","sources":["../../src/services/cache-module.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,MAAM,EACP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EAAE,MAAM,EACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC5B,OAAO,CAAC,MAAM,EAAE,CAAC;CAGrB"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const modules_sdk_1 = require("@medusajs/framework/modules-sdk");
|
|
4
|
+
const utils_1 = require("@medusajs/framework/utils");
|
|
5
|
+
const _types_1 = require("../types");
|
|
6
|
+
const ONE_HOUR_IN_SECOND = 60 * 60;
|
|
7
|
+
class CachingModuleService {
|
|
8
|
+
constructor(container, moduleDeclaration) {
|
|
9
|
+
this.moduleDeclaration = moduleDeclaration;
|
|
10
|
+
this.ongoingRequests = new Map();
|
|
11
|
+
this.__hooks = {
|
|
12
|
+
onApplicationStart: async () => {
|
|
13
|
+
this.onApplicationStart();
|
|
14
|
+
},
|
|
15
|
+
onApplicationShutdown: async () => {
|
|
16
|
+
this.onApplicationShutdown();
|
|
17
|
+
},
|
|
18
|
+
onApplicationPrepareShutdown: async () => {
|
|
19
|
+
this.onApplicationPrepareShutdown();
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
this.container = container;
|
|
23
|
+
this.providerService = container.cacheProviderService;
|
|
24
|
+
this.defaultProviderId = container[_types_1.CachingDefaultProvider];
|
|
25
|
+
this.strategyCtr = container.strategy;
|
|
26
|
+
this.strategy = new this.strategyCtr(this.container, this);
|
|
27
|
+
const moduleOptions = "options" in moduleDeclaration
|
|
28
|
+
? moduleDeclaration.options
|
|
29
|
+
: moduleDeclaration;
|
|
30
|
+
this.ttl = moduleOptions.ttl ?? ONE_HOUR_IN_SECOND;
|
|
31
|
+
this.logger = container.logger ?? console;
|
|
32
|
+
}
|
|
33
|
+
onApplicationStart() {
|
|
34
|
+
const loadedSchema = modules_sdk_1.MedusaModule.getAllJoinerConfigs()
|
|
35
|
+
.map((joinerConfig) => joinerConfig?.schema ?? "")
|
|
36
|
+
.join("\n");
|
|
37
|
+
const defaultMedusaSchema = `
|
|
38
|
+
scalar DateTime
|
|
39
|
+
scalar JSON
|
|
40
|
+
directive @enumValue(value: String) on ENUM_VALUE
|
|
41
|
+
`;
|
|
42
|
+
const { schema: cleanedSchema } = utils_1.GraphQLUtils.cleanGraphQLSchema(defaultMedusaSchema + loadedSchema);
|
|
43
|
+
const mergedSchema = utils_1.GraphQLUtils.mergeTypeDefs(cleanedSchema);
|
|
44
|
+
const schema = utils_1.GraphQLUtils.makeExecutableSchema({
|
|
45
|
+
typeDefs: mergedSchema,
|
|
46
|
+
});
|
|
47
|
+
this.strategy.onApplicationStart?.(schema, modules_sdk_1.MedusaModule.getAllJoinerConfigs());
|
|
48
|
+
}
|
|
49
|
+
onApplicationShutdown() {
|
|
50
|
+
this.strategy.onApplicationShutdown?.();
|
|
51
|
+
}
|
|
52
|
+
onApplicationPrepareShutdown() {
|
|
53
|
+
this.strategy.onApplicationPrepareShutdown?.();
|
|
54
|
+
}
|
|
55
|
+
static normalizeProviders(providers) {
|
|
56
|
+
const providers_ = Array.isArray(providers) ? providers : [providers];
|
|
57
|
+
return providers_.map((provider) => {
|
|
58
|
+
return typeof provider === "string" ? { id: provider } : provider;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
getRequestKey(key, tags, providers) {
|
|
62
|
+
const keyPart = key || "";
|
|
63
|
+
const tagsPart = tags?.sort().join(",") || "";
|
|
64
|
+
const providersPart = providers?.join(",") || this.defaultProviderId;
|
|
65
|
+
return `${keyPart}|${tagsPart}|${providersPart}`;
|
|
66
|
+
}
|
|
67
|
+
getClearRequestKey(key, tags, providers) {
|
|
68
|
+
const keyPart = key || "";
|
|
69
|
+
const tagsPart = tags?.sort().join(",") || "";
|
|
70
|
+
const providersPart = providers?.join(",") || this.defaultProviderId;
|
|
71
|
+
return `clear:${keyPart}|${tagsPart}|${providersPart}`;
|
|
72
|
+
}
|
|
73
|
+
async get(options) {
|
|
74
|
+
if (CachingModuleService.traceGet) {
|
|
75
|
+
return await CachingModuleService.traceGet(() => this.get_(options), options.key ?? "", options.tags ?? []);
|
|
76
|
+
}
|
|
77
|
+
return await this.get_(options);
|
|
78
|
+
}
|
|
79
|
+
async get_({ key, tags, providers, }) {
|
|
80
|
+
if (!key && !tags) {
|
|
81
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_ARGUMENT, "Either key or tags must be provided");
|
|
82
|
+
}
|
|
83
|
+
const requestKey = this.getRequestKey(key, tags, providers);
|
|
84
|
+
const existingRequest = this.ongoingRequests.get(requestKey);
|
|
85
|
+
if (existingRequest) {
|
|
86
|
+
return await existingRequest;
|
|
87
|
+
}
|
|
88
|
+
const requestPromise = this.performCacheGet(key, tags, providers);
|
|
89
|
+
this.ongoingRequests.set(requestKey, requestPromise);
|
|
90
|
+
try {
|
|
91
|
+
const result = await requestPromise;
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
// Clean up the completed request
|
|
96
|
+
this.ongoingRequests.delete(requestKey);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async performCacheGet(key, tags, providers) {
|
|
100
|
+
const providersToCheck = providers ?? [this.defaultProviderId];
|
|
101
|
+
for (const providerId of providersToCheck) {
|
|
102
|
+
try {
|
|
103
|
+
const provider_ = this.providerService.retrieveProvider(providerId);
|
|
104
|
+
const result = await provider_.get({ key, tags });
|
|
105
|
+
if (result != null) {
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
this.logger.warn(`Cache provider ${providerId} failed: ${error.message}\n${error.stack}`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
async set(options) {
|
|
117
|
+
if (CachingModuleService.traceSet) {
|
|
118
|
+
return await CachingModuleService.traceSet(() => this.set_(options), options.key, options.tags ?? [], options.options ?? {});
|
|
119
|
+
}
|
|
120
|
+
return await this.set_(options);
|
|
121
|
+
}
|
|
122
|
+
async set_({ key, data, ttl, tags, providers, options, }) {
|
|
123
|
+
if (!key) {
|
|
124
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_ARGUMENT, "[CachingModuleService] Key must be provided");
|
|
125
|
+
}
|
|
126
|
+
const key_ = key;
|
|
127
|
+
const tags_ = tags ?? (await this.strategy.computeTags(data));
|
|
128
|
+
let providers_ = [
|
|
129
|
+
this.defaultProviderId,
|
|
130
|
+
];
|
|
131
|
+
providers_ = CachingModuleService.normalizeProviders(providers ?? providers_);
|
|
132
|
+
const providerIds = providers_.map((p) => p.id);
|
|
133
|
+
const requestKey = this.getRequestKey(key_, tags_, providerIds);
|
|
134
|
+
const existingRequest = this.ongoingRequests.get(requestKey);
|
|
135
|
+
if (existingRequest) {
|
|
136
|
+
return await existingRequest;
|
|
137
|
+
}
|
|
138
|
+
const requestPromise = this.performCacheSet(key_, tags_, data, ttl, providers_, options);
|
|
139
|
+
this.ongoingRequests.set(requestKey, requestPromise);
|
|
140
|
+
try {
|
|
141
|
+
await requestPromise;
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
// Clean up the completed request
|
|
145
|
+
this.ongoingRequests.delete(requestKey);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async performCacheSet(key, tags, data, ttl, providers, options) {
|
|
149
|
+
for (const providerOptions of providers || []) {
|
|
150
|
+
const ttl_ = providerOptions.ttl ?? ttl ?? this.ttl;
|
|
151
|
+
const provider = this.providerService.retrieveProvider(providerOptions.id);
|
|
152
|
+
void provider.set({
|
|
153
|
+
key,
|
|
154
|
+
tags,
|
|
155
|
+
data,
|
|
156
|
+
ttl: ttl_,
|
|
157
|
+
options,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async clear(options) {
|
|
162
|
+
if (CachingModuleService.traceClear) {
|
|
163
|
+
return await CachingModuleService.traceClear(() => this.clear_(options), options.key ?? "", options.tags ?? [], options.options ?? {});
|
|
164
|
+
}
|
|
165
|
+
return await this.clear_(options);
|
|
166
|
+
}
|
|
167
|
+
async clear_({ key, tags, options, providers, }) {
|
|
168
|
+
if (!key && !tags) {
|
|
169
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_ARGUMENT, "Either key or tags must be provided");
|
|
170
|
+
}
|
|
171
|
+
const requestKey = this.getClearRequestKey(key, tags, providers);
|
|
172
|
+
const existingRequest = this.ongoingRequests.get(requestKey);
|
|
173
|
+
if (existingRequest) {
|
|
174
|
+
return await existingRequest;
|
|
175
|
+
}
|
|
176
|
+
const requestPromise = this.performCacheClear(key, tags, options, providers);
|
|
177
|
+
this.ongoingRequests.set(requestKey, requestPromise);
|
|
178
|
+
try {
|
|
179
|
+
await requestPromise;
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
// Clean up the completed request
|
|
183
|
+
this.ongoingRequests.delete(requestKey);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async performCacheClear(key, tags, options, providers) {
|
|
187
|
+
let providerIds_ = [this.defaultProviderId];
|
|
188
|
+
if (providers) {
|
|
189
|
+
providerIds_ = Array.isArray(providers) ? providers : [providers];
|
|
190
|
+
}
|
|
191
|
+
for (const providerId of providerIds_) {
|
|
192
|
+
const provider = this.providerService.retrieveProvider(providerId);
|
|
193
|
+
void provider.clear({ key, tags, options });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async computeKey(input) {
|
|
197
|
+
return await this.strategy.computeKey(input);
|
|
198
|
+
}
|
|
199
|
+
async computeTags(input, options) {
|
|
200
|
+
return await this.strategy.computeTags(input, options);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.default = CachingModuleService;
|
|
204
|
+
//# sourceMappingURL=cache-module.js.map
|