@open-mercato/cache 0.11.1-develop.8593.1.50784dae35 → 0.11.1-develop.8665.1.2455b2503c
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/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +27 -21
- package/README.md +30 -0
- package/dist/codec.js +1 -74
- package/dist/codec.js.map +2 -2
- package/dist/defaults.js +1 -6
- package/dist/defaults.js.map +2 -2
- package/dist/errors.js +1 -12
- package/dist/errors.js.map +2 -2
- package/dist/index.js.map +1 -1
- package/dist/patterns.js +1 -35
- package/dist/patterns.js.map +2 -2
- package/dist/provider.js +440 -0
- package/dist/provider.js.map +7 -0
- package/dist/redisConnection.js +2 -0
- package/dist/redisConnection.js.map +7 -0
- package/dist/service.js +320 -80
- package/dist/service.js.map +3 -3
- package/dist/strategies/jsonfile.js +1 -186
- package/dist/strategies/jsonfile.js.map +3 -3
- package/dist/strategies/memory.js +1 -196
- package/dist/strategies/memory.js.map +3 -3
- package/dist/strategies/redis.js +1 -443
- package/dist/strategies/redis.js.map +3 -3
- package/dist/strategies/sqlite.js +1 -196
- package/dist/strategies/sqlite.js.map +3 -3
- package/dist/testing/conformance.js +448 -0
- package/dist/testing/conformance.js.map +7 -0
- package/dist/testing/index.js +12 -0
- package/dist/testing/index.js.map +7 -0
- package/dist/testing/remote.js +182 -0
- package/dist/testing/remote.js.map +7 -0
- package/fixtures/build-conformance-host.mjs +58 -0
- package/fixtures/cache-provider-fixture/index.d.ts +18 -0
- package/fixtures/cache-provider-fixture/index.js +307 -0
- package/fixtures/cache-provider-fixture/package.json +25 -0
- package/fixtures/conformance-child.mjs +41 -0
- package/jest.config.cjs +3 -1
- package/package.json +10 -2
- package/src/__tests__/cache-di-contract-docs.test.ts +2 -2
- package/src/__tests__/conformance.memory-provider.test.ts +77 -0
- package/src/__tests__/provider-fixture.packed.test.ts +178 -0
- package/src/__tests__/provider.test.ts +469 -0
- package/src/__tests__/service.provider.test.ts +513 -0
- package/src/__tests__/service.test.ts +94 -3
- package/src/codec.ts +1 -94
- package/src/defaults.ts +1 -2
- package/src/errors.ts +1 -14
- package/src/index.ts +4 -0
- package/src/patterns.ts +1 -40
- package/src/provider.ts +641 -0
- package/src/redisConnection.ts +1 -0
- package/src/service.ts +396 -89
- package/src/strategies/jsonfile.ts +1 -242
- package/src/strategies/memory.ts +1 -275
- package/src/strategies/redis.ts +1 -615
- package/src/strategies/sqlite.ts +1 -292
- package/src/testing/conformance.ts +569 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/remote.ts +240 -0
- package/src/types.ts +8 -106
|
@@ -1,242 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import path from 'node:path'
|
|
4
|
-
import { DEFAULT_JSON_FILE_CACHE_PATH } from '../defaults'
|
|
5
|
-
import { matchCacheKeyPattern } from '../patterns'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* JSON file cache strategy with tag support
|
|
9
|
-
* Persistent across process restarts, stored in JSON files
|
|
10
|
-
* Simple and requires no external dependencies, but not suitable for high-performance scenarios
|
|
11
|
-
*/
|
|
12
|
-
export function createJsonFileStrategy(filePath?: string, options?: { defaultTtl?: number }): CacheStrategy {
|
|
13
|
-
const defaultTtl = options?.defaultTtl
|
|
14
|
-
const cacheFile = filePath || process.env.CACHE_JSON_FILE_PATH || DEFAULT_JSON_FILE_CACHE_PATH
|
|
15
|
-
const dir = path.dirname(cacheFile)
|
|
16
|
-
|
|
17
|
-
type StorageData = {
|
|
18
|
-
entries: Record<string, CacheEntry>
|
|
19
|
-
tagIndex: Record<string, string[]> // tag -> array of keys
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function ensureDir(): void {
|
|
23
|
-
if (!fs.existsSync(dir)) {
|
|
24
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function readData(): StorageData {
|
|
29
|
-
ensureDir()
|
|
30
|
-
if (!fs.existsSync(cacheFile)) {
|
|
31
|
-
return { entries: {}, tagIndex: {} }
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
const content = fs.readFileSync(cacheFile, 'utf8')
|
|
35
|
-
return JSON.parse(content) as StorageData
|
|
36
|
-
} catch {
|
|
37
|
-
return { entries: {}, tagIndex: {} }
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function writeData(data: StorageData): void {
|
|
42
|
-
ensureDir()
|
|
43
|
-
fs.writeFileSync(cacheFile, JSON.stringify(data, null, 2), 'utf8')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isExpired(entry: CacheEntry): boolean {
|
|
47
|
-
if (entry.expiresAt === null) return false
|
|
48
|
-
return Date.now() > entry.expiresAt
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function addToTagIndex(data: StorageData, key: string, tags: string[]): void {
|
|
52
|
-
for (const tag of tags) {
|
|
53
|
-
if (!data.tagIndex[tag]) {
|
|
54
|
-
data.tagIndex[tag] = []
|
|
55
|
-
}
|
|
56
|
-
if (!data.tagIndex[tag].includes(key)) {
|
|
57
|
-
data.tagIndex[tag].push(key)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function removeFromTagIndex(data: StorageData, key: string, tags: string[]): void {
|
|
63
|
-
for (const tag of tags) {
|
|
64
|
-
if (data.tagIndex[tag]) {
|
|
65
|
-
data.tagIndex[tag] = data.tagIndex[tag].filter((k) => k !== key)
|
|
66
|
-
if (data.tagIndex[tag].length === 0) {
|
|
67
|
-
delete data.tagIndex[tag]
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const get = async (key: string, options?: CacheGetOptions): Promise<CacheValue | null> => {
|
|
74
|
-
const data = readData()
|
|
75
|
-
const entry = data.entries[key]
|
|
76
|
-
|
|
77
|
-
if (!entry) return null
|
|
78
|
-
|
|
79
|
-
if (isExpired(entry)) {
|
|
80
|
-
if (options?.returnExpired) {
|
|
81
|
-
return entry.value
|
|
82
|
-
}
|
|
83
|
-
// Clean up expired entry
|
|
84
|
-
removeFromTagIndex(data, key, entry.tags)
|
|
85
|
-
delete data.entries[key]
|
|
86
|
-
writeData(data)
|
|
87
|
-
return null
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return entry.value
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const set = async (key: string, value: CacheValue, options?: CacheSetOptions): Promise<void> => {
|
|
94
|
-
const data = readData()
|
|
95
|
-
|
|
96
|
-
// Remove old entry from tag index if it exists
|
|
97
|
-
const oldEntry = data.entries[key]
|
|
98
|
-
if (oldEntry) {
|
|
99
|
-
removeFromTagIndex(data, key, oldEntry.tags)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const ttl = options?.ttl ?? defaultTtl
|
|
103
|
-
const tags = options?.tags || []
|
|
104
|
-
const expiresAt = ttl ? Date.now() + ttl : null
|
|
105
|
-
|
|
106
|
-
const entry: CacheEntry = {
|
|
107
|
-
key,
|
|
108
|
-
value,
|
|
109
|
-
tags,
|
|
110
|
-
expiresAt,
|
|
111
|
-
createdAt: Date.now(),
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
data.entries[key] = entry
|
|
115
|
-
addToTagIndex(data, key, tags)
|
|
116
|
-
|
|
117
|
-
writeData(data)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const has = async (key: string): Promise<boolean> => {
|
|
121
|
-
const data = readData()
|
|
122
|
-
const entry = data.entries[key]
|
|
123
|
-
|
|
124
|
-
if (!entry) return false
|
|
125
|
-
|
|
126
|
-
if (isExpired(entry)) {
|
|
127
|
-
removeFromTagIndex(data, key, entry.tags)
|
|
128
|
-
delete data.entries[key]
|
|
129
|
-
writeData(data)
|
|
130
|
-
return false
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return true
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const deleteKey = async (key: string): Promise<boolean> => {
|
|
137
|
-
const data = readData()
|
|
138
|
-
const entry = data.entries[key]
|
|
139
|
-
|
|
140
|
-
if (!entry) return false
|
|
141
|
-
|
|
142
|
-
removeFromTagIndex(data, key, entry.tags)
|
|
143
|
-
delete data.entries[key]
|
|
144
|
-
writeData(data)
|
|
145
|
-
|
|
146
|
-
return true
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const deleteByTags = async (tags: string[]): Promise<number> => {
|
|
150
|
-
const data = readData()
|
|
151
|
-
const keysToDelete = new Set<string>()
|
|
152
|
-
|
|
153
|
-
// Collect all keys that have any of the specified tags
|
|
154
|
-
for (const tag of tags) {
|
|
155
|
-
const keys = data.tagIndex[tag] || []
|
|
156
|
-
for (const key of keys) {
|
|
157
|
-
keysToDelete.add(key)
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Delete all collected keys
|
|
162
|
-
for (const key of keysToDelete) {
|
|
163
|
-
const entry = data.entries[key]
|
|
164
|
-
if (entry) {
|
|
165
|
-
removeFromTagIndex(data, key, entry.tags)
|
|
166
|
-
delete data.entries[key]
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
writeData(data)
|
|
171
|
-
return keysToDelete.size
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const clear = async (): Promise<number> => {
|
|
175
|
-
const data = readData()
|
|
176
|
-
const size = Object.keys(data.entries).length
|
|
177
|
-
|
|
178
|
-
writeData({ entries: {}, tagIndex: {} })
|
|
179
|
-
|
|
180
|
-
return size
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const keys = async (pattern?: string): Promise<string[]> => {
|
|
184
|
-
const data = readData()
|
|
185
|
-
const allKeys = Object.keys(data.entries)
|
|
186
|
-
|
|
187
|
-
if (!pattern) return allKeys
|
|
188
|
-
|
|
189
|
-
return allKeys.filter((key) => matchCacheKeyPattern(key, pattern))
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const stats = async (): Promise<{ size: number; expired: number }> => {
|
|
193
|
-
const data = readData()
|
|
194
|
-
const allEntries = Object.values(data.entries)
|
|
195
|
-
|
|
196
|
-
let expired = 0
|
|
197
|
-
for (const entry of allEntries) {
|
|
198
|
-
if (isExpired(entry)) {
|
|
199
|
-
expired++
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return { size: allEntries.length, expired }
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const cleanup = async (): Promise<number> => {
|
|
207
|
-
const data = readData()
|
|
208
|
-
let removed = 0
|
|
209
|
-
|
|
210
|
-
const keysToRemove: string[] = []
|
|
211
|
-
for (const [key, entry] of Object.entries(data.entries)) {
|
|
212
|
-
if (isExpired(entry)) {
|
|
213
|
-
keysToRemove.push(key)
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
for (const key of keysToRemove) {
|
|
218
|
-
const entry = data.entries[key]
|
|
219
|
-
removeFromTagIndex(data, key, entry.tags)
|
|
220
|
-
delete data.entries[key]
|
|
221
|
-
removed++
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (removed > 0) {
|
|
225
|
-
writeData(data)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return removed
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
get,
|
|
233
|
-
set,
|
|
234
|
-
has,
|
|
235
|
-
delete: deleteKey,
|
|
236
|
-
deleteByTags,
|
|
237
|
-
clear,
|
|
238
|
-
keys,
|
|
239
|
-
stats,
|
|
240
|
-
cleanup,
|
|
241
|
-
}
|
|
242
|
-
}
|
|
1
|
+
export * from '@open-mercato/cache-provider-jsonfile'
|
package/src/strategies/memory.ts
CHANGED
|
@@ -1,275 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { matchCacheKeyPattern } from '../patterns'
|
|
3
|
-
|
|
4
|
-
const EXPIRED_SWEEP_WRITE_INTERVAL = 256
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Upper bound on entries scanned by a single amortized sweep pass. Keeps the
|
|
8
|
-
* sweep O(budget) instead of O(store size) so a large, hot store never pays a
|
|
9
|
-
* full-store scan latency spike on the `set()` that crosses the sweep interval.
|
|
10
|
-
* The scan walks from the LRU head, where expired-and-cold entries accumulate;
|
|
11
|
-
* anything beyond the budget is reclaimed on a later sweep, on access, by LRU
|
|
12
|
-
* eviction once the cap is exceeded, or by an explicit `cleanup()`.
|
|
13
|
-
*/
|
|
14
|
-
const EXPIRED_SWEEP_MAX_SCAN = 1_000
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Default upper bound on the number of entries a memory cache retains.
|
|
18
|
-
* Bounds memory for long-lived (process-wide) instances; LRU eviction drops
|
|
19
|
-
* the least-recently-used entries once the cap is exceeded. Override via the
|
|
20
|
-
* `maxEntries` option (or `CACHE_MEMORY_MAX_ENTRIES`); a non-positive value
|
|
21
|
-
* disables the cap (unbounded — only safe for short-lived instances).
|
|
22
|
-
*/
|
|
23
|
-
export const DEFAULT_MEMORY_MAX_ENTRIES = 50_000
|
|
24
|
-
|
|
25
|
-
function normalizeMaxEntries(raw?: number): number {
|
|
26
|
-
if (raw === undefined) return DEFAULT_MEMORY_MAX_ENTRIES
|
|
27
|
-
if (!Number.isFinite(raw) || raw <= 0) return Number.POSITIVE_INFINITY
|
|
28
|
-
return Math.floor(raw)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* In-memory cache strategy with tag support.
|
|
33
|
-
* Fast but data is lost when process restarts.
|
|
34
|
-
*
|
|
35
|
-
* Bounded by an LRU cap (`maxEntries`, default {@link DEFAULT_MEMORY_MAX_ENTRIES},
|
|
36
|
-
* env-tunable via `CACHE_MEMORY_MAX_ENTRIES` resolved in the cache service) so a
|
|
37
|
-
* process-shared instance (OM_BOOTSTRAP_CACHE, long-lived workers, memory-backed
|
|
38
|
-
* CRUD list cache) cannot grow without limit on user-controllable key
|
|
39
|
-
* cardinality. Recency is refreshed on read (Map re-insertion), the oldest
|
|
40
|
-
* entries are evicted on write, and expired entries are reclaimed by an
|
|
41
|
-
* amortized sweep every N writes — no per-instance timer, so the per-request
|
|
42
|
-
* default stays leak-free (a per-instance setInterval would pin every request's
|
|
43
|
-
* Maps for the process lifetime).
|
|
44
|
-
*/
|
|
45
|
-
export function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries?: number }): CacheStrategy {
|
|
46
|
-
const store = new Map<string, CacheEntry>()
|
|
47
|
-
const tagIndex = new Map<string, Set<string>>() // tag -> Set of keys
|
|
48
|
-
const defaultTtl = options?.defaultTtl
|
|
49
|
-
const maxEntries = normalizeMaxEntries(options?.maxEntries)
|
|
50
|
-
let writesSinceSweep = 0
|
|
51
|
-
// Lightweight observability counters so operators can tell whether the bound
|
|
52
|
-
// is actively protecting the process. Process-global for this instance, not
|
|
53
|
-
// tenant-scoped; surfaced via stats().
|
|
54
|
-
let evictions = 0
|
|
55
|
-
let sweeps = 0
|
|
56
|
-
let lastSweepReclaimed = 0
|
|
57
|
-
|
|
58
|
-
function isExpired(entry: CacheEntry): boolean {
|
|
59
|
-
if (entry.expiresAt === null) return false
|
|
60
|
-
return Date.now() > entry.expiresAt
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// LRU bookkeeping: re-insert on read so the most-recently-used entry moves
|
|
64
|
-
// to the tail (Map preserves insertion order), mirroring the rbacDefaultCache
|
|
65
|
-
// precedent; evictIfNeeded then drops from the head (least-recently-used).
|
|
66
|
-
function touchKey(key: string, entry: CacheEntry): void {
|
|
67
|
-
if (maxEntries === Number.POSITIVE_INFINITY) return
|
|
68
|
-
store.delete(key)
|
|
69
|
-
store.set(key, entry)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function evictIfNeeded(): void {
|
|
73
|
-
if (maxEntries === Number.POSITIVE_INFINITY) return
|
|
74
|
-
while (store.size > maxEntries) {
|
|
75
|
-
const oldest = store.keys().next().value
|
|
76
|
-
if (typeof oldest !== 'string') break
|
|
77
|
-
const entry = store.get(oldest)
|
|
78
|
-
store.delete(oldest)
|
|
79
|
-
if (entry) removeFromTagIndex(oldest, entry.tags)
|
|
80
|
-
evictions++
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function cleanupExpiredEntry(key: string, entry: CacheEntry): void {
|
|
85
|
-
store.delete(key)
|
|
86
|
-
// Remove from tag index
|
|
87
|
-
for (const tag of entry.tags) {
|
|
88
|
-
const keys = tagIndex.get(tag)
|
|
89
|
-
if (keys) {
|
|
90
|
-
keys.delete(key)
|
|
91
|
-
if (keys.size === 0) {
|
|
92
|
-
tagIndex.delete(tag)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function addToTagIndex(key: string, tags: string[]): void {
|
|
99
|
-
for (const tag of tags) {
|
|
100
|
-
if (!tagIndex.has(tag)) {
|
|
101
|
-
tagIndex.set(tag, new Set())
|
|
102
|
-
}
|
|
103
|
-
tagIndex.get(tag)!.add(key)
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function removeFromTagIndex(key: string, tags: string[]): void {
|
|
108
|
-
for (const tag of tags) {
|
|
109
|
-
const keys = tagIndex.get(tag)
|
|
110
|
-
if (keys) {
|
|
111
|
-
keys.delete(key)
|
|
112
|
-
if (keys.size === 0) {
|
|
113
|
-
tagIndex.delete(tag)
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Amortized reclamation of already-expired entries. Runs every N writes
|
|
120
|
-
// instead of on a timer, keeping the no-shared-state property that makes the
|
|
121
|
-
// per-request default safe. Independent of the LRU cap so expired-but-cold
|
|
122
|
-
// entries are reclaimed even when the store stays under `maxEntries`. The
|
|
123
|
-
// scan is budgeted (EXPIRED_SWEEP_MAX_SCAN entries from the LRU head per pass)
|
|
124
|
-
// so it stays O(budget) on large stores instead of scanning every entry.
|
|
125
|
-
function sweepExpiredIfDue(): void {
|
|
126
|
-
if (++writesSinceSweep < EXPIRED_SWEEP_WRITE_INTERVAL) return
|
|
127
|
-
writesSinceSweep = 0
|
|
128
|
-
sweeps++
|
|
129
|
-
let scanned = 0
|
|
130
|
-
let reclaimed = 0
|
|
131
|
-
for (const [key, entry] of store.entries()) {
|
|
132
|
-
if (scanned >= EXPIRED_SWEEP_MAX_SCAN) break
|
|
133
|
-
scanned++
|
|
134
|
-
if (isExpired(entry)) {
|
|
135
|
-
cleanupExpiredEntry(key, entry)
|
|
136
|
-
reclaimed++
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
lastSweepReclaimed = reclaimed
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const get = async (key: string, options?: CacheGetOptions): Promise<CacheValue | null> => {
|
|
143
|
-
const entry = store.get(key)
|
|
144
|
-
if (!entry) return null
|
|
145
|
-
|
|
146
|
-
if (isExpired(entry)) {
|
|
147
|
-
if (options?.returnExpired) {
|
|
148
|
-
return entry.value
|
|
149
|
-
}
|
|
150
|
-
cleanupExpiredEntry(key, entry)
|
|
151
|
-
return null
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
touchKey(key, entry)
|
|
155
|
-
return entry.value
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const set = async (key: string, value: CacheValue, options?: CacheSetOptions): Promise<void> => {
|
|
159
|
-
// Remove old entry from tag index if it exists
|
|
160
|
-
const oldEntry = store.get(key)
|
|
161
|
-
if (oldEntry) {
|
|
162
|
-
removeFromTagIndex(key, oldEntry.tags)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const ttl = options?.ttl ?? defaultTtl
|
|
166
|
-
const tags = options?.tags || []
|
|
167
|
-
const expiresAt = ttl ? Date.now() + ttl : null
|
|
168
|
-
|
|
169
|
-
const entry: CacheEntry = {
|
|
170
|
-
key,
|
|
171
|
-
value,
|
|
172
|
-
tags,
|
|
173
|
-
expiresAt,
|
|
174
|
-
createdAt: Date.now(),
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
store.set(key, entry)
|
|
178
|
-
addToTagIndex(key, tags)
|
|
179
|
-
sweepExpiredIfDue()
|
|
180
|
-
evictIfNeeded()
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const has = async (key: string): Promise<boolean> => {
|
|
184
|
-
const entry = store.get(key)
|
|
185
|
-
if (!entry) return false
|
|
186
|
-
if (isExpired(entry)) {
|
|
187
|
-
cleanupExpiredEntry(key, entry)
|
|
188
|
-
return false
|
|
189
|
-
}
|
|
190
|
-
return true
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const deleteKey = async (key: string): Promise<boolean> => {
|
|
194
|
-
const entry = store.get(key)
|
|
195
|
-
if (!entry) return false
|
|
196
|
-
|
|
197
|
-
removeFromTagIndex(key, entry.tags)
|
|
198
|
-
return store.delete(key)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const deleteByTags = async (tags: string[]): Promise<number> => {
|
|
202
|
-
const keysToDelete = new Set<string>()
|
|
203
|
-
|
|
204
|
-
// Collect all keys that have any of the specified tags
|
|
205
|
-
for (const tag of tags) {
|
|
206
|
-
const keys = tagIndex.get(tag)
|
|
207
|
-
if (keys) {
|
|
208
|
-
for (const key of keys) {
|
|
209
|
-
keysToDelete.add(key)
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Delete all collected keys
|
|
215
|
-
let deleted = 0
|
|
216
|
-
for (const key of keysToDelete) {
|
|
217
|
-
const success = await deleteKey(key)
|
|
218
|
-
if (success) deleted++
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return deleted
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const clear = async (): Promise<number> => {
|
|
225
|
-
const size = store.size
|
|
226
|
-
store.clear()
|
|
227
|
-
tagIndex.clear()
|
|
228
|
-
return size
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const keys = async (pattern?: string): Promise<string[]> => {
|
|
232
|
-
const allKeys = Array.from(store.keys())
|
|
233
|
-
if (!pattern) return allKeys
|
|
234
|
-
return allKeys.filter((key) => matchCacheKeyPattern(key, pattern))
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const stats = async (): Promise<{
|
|
238
|
-
size: number
|
|
239
|
-
expired: number
|
|
240
|
-
evictions: number
|
|
241
|
-
sweeps: number
|
|
242
|
-
lastSweepReclaimed: number
|
|
243
|
-
}> => {
|
|
244
|
-
let expired = 0
|
|
245
|
-
for (const entry of store.values()) {
|
|
246
|
-
if (isExpired(entry)) {
|
|
247
|
-
expired++
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return { size: store.size, expired, evictions, sweeps, lastSweepReclaimed }
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const cleanup = async (): Promise<number> => {
|
|
254
|
-
let removed = 0
|
|
255
|
-
for (const [key, entry] of store.entries()) {
|
|
256
|
-
if (isExpired(entry)) {
|
|
257
|
-
cleanupExpiredEntry(key, entry)
|
|
258
|
-
removed++
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
return removed
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return {
|
|
265
|
-
get,
|
|
266
|
-
set,
|
|
267
|
-
has,
|
|
268
|
-
delete: deleteKey,
|
|
269
|
-
deleteByTags,
|
|
270
|
-
clear,
|
|
271
|
-
keys,
|
|
272
|
-
stats,
|
|
273
|
-
cleanup,
|
|
274
|
-
}
|
|
275
|
-
}
|
|
1
|
+
export * from '@open-mercato/cache-provider-memory'
|