|
1
|
import { ICacheService } from "@medusajs/framework/types" |
|
2
|
import { CacheRecord, InMemoryCacheModuleOptions } from "../types" |
|
3
|
|
|
4
|
const DEFAULT_TTL = 30 // seconds |
|
5
|
|
|
6
|
type InjectedDependencies = {} |
|
7
|
|
|
8
|
/** |
|
9
|
* Class represents basic, in-memory, cache store. |
|
10
|
*/ |
|
11
|
class InMemoryCacheService implements ICacheService { |
|
12
|
protected readonly TTL: number |
|
13
|
|
|
14
|
protected readonly store = new Map<string, CacheRecord<any>>() |
|
15
|
protected readonly timoutRefs = new Map<string, NodeJS.Timeout>() |
|
16
|
|
|
17
|
constructor( |
|
18
|
deps: InjectedDependencies, |
|
19
|
options: InMemoryCacheModuleOptions = {} |
|
20
|
) { |
|
21
|
this.TTL = options.ttl ?? DEFAULT_TTL |
|
22
|
} |
|
23
|
|
|
24
|
/** |
|
25
|
* Retrieve data from the cache. |
|
26
|
* @param key - cache key |
|
27
|
*/ |
|
28
|
async get<T>(key: string): Promise<T | null> { |
|
29
|
const now = Date.now() |
|
30
|
const record: CacheRecord<T> | undefined = this.store.get(key) |
|
31
|
|
|
32
|
const recordExpire = record?.expire ?? Infinity |
|
33
|
|
|
34
|
if (!record || recordExpire < now) { |
|
35
|
return null |
|
36
|
} |
|
37
|
|
|
38
|
return record.data |
|
39
|
} |
|
40
|
|
|
41
|
/** |
|
42
|
* Set data to the cache. |
|
43
|
* @param key - cache key under which the data is stored |
|
44
|
* @param data - data to be stored in the cache |
|
45
|
* @param ttl - expiration time in seconds |
|
46
|
*/ |
|
47
|
async set<T>(key: string, data: T, ttl: number = this.TTL): Promise<void> { |
|
48
|
if (ttl === 0) { |
|
49
|
return |
|
50
|
} |
|
51
|
|
|
52
|
const record: CacheRecord<T> = { data, expire: ttl * 1000 + Date.now() } |
|
53
|
|
|
54
|
const oldRecord = this.store.get(key) |
|
55
|
|
|
56
|
if (oldRecord) { |
|
57
|
clearTimeout(this.timoutRefs.get(key)) |
|
58
|
this.timoutRefs.delete(key) |
|
59
|
} |
|
60
|
|
|
61
|
const ref = setTimeout(async () => { |
|
62
|
await this.invalidate(key) |
|
63
|
}, ttl * 1000) |
|
64
|
|
|
65
|
ref.unref() |
|
66
|
|
|
67
|
this.timoutRefs.set(key, ref) |
|
68
|
this.store.set(key, record) |
|
69
|
} |
|
70
|
|
|
71
|
/** |
|
72
|
* Delete data from the cache. |
|
73
|
* Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:" |
|
74
|
* |
|
75
|
* @param key - cache key |
|
76
|
*/ |
|
77
|
async invalidate(key: string): Promise<void> { |
|
78
|
let keys = [key] |
|
79
|
|
|
80
|
if (key.includes("*")) { |
|
81
|
const regExp = new RegExp(key.replace("*", ".*")) |
|
82
|
keys = Array.from(this.store.keys()).filter((k) => k.match(regExp)) |
|
83
|
} |
|
84
|
|
|
85
|
keys.forEach((key) => { |
|
86
|
const timeoutRef = this.timoutRefs.get(key) |
|
87
|
if (timeoutRef) { |
|
88
|
clearTimeout(timeoutRef) |
|
89
|
this.timoutRefs.delete(key) |
|
90
|
} |
|
91
|
this.store.delete(key) |
|
92
|
}) |
|
93
|
} |
|
94
|
|
|
95
|
/** |
|
96
|
* Delete the entire cache. |
|
97
|
*/ |
|
98
|
async clear() { |
|
99
|
this.timoutRefs.forEach((ref) => clearTimeout(ref)) |
|
100
|
this.timoutRefs.clear() |
|
101
|
|
|
102
|
this.store.clear() |
|
103
|
} |
|
104
|
} |
|
105
|
|
|
106
|
export default InMemoryCacheService |