@medusajs/cache-inmemory 2.0.0-next-20230310141356

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/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Medusa Cache In-memory
2
+
3
+ Medusa in-memory cache module. Use plain JS Map as a cache store.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ yarn add @medusajs/cache-inmemory
9
+ ```
10
+
11
+ ## Options
12
+
13
+ ```
14
+ {
15
+ ttl?: number // Time to keep data in cache (in seconds)
16
+ }
17
+ ```
18
+
19
+ ### Note
20
+ Recommended for testing and development. For production, use Redis cache module.
21
+
22
+ ### Other caching modules
23
+ - [Medusa Cache Redis](../cache-redis/README.md)
@@ -0,0 +1,3 @@
1
+ import { ModuleExports } from "@medusajs/modules-sdk";
2
+ declare const moduleDefinition: ModuleExports;
3
+ export default moduleDefinition;
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
+ var inmemory_cache_1 = __importDefault(require("./services/inmemory-cache"));
7
+ var loaders = [];
8
+ var service = inmemory_cache_1.default;
9
+ var moduleDefinition = {
10
+ service: service,
11
+ loaders: loaders,
12
+ };
13
+ exports.default = moduleDefinition;
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAEA,6EAA4D;AAE5D,IAAM,OAAO,GAAG,EAAE,CAAA;AAClB,IAAM,OAAO,GAAG,wBAAoB,CAAA;AAEpC,IAAM,gBAAgB,GAAkB;IACtC,OAAO,SAAA;IACP,OAAO,SAAA;CACR,CAAA;AAED,kBAAe,gBAAgB,CAAA"}
@@ -0,0 +1 @@
1
+ export { default as InMemoryCacheService } from "./inmemory-cache";
@@ -0,0 +1,9 @@
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.InMemoryCacheService = void 0;
7
+ var inmemory_cache_1 = require("./inmemory-cache");
8
+ Object.defineProperty(exports, "InMemoryCacheService", { enumerable: true, get: function () { return __importDefault(inmemory_cache_1).default; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;AAAA,mDAAkE;AAAzD,uIAAA,OAAO,OAAwB"}
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" />
2
+ import { ICacheService } from "@medusajs/medusa";
3
+ import { CacheRecord, InMemoryCacheModuleOptions } from "../types";
4
+ declare type InjectedDependencies = {};
5
+ /**
6
+ * Class represents basic, in-memory, cache store.
7
+ */
8
+ declare class InMemoryCacheService implements ICacheService {
9
+ protected readonly TTL: number;
10
+ protected readonly store: Map<string, CacheRecord<any>>;
11
+ protected readonly timoutRefs: Map<string, NodeJS.Timeout>;
12
+ constructor(deps: InjectedDependencies, options?: InMemoryCacheModuleOptions);
13
+ /**
14
+ * Retrieve data from the cache.
15
+ * @param key - cache key
16
+ */
17
+ get<T>(key: string): Promise<T | null>;
18
+ /**
19
+ * Set data to the cache.
20
+ * @param key - cache key under which the data is stored
21
+ * @param data - data to be stored in the cache
22
+ * @param ttl - expiration time in seconds
23
+ */
24
+ set<T>(key: string, data: T, ttl?: number): Promise<void>;
25
+ /**
26
+ * Delete data from the cache.
27
+ * Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
28
+ *
29
+ * @param key - cache key
30
+ */
31
+ invalidate(key: string): Promise<void>;
32
+ /**
33
+ * Delete the entire cache.
34
+ */
35
+ clear(): Promise<void>;
36
+ }
37
+ export default InMemoryCacheService;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var DEFAULT_TTL = 30; // seconds
40
+ /**
41
+ * Class represents basic, in-memory, cache store.
42
+ */
43
+ var InMemoryCacheService = /** @class */ (function () {
44
+ function InMemoryCacheService(deps, options) {
45
+ if (options === void 0) { options = {}; }
46
+ var _a;
47
+ this.store = new Map();
48
+ this.timoutRefs = new Map();
49
+ this.TTL = (_a = options.ttl) !== null && _a !== void 0 ? _a : DEFAULT_TTL;
50
+ }
51
+ /**
52
+ * Retrieve data from the cache.
53
+ * @param key - cache key
54
+ */
55
+ InMemoryCacheService.prototype.get = function (key) {
56
+ var _a;
57
+ return __awaiter(this, void 0, void 0, function () {
58
+ var now, record, recordExpire;
59
+ return __generator(this, function (_b) {
60
+ now = Date.now();
61
+ record = this.store.get(key);
62
+ recordExpire = (_a = record === null || record === void 0 ? void 0 : record.expire) !== null && _a !== void 0 ? _a : Infinity;
63
+ if (!record || recordExpire < now) {
64
+ return [2 /*return*/, null];
65
+ }
66
+ return [2 /*return*/, record.data];
67
+ });
68
+ });
69
+ };
70
+ /**
71
+ * Set data to the cache.
72
+ * @param key - cache key under which the data is stored
73
+ * @param data - data to be stored in the cache
74
+ * @param ttl - expiration time in seconds
75
+ */
76
+ InMemoryCacheService.prototype.set = function (key, data, ttl) {
77
+ if (ttl === void 0) { ttl = this.TTL; }
78
+ return __awaiter(this, void 0, void 0, function () {
79
+ var record, oldRecord, ref;
80
+ var _this = this;
81
+ return __generator(this, function (_a) {
82
+ record = { data: data, expire: ttl * 1000 + Date.now() };
83
+ oldRecord = this.store.get(key);
84
+ if (oldRecord) {
85
+ clearTimeout(this.timoutRefs.get(key));
86
+ this.timoutRefs.delete(key);
87
+ }
88
+ ref = setTimeout(function () {
89
+ _this.invalidate(key);
90
+ }, ttl * 1000);
91
+ this.timoutRefs.set(key, ref);
92
+ this.store.set(key, record);
93
+ return [2 /*return*/];
94
+ });
95
+ });
96
+ };
97
+ /**
98
+ * Delete data from the cache.
99
+ * Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
100
+ *
101
+ * @param key - cache key
102
+ */
103
+ InMemoryCacheService.prototype.invalidate = function (key) {
104
+ return __awaiter(this, void 0, void 0, function () {
105
+ var keys, regExp_1;
106
+ var _this = this;
107
+ return __generator(this, function (_a) {
108
+ keys = [key];
109
+ if (key.includes("*")) {
110
+ regExp_1 = new RegExp(key.replace("*", ".*"));
111
+ keys = Array.from(this.store.keys()).filter(function (k) { return k.match(regExp_1); });
112
+ }
113
+ keys.forEach(function (key) {
114
+ var timeoutRef = _this.timoutRefs.get(key);
115
+ if (timeoutRef) {
116
+ clearTimeout(timeoutRef);
117
+ _this.timoutRefs.delete(key);
118
+ }
119
+ _this.store.delete(key);
120
+ });
121
+ return [2 /*return*/];
122
+ });
123
+ });
124
+ };
125
+ /**
126
+ * Delete the entire cache.
127
+ */
128
+ InMemoryCacheService.prototype.clear = function () {
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ return __generator(this, function (_a) {
131
+ this.timoutRefs.forEach(function (ref) { return clearTimeout(ref); });
132
+ this.timoutRefs.clear();
133
+ this.store.clear();
134
+ return [2 /*return*/];
135
+ });
136
+ });
137
+ };
138
+ return InMemoryCacheService;
139
+ }());
140
+ exports.default = InMemoryCacheService;
141
+ //# sourceMappingURL=inmemory-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inmemory-cache.js","sourceRoot":"","sources":["../../src/services/inmemory-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,IAAM,WAAW,GAAG,EAAE,CAAA,CAAC,UAAU;AAIjC;;GAEG;AACH;IAME,8BACE,IAA0B,EAC1B,OAAwC;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Ae,oBAAoB,CAAA"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Shape of a record saved in `in-memory` cache
3
+ */
4
+ export declare type CacheRecord<T> = {
5
+ data: T;
6
+ /**
7
+ * Timestamp in milliseconds
8
+ */
9
+ expire: number;
10
+ };
11
+ export declare type InMemoryCacheModuleOptions = {
12
+ /**
13
+ * Time to keep data in cache (in seconds)
14
+ */
15
+ ttl?: number;
16
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@medusajs/cache-inmemory",
3
+ "version": "2.0.0-next-20230310141356",
4
+ "description": "In-memory Cache Module for Medusa",
5
+ "main": "dist/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/medusajs/medusa",
9
+ "directory": "packages/cache-inmemory"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "author": "Medusa",
18
+ "license": "MIT",
19
+ "devDependencies": {
20
+ "@medusajs/medusa": "2.0.0-next-20230310141356",
21
+ "cross-env": "^5.2.1",
22
+ "jest": "^25.5.4",
23
+ "ts-jest": "^25.5.1",
24
+ "typescript": "^4.4.4"
25
+ },
26
+ "scripts": {
27
+ "watch": "tsc --build --watch",
28
+ "prepare": "cross-env NODE_ENV=production yarn run build",
29
+ "build": "tsc --build",
30
+ "test": "jest --passWithNoTests",
31
+ "test:unit": "jest --passWithNoTests"
32
+ },
33
+ "peerDependencies": {
34
+ "@medusajs/medusa": "2.0.0-next-20230310141356"
35
+ }
36
+ }