@medusajs/cache-inmemory 2.0.0-snapshot-20230309082112 → 2.0.0-snapshot-20230313175443
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +3 -0
- package/dist/services/inmemory-cache.d.ts +4 -3
- package/dist/services/inmemory-cache.js +24 -11
- package/dist/services/inmemory-cache.js.map +1 -1
- package/package.json +7 -8
- package/.turbo/turbo-build.log +0 -0
- package/CHANGELOG.md +0 -13
- package/jest.config.js +0 -13
- package/src/index.ts +0 -13
- package/src/services/__tests__/inmemory-cache.js +0 -65
- package/src/services/index.ts +0 -1
- package/src/services/inmemory-cache.ts +0 -90
- package/src/types/index.ts +0 -17
- package/tsconfig.json +0 -32
package/README.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
import { ConfigurableModuleDeclaration } from "@medusajs/modules-sdk";
|
3
2
|
import { ICacheService } from "@medusajs/medusa";
|
4
3
|
import { CacheRecord, InMemoryCacheModuleOptions } from "../types";
|
5
4
|
declare type InjectedDependencies = {};
|
@@ -7,10 +6,10 @@ declare type InjectedDependencies = {};
|
|
7
6
|
* Class represents basic, in-memory, cache store.
|
8
7
|
*/
|
9
8
|
declare class InMemoryCacheService implements ICacheService {
|
10
|
-
protected TTL: number;
|
9
|
+
protected readonly TTL: number;
|
11
10
|
protected readonly store: Map<string, CacheRecord<any>>;
|
12
11
|
protected readonly timoutRefs: Map<string, NodeJS.Timeout>;
|
13
|
-
constructor(deps: InjectedDependencies, options?: InMemoryCacheModuleOptions
|
12
|
+
constructor(deps: InjectedDependencies, options?: InMemoryCacheModuleOptions);
|
14
13
|
/**
|
15
14
|
* Retrieve data from the cache.
|
16
15
|
* @param key - cache key
|
@@ -25,6 +24,8 @@ declare class InMemoryCacheService implements ICacheService {
|
|
25
24
|
set<T>(key: string, data: T, ttl?: number): Promise<void>;
|
26
25
|
/**
|
27
26
|
* Delete data from the cache.
|
27
|
+
* Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
|
28
|
+
*
|
28
29
|
* @param key - cache key
|
29
30
|
*/
|
30
31
|
invalidate(key: string): Promise<void>;
|
@@ -41,23 +41,26 @@ var DEFAULT_TTL = 30; // seconds
|
|
41
41
|
* Class represents basic, in-memory, cache store.
|
42
42
|
*/
|
43
43
|
var InMemoryCacheService = /** @class */ (function () {
|
44
|
-
function InMemoryCacheService(deps, options
|
44
|
+
function InMemoryCacheService(deps, options) {
|
45
45
|
if (options === void 0) { options = {}; }
|
46
|
+
var _a;
|
46
47
|
this.store = new Map();
|
47
48
|
this.timoutRefs = new Map();
|
48
|
-
this.TTL = options.ttl
|
49
|
+
this.TTL = (_a = options.ttl) !== null && _a !== void 0 ? _a : DEFAULT_TTL;
|
49
50
|
}
|
50
51
|
/**
|
51
52
|
* Retrieve data from the cache.
|
52
53
|
* @param key - cache key
|
53
54
|
*/
|
54
55
|
InMemoryCacheService.prototype.get = function (key) {
|
56
|
+
var _a;
|
55
57
|
return __awaiter(this, void 0, void 0, function () {
|
56
|
-
var now, record;
|
57
|
-
return __generator(this, function (
|
58
|
+
var now, record, recordExpire;
|
59
|
+
return __generator(this, function (_b) {
|
58
60
|
now = Date.now();
|
59
61
|
record = this.store.get(key);
|
60
|
-
|
62
|
+
recordExpire = (_a = record === null || record === void 0 ? void 0 : record.expire) !== null && _a !== void 0 ? _a : Infinity;
|
63
|
+
if (!record || recordExpire < now) {
|
61
64
|
return [2 /*return*/, null];
|
62
65
|
}
|
63
66
|
return [2 /*return*/, record.data];
|
@@ -93,18 +96,28 @@ var InMemoryCacheService = /** @class */ (function () {
|
|
93
96
|
};
|
94
97
|
/**
|
95
98
|
* Delete data from the cache.
|
99
|
+
* Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
|
100
|
+
*
|
96
101
|
* @param key - cache key
|
97
102
|
*/
|
98
103
|
InMemoryCacheService.prototype.invalidate = function (key) {
|
99
104
|
return __awaiter(this, void 0, void 0, function () {
|
100
|
-
var
|
105
|
+
var keys, regExp_1;
|
106
|
+
var _this = this;
|
101
107
|
return __generator(this, function (_a) {
|
102
|
-
|
103
|
-
if (
|
104
|
-
|
105
|
-
this.
|
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); });
|
106
112
|
}
|
107
|
-
|
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
|
+
});
|
108
121
|
return [2 /*return*/];
|
109
122
|
});
|
110
123
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"inmemory-cache.js","sourceRoot":"","sources":["../../src/services/inmemory-cache.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
1
|
+
{"version":3,"file":"inmemory-cache.js","sourceRoot":"","sources":["../../src/services/inmemory-cache.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/cache-inmemory",
|
3
|
-
"version": "2.0.0-snapshot-
|
3
|
+
"version": "2.0.0-snapshot-20230313175443",
|
4
4
|
"description": "In-memory Cache Module for Medusa",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"repository": {
|
@@ -9,15 +9,15 @@
|
|
9
9
|
"directory": "packages/cache-inmemory"
|
10
10
|
},
|
11
11
|
"publishConfig": {
|
12
|
-
"access": "public"
|
13
|
-
"files": [
|
14
|
-
"dist"
|
15
|
-
]
|
12
|
+
"access": "public"
|
16
13
|
},
|
14
|
+
"files": [
|
15
|
+
"dist"
|
16
|
+
],
|
17
17
|
"author": "Medusa",
|
18
18
|
"license": "MIT",
|
19
19
|
"devDependencies": {
|
20
|
-
"@medusajs/medusa": "
|
20
|
+
"@medusajs/medusa": "2.0.0-snapshot-20230313175443",
|
21
21
|
"cross-env": "^5.2.1",
|
22
22
|
"jest": "^25.5.4",
|
23
23
|
"ts-jest": "^25.5.1",
|
@@ -31,7 +31,6 @@
|
|
31
31
|
"test:unit": "jest --passWithNoTests"
|
32
32
|
},
|
33
33
|
"peerDependencies": {
|
34
|
-
"@medusajs/medusa": "
|
35
|
-
"medusa-interfaces": "1.3.7-snapshot-20230309082112"
|
34
|
+
"@medusajs/medusa": "2.0.0-snapshot-20230313175443"
|
36
35
|
}
|
37
36
|
}
|
package/.turbo/turbo-build.log
DELETED
File without changes
|
package/CHANGELOG.md
DELETED
@@ -1,13 +0,0 @@
|
|
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/jest.config.js
DELETED
package/src/index.ts
DELETED
@@ -1,13 +0,0 @@
|
|
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
|
@@ -1,65 +0,0 @@
|
|
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
|
-
})
|
package/src/services/index.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export { default as InMemoryCacheService } from "./inmemory-cache"
|
@@ -1,90 +0,0 @@
|
|
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
|
package/src/types/index.ts
DELETED
@@ -1,17 +0,0 @@
|
|
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
DELETED
@@ -1,32 +0,0 @@
|
|
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
|
-
}
|