@medusajs/cache-inmemory 2.0.0-20230320210331
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +23 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +9 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/inmemory-cache.d.ts +37 -0
- package/dist/services/inmemory-cache.js +142 -0
- package/dist/services/inmemory-cache.js.map +1 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +36 -0
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)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,12 @@
|
|
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 service = inmemory_cache_1.default;
|
8
|
+
var moduleDefinition = {
|
9
|
+
service: service,
|
10
|
+
};
|
11
|
+
exports.default = moduleDefinition;
|
12
|
+
//# 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,wBAAoB,CAAA;AAEpC,IAAM,gBAAgB,GAAkB;IACtC,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,142 @@
|
|
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
|
+
ref.unref();
|
92
|
+
this.timoutRefs.set(key, ref);
|
93
|
+
this.store.set(key, record);
|
94
|
+
return [2 /*return*/];
|
95
|
+
});
|
96
|
+
});
|
97
|
+
};
|
98
|
+
/**
|
99
|
+
* Delete data from the cache.
|
100
|
+
* Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
|
101
|
+
*
|
102
|
+
* @param key - cache key
|
103
|
+
*/
|
104
|
+
InMemoryCacheService.prototype.invalidate = function (key) {
|
105
|
+
return __awaiter(this, void 0, void 0, function () {
|
106
|
+
var keys, regExp_1;
|
107
|
+
var _this = this;
|
108
|
+
return __generator(this, function (_a) {
|
109
|
+
keys = [key];
|
110
|
+
if (key.includes("*")) {
|
111
|
+
regExp_1 = new RegExp(key.replace("*", ".*"));
|
112
|
+
keys = Array.from(this.store.keys()).filter(function (k) { return k.match(regExp_1); });
|
113
|
+
}
|
114
|
+
keys.forEach(function (key) {
|
115
|
+
var timeoutRef = _this.timoutRefs.get(key);
|
116
|
+
if (timeoutRef) {
|
117
|
+
clearTimeout(timeoutRef);
|
118
|
+
_this.timoutRefs.delete(key);
|
119
|
+
}
|
120
|
+
_this.store.delete(key);
|
121
|
+
});
|
122
|
+
return [2 /*return*/];
|
123
|
+
});
|
124
|
+
});
|
125
|
+
};
|
126
|
+
/**
|
127
|
+
* Delete the entire cache.
|
128
|
+
*/
|
129
|
+
InMemoryCacheService.prototype.clear = function () {
|
130
|
+
return __awaiter(this, void 0, void 0, function () {
|
131
|
+
return __generator(this, function (_a) {
|
132
|
+
this.timoutRefs.forEach(function (ref) { return clearTimeout(ref); });
|
133
|
+
this.timoutRefs.clear();
|
134
|
+
this.store.clear();
|
135
|
+
return [2 /*return*/];
|
136
|
+
});
|
137
|
+
});
|
138
|
+
};
|
139
|
+
return InMemoryCacheService;
|
140
|
+
}());
|
141
|
+
exports.default = InMemoryCacheService;
|
142
|
+
//# 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,EA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B;IACH,2BAAC;AAAD,CAAC,AAzFD,IAyFC;AAED,kBAAe,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 @@
|
|
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-20230320210331",
|
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-20230320210331",
|
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-20230320210331"
|
35
|
+
}
|
36
|
+
}
|