@medusajs/cache-inmemory 2.0.0-snapshot-20230309082112
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +0 -0
- package/CHANGELOG.md +13 -0
- package/README.md +20 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +14 -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 +36 -0
- package/dist/services/inmemory-cache.js +128 -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/jest.config.js +13 -0
- package/package.json +37 -0
- package/src/index.ts +13 -0
- package/src/services/__tests__/inmemory-cache.js +65 -0
- package/src/services/index.ts +1 -0
- package/src/services/inmemory-cache.ts +90 -0
- package/src/types/index.ts +17 -0
- package/tsconfig.json +32 -0
File without changes
|
package/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# @medusajs/cache-inmemory
|
2
|
+
|
3
|
+
## 2.0.0-snapshot-20230309082112
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- [#3187](https://github.com/medusajs/medusa/pull/3187) [`ec0299135`](https://github.com/medusajs/medusa/commit/ec0299135ebf4ab60888bf817ec2fee06e507d80) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa, cache-redis, cache-inmemory): Added cache modules
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- Updated dependencies [[`d6b1ad1cc`](https://github.com/medusajs/medusa/commit/d6b1ad1ccd9a8f91b169f30ff99492cf3adcccac), [`e143a8697`](https://github.com/medusajs/medusa/commit/e143a86976a5cc6a53b7a795e8486df4db1d1c09), [`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`6323868f6`](https://github.com/medusajs/medusa/commit/6323868f65651f19597e9a02ae83890e2fb378e4), [`84e448968`](https://github.com/medusajs/medusa/commit/84e44896836b2c44017572ac5192ab1cd937048c), [`33c6ccf05`](https://github.com/medusajs/medusa/commit/33c6ccf0591b8ef527f3b10a70b51e29751b4998), [`75924b682`](https://github.com/medusajs/medusa/commit/75924b682f3ff1867f43cf305394f2cfc0b03487), [`240d0ea7b`](https://github.com/medusajs/medusa/commit/240d0ea7b88ff494d0fe28c7c5348e958c14571f), [`6c0462472`](https://github.com/medusajs/medusa/commit/6c046247275c46d934f03d53471bdd555a19a9ad), [`589d1c09b`](https://github.com/medusajs/medusa/commit/589d1c09b085dcaf9667061201ac9deff3047466), [`0a02b70e5`](https://github.com/medusajs/medusa/commit/0a02b70e59cfbd8888fb58c29ee9aaf96eb8099a), [`5eb61fa0e`](https://github.com/medusajs/medusa/commit/5eb61fa0ef991b8a9fabb16c2c159e51b9541867), [`f033711ad`](https://github.com/medusajs/medusa/commit/f033711ad649466d72dd9f673d75848c97c0861f), [`a1e59313c`](https://github.com/medusajs/medusa/commit/a1e59313c964a944a287b54f6654d1d19ac8a59b), [`cbbf3ca05`](https://github.com/medusajs/medusa/commit/cbbf3ca054387a900c5777c2eb0218df2c72bae6), [`12d304307`](https://github.com/medusajs/medusa/commit/12d304307af87ea9287a41869eb33ef09f273d85), [`0a6aa0e62`](https://github.com/medusajs/medusa/commit/0a6aa0e6248dec42e295d3ec0f61322d4d5e375a), [`287c829c9`](https://github.com/medusajs/medusa/commit/287c829c9c5a9797fb8cd118b7a6066ad1935898), [`f43e9f0f2`](https://github.com/medusajs/medusa/commit/f43e9f0f20a8b0637252951b2bdfed4d42fb9f5e), [`842423679`](https://github.com/medusajs/medusa/commit/8424236799c3789f884285cd3c8a491e91aef2ca), [`935870e01`](https://github.com/medusajs/medusa/commit/935870e010af1ec884259b1f1328421e99acc3af), [`57d7728dd`](https://github.com/medusajs/medusa/commit/57d7728dd9d00df712e1a872899b8397955dfe46), [`15f47baf5`](https://github.com/medusajs/medusa/commit/15f47baf56e6722b7821cfaa2fb468e582dfa2c1), [`38503fff5`](https://github.com/medusajs/medusa/commit/38503fff56bbceb092e396ac11432a56967b53e9), [`40de54b01`](https://github.com/medusajs/medusa/commit/40de54b0101bdfd37f577d18c10ec9f1ab1ce8fe), [`478d1af8d`](https://github.com/medusajs/medusa/commit/478d1af8d0df0af16baf4f130e19b0be34f5f295), [`1d09a266b`](https://github.com/medusajs/medusa/commit/1d09a266beacefce56b962924d1e3dd1ca4b693f), [`7d585f5f8`](https://github.com/medusajs/medusa/commit/7d585f5f84a910c02d274df7a489dc3ff1ea273a), [`46547f29c`](https://github.com/medusajs/medusa/commit/46547f29c755719e22d2977c5e5f8ab8a4a7fcae), [`ec0299135`](https://github.com/medusajs/medusa/commit/ec0299135ebf4ab60888bf817ec2fee06e507d80), [`f3bf351d2`](https://github.com/medusajs/medusa/commit/f3bf351d21d1c2a67ed2d603c8b7ed4ae5cbd366), [`d61d6c7b7`](https://github.com/medusajs/medusa/commit/d61d6c7b7f8549996090f5315597d22d2af968f9), [`68496ffe6`](https://github.com/medusajs/medusa/commit/68496ffe6018c378e928e407710157a0aed4bd87), [`53eda215e`](https://github.com/medusajs/medusa/commit/53eda215e00509eb63e571f1b38b9c8884b8e6d5), [`aefe5aa13`](https://github.com/medusajs/medusa/commit/aefe5aa133ea3ab98eb3c1ecd0ba51fb76c173de), [`48ad2426a`](https://github.com/medusajs/medusa/commit/48ad2426aa7a604c226132e85ef3da5cce045f45)]:
|
12
|
+
- @medusajs/medusa@1.8.0-snapshot-20230309082112
|
13
|
+
- medusa-interfaces@1.3.7-snapshot-20230309082112
|
package/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
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
|
+
### Other caching modules
|
20
|
+
- [Medusa Cache Redis](../cache-redis/README.md)
|
package/dist/index.d.ts
ADDED
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,36 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { ConfigurableModuleDeclaration } from "@medusajs/modules-sdk";
|
3
|
+
import { ICacheService } from "@medusajs/medusa";
|
4
|
+
import { CacheRecord, InMemoryCacheModuleOptions } from "../types";
|
5
|
+
declare type InjectedDependencies = {};
|
6
|
+
/**
|
7
|
+
* Class represents basic, in-memory, cache store.
|
8
|
+
*/
|
9
|
+
declare class InMemoryCacheService implements ICacheService {
|
10
|
+
protected TTL: number;
|
11
|
+
protected readonly store: Map<string, CacheRecord<any>>;
|
12
|
+
protected readonly timoutRefs: Map<string, NodeJS.Timeout>;
|
13
|
+
constructor(deps: InjectedDependencies, options?: InMemoryCacheModuleOptions, moduleDeclaration?: ConfigurableModuleDeclaration);
|
14
|
+
/**
|
15
|
+
* Retrieve data from the cache.
|
16
|
+
* @param key - cache key
|
17
|
+
*/
|
18
|
+
get<T>(key: string): Promise<T | null>;
|
19
|
+
/**
|
20
|
+
* Set data to the cache.
|
21
|
+
* @param key - cache key under which the data is stored
|
22
|
+
* @param data - data to be stored in the cache
|
23
|
+
* @param ttl - expiration time in seconds
|
24
|
+
*/
|
25
|
+
set<T>(key: string, data: T, ttl?: number): Promise<void>;
|
26
|
+
/**
|
27
|
+
* Delete data from the cache.
|
28
|
+
* @param key - cache key
|
29
|
+
*/
|
30
|
+
invalidate(key: string): Promise<void>;
|
31
|
+
/**
|
32
|
+
* Delete the entire cache.
|
33
|
+
*/
|
34
|
+
clear(): Promise<void>;
|
35
|
+
}
|
36
|
+
export default InMemoryCacheService;
|
@@ -0,0 +1,128 @@
|
|
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, moduleDeclaration) {
|
45
|
+
if (options === void 0) { options = {}; }
|
46
|
+
this.store = new Map();
|
47
|
+
this.timoutRefs = new Map();
|
48
|
+
this.TTL = options.ttl || DEFAULT_TTL;
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Retrieve data from the cache.
|
52
|
+
* @param key - cache key
|
53
|
+
*/
|
54
|
+
InMemoryCacheService.prototype.get = function (key) {
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
56
|
+
var now, record;
|
57
|
+
return __generator(this, function (_a) {
|
58
|
+
now = Date.now();
|
59
|
+
record = this.store.get(key);
|
60
|
+
if (!record || (record.expire && record.expire < now)) {
|
61
|
+
return [2 /*return*/, null];
|
62
|
+
}
|
63
|
+
return [2 /*return*/, record.data];
|
64
|
+
});
|
65
|
+
});
|
66
|
+
};
|
67
|
+
/**
|
68
|
+
* Set data to the cache.
|
69
|
+
* @param key - cache key under which the data is stored
|
70
|
+
* @param data - data to be stored in the cache
|
71
|
+
* @param ttl - expiration time in seconds
|
72
|
+
*/
|
73
|
+
InMemoryCacheService.prototype.set = function (key, data, ttl) {
|
74
|
+
if (ttl === void 0) { ttl = this.TTL; }
|
75
|
+
return __awaiter(this, void 0, void 0, function () {
|
76
|
+
var record, oldRecord, ref;
|
77
|
+
var _this = this;
|
78
|
+
return __generator(this, function (_a) {
|
79
|
+
record = { data: data, expire: ttl * 1000 + Date.now() };
|
80
|
+
oldRecord = this.store.get(key);
|
81
|
+
if (oldRecord) {
|
82
|
+
clearTimeout(this.timoutRefs.get(key));
|
83
|
+
this.timoutRefs.delete(key);
|
84
|
+
}
|
85
|
+
ref = setTimeout(function () {
|
86
|
+
_this.invalidate(key);
|
87
|
+
}, ttl * 1000);
|
88
|
+
this.timoutRefs.set(key, ref);
|
89
|
+
this.store.set(key, record);
|
90
|
+
return [2 /*return*/];
|
91
|
+
});
|
92
|
+
});
|
93
|
+
};
|
94
|
+
/**
|
95
|
+
* Delete data from the cache.
|
96
|
+
* @param key - cache key
|
97
|
+
*/
|
98
|
+
InMemoryCacheService.prototype.invalidate = function (key) {
|
99
|
+
return __awaiter(this, void 0, void 0, function () {
|
100
|
+
var timeoutRef;
|
101
|
+
return __generator(this, function (_a) {
|
102
|
+
timeoutRef = this.timoutRefs.get(key);
|
103
|
+
if (timeoutRef) {
|
104
|
+
clearTimeout(timeoutRef);
|
105
|
+
this.timoutRefs.delete(key);
|
106
|
+
}
|
107
|
+
this.store.delete(key);
|
108
|
+
return [2 /*return*/];
|
109
|
+
});
|
110
|
+
});
|
111
|
+
};
|
112
|
+
/**
|
113
|
+
* Delete the entire cache.
|
114
|
+
*/
|
115
|
+
InMemoryCacheService.prototype.clear = function () {
|
116
|
+
return __awaiter(this, void 0, void 0, function () {
|
117
|
+
return __generator(this, function (_a) {
|
118
|
+
this.timoutRefs.forEach(function (ref) { return clearTimeout(ref); });
|
119
|
+
this.timoutRefs.clear();
|
120
|
+
this.store.clear();
|
121
|
+
return [2 /*return*/];
|
122
|
+
});
|
123
|
+
});
|
124
|
+
};
|
125
|
+
return InMemoryCacheService;
|
126
|
+
}());
|
127
|
+
exports.default = InMemoryCacheService;
|
128
|
+
//# sourceMappingURL=inmemory-cache.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"inmemory-cache.js","sourceRoot":"","sources":["../../src/services/inmemory-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,IAAM,WAAW,GAAG,EAAE,CAAA,CAAC,UAAU;AAIjC;;GAEG;AACH;IAME,8BACE,IAA0B,EAC1B,OAAwC,EACxC,iBAAiD;QADjD,wBAAA,EAAA,YAAwC;QALvB,UAAK,GAAG,IAAI,GAAG,EAA4B,CAAA;QAC3C,eAAU,GAAG,IAAI,GAAG,EAA0B,CAAA;QAO/D,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,WAAW,CAAA;IACvC,CAAC;IAED;;;OAGG;IACG,kCAAG,GAAT,UAAa,GAAW;;;;gBAChB,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;gBAChB,MAAM,GAA+B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;gBAE9D,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,EAAE;oBACrD,sBAAO,IAAI,EAAA;iBACZ;gBAED,sBAAO,MAAM,CAAC,IAAI,EAAA;;;KACnB;IAED;;;;;OAKG;IACG,kCAAG,GAAT,UAAa,GAAW,EAAE,IAAO,EAAE,GAAsB;QAAtB,oBAAA,EAAA,MAAc,IAAI,CAAC,GAAG;;;;;gBACjD,MAAM,GAAmB,EAAE,IAAI,MAAA,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,EAAE,CAAA;gBAElE,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;gBAErC,IAAI,SAAS,EAAE;oBACb,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;oBACtC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;iBAC5B;gBAEK,GAAG,GAAG,UAAU,CAAC;oBACrB,KAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;gBACtB,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,CAAA;gBAEd,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;gBAC7B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;;;;KAC5B;IAED;;;OAGG;IACG,yCAAU,GAAhB,UAAiB,GAAW;;;;gBACpB,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;gBAC3C,IAAI,UAAU,EAAE;oBACd,YAAY,CAAC,UAAU,CAAC,CAAA;oBACxB,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;iBAC5B;gBACD,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;;;;KACvB;IAED;;OAEG;IACG,oCAAK,GAAX;;;gBACE,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,UAAC,GAAG,IAAK,OAAA,YAAY,CAAC,GAAG,CAAC,EAAjB,CAAiB,CAAC,CAAA;gBACnD,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAA;gBAEvB,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;;;;KACnB;IACH,2BAAC;AAAD,CAAC,AA3ED,IA2EC;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/jest.config.js
ADDED
package/package.json
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
{
|
2
|
+
"name": "@medusajs/cache-inmemory",
|
3
|
+
"version": "2.0.0-snapshot-20230309082112",
|
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
|
+
"files": [
|
14
|
+
"dist"
|
15
|
+
]
|
16
|
+
},
|
17
|
+
"author": "Medusa",
|
18
|
+
"license": "MIT",
|
19
|
+
"devDependencies": {
|
20
|
+
"@medusajs/medusa": "1.8.0-snapshot-20230309082112",
|
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": "1.8.0-snapshot-20230309082112",
|
35
|
+
"medusa-interfaces": "1.3.7-snapshot-20230309082112"
|
36
|
+
}
|
37
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
import { ModuleExports } from "@medusajs/modules-sdk"
|
2
|
+
|
3
|
+
import InMemoryCacheService from "./services/inmemory-cache"
|
4
|
+
|
5
|
+
const loaders = []
|
6
|
+
const service = InMemoryCacheService
|
7
|
+
|
8
|
+
const moduleDefinition: ModuleExports = {
|
9
|
+
service,
|
10
|
+
loaders,
|
11
|
+
}
|
12
|
+
|
13
|
+
export default moduleDefinition
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import { InMemoryCacheService } from "../index"
|
2
|
+
|
3
|
+
jest.setTimeout(40000)
|
4
|
+
|
5
|
+
describe("InMemoryCacheService", () => {
|
6
|
+
let inMemoryCache
|
7
|
+
beforeAll(() => {
|
8
|
+
jest.resetAllMocks()
|
9
|
+
})
|
10
|
+
|
11
|
+
it("Creates an `InMemoryCacheService`", () => {
|
12
|
+
inMemoryCache = new InMemoryCacheService({}, { ttl: 10 })
|
13
|
+
})
|
14
|
+
|
15
|
+
it("Stores and retrieves data", async () => {
|
16
|
+
inMemoryCache = new InMemoryCacheService({}, {})
|
17
|
+
|
18
|
+
await inMemoryCache.set("cache-key", { data: "value" })
|
19
|
+
|
20
|
+
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
|
21
|
+
})
|
22
|
+
|
23
|
+
it("Invalidates data", async () => {
|
24
|
+
inMemoryCache = new InMemoryCacheService({}, {})
|
25
|
+
|
26
|
+
await inMemoryCache.set("cache-key", { data: "value" })
|
27
|
+
|
28
|
+
await inMemoryCache.invalidate("cache-key")
|
29
|
+
|
30
|
+
expect(await inMemoryCache.get("cache-key")).toEqual(null)
|
31
|
+
})
|
32
|
+
|
33
|
+
it("Removes data after TTL", async () => {
|
34
|
+
inMemoryCache = new InMemoryCacheService({}, {})
|
35
|
+
|
36
|
+
await inMemoryCache.set("cache-key", { data: "value" }, 2)
|
37
|
+
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
|
38
|
+
|
39
|
+
await new Promise((res) => setTimeout(res, 3000))
|
40
|
+
|
41
|
+
expect(await inMemoryCache.get("cache-key")).toEqual(null)
|
42
|
+
})
|
43
|
+
|
44
|
+
it("Removes data after default TTL if TTL params isn't passed", async () => {
|
45
|
+
inMemoryCache = new InMemoryCacheService({}, { ttl: 1 })
|
46
|
+
|
47
|
+
await inMemoryCache.set("cache-key", { data: "value" })
|
48
|
+
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
|
49
|
+
|
50
|
+
await new Promise((res) => setTimeout(res, 2000))
|
51
|
+
|
52
|
+
expect(await inMemoryCache.get("cache-key")).toEqual(null)
|
53
|
+
})
|
54
|
+
|
55
|
+
it("Removes data after TTL from the config if TTL params isn't passed", async () => {
|
56
|
+
inMemoryCache = new InMemoryCacheService({}, { ttl: 1 })
|
57
|
+
|
58
|
+
await inMemoryCache.set("cache-key", { data: "value" })
|
59
|
+
expect(await inMemoryCache.get("cache-key")).toEqual({ data: "value" })
|
60
|
+
|
61
|
+
await new Promise((res) => setTimeout(res, 2000))
|
62
|
+
|
63
|
+
expect(await inMemoryCache.get("cache-key")).toEqual(null)
|
64
|
+
})
|
65
|
+
})
|
@@ -0,0 +1 @@
|
|
1
|
+
export { default as InMemoryCacheService } from "./inmemory-cache"
|
@@ -0,0 +1,90 @@
|
|
1
|
+
import { ConfigurableModuleDeclaration } from "@medusajs/modules-sdk"
|
2
|
+
import { ICacheService } from "@medusajs/medusa"
|
3
|
+
|
4
|
+
import { CacheRecord, InMemoryCacheModuleOptions } from "../types"
|
5
|
+
|
6
|
+
const DEFAULT_TTL = 30 // seconds
|
7
|
+
|
8
|
+
type InjectedDependencies = {}
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Class represents basic, in-memory, cache store.
|
12
|
+
*/
|
13
|
+
class InMemoryCacheService implements ICacheService {
|
14
|
+
protected TTL: number
|
15
|
+
|
16
|
+
protected readonly store = new Map<string, CacheRecord<any>>()
|
17
|
+
protected readonly timoutRefs = new Map<string, NodeJS.Timeout>()
|
18
|
+
|
19
|
+
constructor(
|
20
|
+
deps: InjectedDependencies,
|
21
|
+
options: InMemoryCacheModuleOptions = {},
|
22
|
+
moduleDeclaration?: ConfigurableModuleDeclaration
|
23
|
+
) {
|
24
|
+
this.TTL = options.ttl || DEFAULT_TTL
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Retrieve data from the cache.
|
29
|
+
* @param key - cache key
|
30
|
+
*/
|
31
|
+
async get<T>(key: string): Promise<T | null> {
|
32
|
+
const now = Date.now()
|
33
|
+
const record: CacheRecord<T> | undefined = this.store.get(key)
|
34
|
+
|
35
|
+
if (!record || (record.expire && record.expire < now)) {
|
36
|
+
return null
|
37
|
+
}
|
38
|
+
|
39
|
+
return record.data
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Set data to the cache.
|
44
|
+
* @param key - cache key under which the data is stored
|
45
|
+
* @param data - data to be stored in the cache
|
46
|
+
* @param ttl - expiration time in seconds
|
47
|
+
*/
|
48
|
+
async set<T>(key: string, data: T, ttl: number = this.TTL): Promise<void> {
|
49
|
+
const record: CacheRecord<T> = { data, expire: ttl * 1000 + Date.now() }
|
50
|
+
|
51
|
+
const oldRecord = this.store.get(key)
|
52
|
+
|
53
|
+
if (oldRecord) {
|
54
|
+
clearTimeout(this.timoutRefs.get(key))
|
55
|
+
this.timoutRefs.delete(key)
|
56
|
+
}
|
57
|
+
|
58
|
+
const ref = setTimeout(() => {
|
59
|
+
this.invalidate(key)
|
60
|
+
}, ttl * 1000)
|
61
|
+
|
62
|
+
this.timoutRefs.set(key, ref)
|
63
|
+
this.store.set(key, record)
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Delete data from the cache.
|
68
|
+
* @param key - cache key
|
69
|
+
*/
|
70
|
+
async invalidate(key: string): Promise<void> {
|
71
|
+
const timeoutRef = this.timoutRefs.get(key)
|
72
|
+
if (timeoutRef) {
|
73
|
+
clearTimeout(timeoutRef)
|
74
|
+
this.timoutRefs.delete(key)
|
75
|
+
}
|
76
|
+
this.store.delete(key)
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Delete the entire cache.
|
81
|
+
*/
|
82
|
+
async clear() {
|
83
|
+
this.timoutRefs.forEach((ref) => clearTimeout(ref))
|
84
|
+
this.timoutRefs.clear()
|
85
|
+
|
86
|
+
this.store.clear()
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
export default InMemoryCacheService
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/**
|
2
|
+
* Shape of a record saved in `in-memory` cache
|
3
|
+
*/
|
4
|
+
export type CacheRecord<T> = {
|
5
|
+
data: T
|
6
|
+
/**
|
7
|
+
* Timestamp in milliseconds
|
8
|
+
*/
|
9
|
+
expire: number
|
10
|
+
}
|
11
|
+
|
12
|
+
export type InMemoryCacheModuleOptions = {
|
13
|
+
/**
|
14
|
+
* Time to keep data in cache (in seconds)
|
15
|
+
*/
|
16
|
+
ttl?: number
|
17
|
+
}
|
package/tsconfig.json
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"lib": [
|
4
|
+
"es5",
|
5
|
+
"es6",
|
6
|
+
"es2019"
|
7
|
+
],
|
8
|
+
"target": "es5",
|
9
|
+
"outDir": "./dist",
|
10
|
+
"esModuleInterop": true,
|
11
|
+
"declaration": true,
|
12
|
+
"module": "commonjs",
|
13
|
+
"moduleResolution": "node",
|
14
|
+
"emitDecoratorMetadata": true,
|
15
|
+
"experimentalDecorators": true,
|
16
|
+
"sourceMap": true,
|
17
|
+
"noImplicitReturns": true,
|
18
|
+
"strictNullChecks": true,
|
19
|
+
"strictFunctionTypes": true,
|
20
|
+
"noImplicitThis": true,
|
21
|
+
"allowJs": true,
|
22
|
+
"skipLibCheck": true,
|
23
|
+
"downlevelIteration": true // to use ES5 specific tooling
|
24
|
+
},
|
25
|
+
"include": ["./src/**/*", "index.d.ts"],
|
26
|
+
"exclude": [
|
27
|
+
"./dist/**/*",
|
28
|
+
"./src/**/__tests__",
|
29
|
+
"./src/**/__mocks__",
|
30
|
+
"node_modules"
|
31
|
+
]
|
32
|
+
}
|