@open-mercato/cache 0.11.1-develop.8593.1.50784dae35 → 0.11.1-develop.8687.1.adb16f62b3
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,197 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { CacheDependencyUnavailableError } from "../errors.js";
|
|
5
|
-
import { DEFAULT_SQLITE_CACHE_PATH } from "../defaults.js";
|
|
6
|
-
import { matchCacheKeyPattern } from "../patterns.js";
|
|
7
|
-
import { decodeCacheText, encodeCacheText, resolveCacheCodecConfig } from "../codec.js";
|
|
8
|
-
const sqliteRequire = createRequire(path.join(process.cwd(), "package.json"));
|
|
9
|
-
function createSqliteStrategy(dbPath, options) {
|
|
10
|
-
let db = null;
|
|
11
|
-
const defaultTtl = options?.defaultTtl;
|
|
12
|
-
const codec = resolveCacheCodecConfig(options);
|
|
13
|
-
const filePath = dbPath || process.env.CACHE_SQLITE_PATH || DEFAULT_SQLITE_CACHE_PATH;
|
|
14
|
-
async function getDb() {
|
|
15
|
-
if (db) return db;
|
|
16
|
-
try {
|
|
17
|
-
const imported = options?.databaseConstructor ?? sqliteRequire("better-sqlite3");
|
|
18
|
-
const Database = typeof imported === "function" ? imported : imported.default;
|
|
19
|
-
const dir = path.dirname(filePath);
|
|
20
|
-
if (!fs.existsSync(dir)) {
|
|
21
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
22
|
-
}
|
|
23
|
-
db = new Database(filePath);
|
|
24
|
-
db.exec(`
|
|
25
|
-
PRAGMA journal_mode = WAL;
|
|
26
|
-
PRAGMA synchronous = NORMAL;
|
|
27
|
-
PRAGMA busy_timeout = 5000;
|
|
28
|
-
PRAGMA foreign_keys = ON;
|
|
29
|
-
`);
|
|
30
|
-
db.exec(`
|
|
31
|
-
CREATE TABLE IF NOT EXISTS cache_entries (
|
|
32
|
-
key TEXT PRIMARY KEY,
|
|
33
|
-
value TEXT NOT NULL,
|
|
34
|
-
expires_at INTEGER,
|
|
35
|
-
created_at INTEGER NOT NULL
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
CREATE TABLE IF NOT EXISTS cache_tags (
|
|
39
|
-
key TEXT NOT NULL,
|
|
40
|
-
tag TEXT NOT NULL,
|
|
41
|
-
PRIMARY KEY (key, tag),
|
|
42
|
-
FOREIGN KEY (key) REFERENCES cache_entries(key) ON DELETE CASCADE
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
CREATE INDEX IF NOT EXISTS idx_cache_tags_tag ON cache_tags(tag);
|
|
46
|
-
CREATE INDEX IF NOT EXISTS idx_cache_entries_expires_at ON cache_entries(expires_at);
|
|
47
|
-
`);
|
|
48
|
-
return db;
|
|
49
|
-
} catch (error) {
|
|
50
|
-
throw new CacheDependencyUnavailableError("sqlite", "better-sqlite3", error);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function isExpired(expiresAt) {
|
|
54
|
-
if (expiresAt === null) return false;
|
|
55
|
-
return Date.now() > expiresAt;
|
|
56
|
-
}
|
|
57
|
-
const get = async (key, options2) => {
|
|
58
|
-
const database = await getDb();
|
|
59
|
-
const stmt = database.prepare("SELECT value, expires_at FROM cache_entries WHERE key = ?");
|
|
60
|
-
const row = stmt.get(key);
|
|
61
|
-
if (!row) return null;
|
|
62
|
-
try {
|
|
63
|
-
const value = JSON.parse(decodeCacheText(row.value));
|
|
64
|
-
const expiresAt = row.expires_at;
|
|
65
|
-
if (isExpired(expiresAt)) {
|
|
66
|
-
if (options2?.returnExpired) {
|
|
67
|
-
return value;
|
|
68
|
-
}
|
|
69
|
-
await deleteKey(key);
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
return value;
|
|
73
|
-
} catch {
|
|
74
|
-
await deleteKey(key);
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
const set = async (key, value, options2) => {
|
|
79
|
-
const database = await getDb();
|
|
80
|
-
const ttl = options2?.ttl ?? defaultTtl;
|
|
81
|
-
const tags = options2?.tags || [];
|
|
82
|
-
const expiresAt = ttl ? Date.now() + ttl : null;
|
|
83
|
-
const createdAt = Date.now();
|
|
84
|
-
const serialized = encodeCacheText(JSON.stringify(value), codec);
|
|
85
|
-
database.transaction(() => {
|
|
86
|
-
database.prepare("DELETE FROM cache_tags WHERE key = ?").run(key);
|
|
87
|
-
database.prepare(`
|
|
88
|
-
INSERT OR REPLACE INTO cache_entries (key, value, expires_at, created_at)
|
|
89
|
-
VALUES (?, ?, ?, ?)
|
|
90
|
-
`).run(key, serialized, expiresAt, createdAt);
|
|
91
|
-
if (tags.length > 0) {
|
|
92
|
-
const insertTag = database.prepare("INSERT INTO cache_tags (key, tag) VALUES (?, ?)");
|
|
93
|
-
for (const tag of tags) {
|
|
94
|
-
insertTag.run(key, tag);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
})();
|
|
98
|
-
};
|
|
99
|
-
const has = async (key) => {
|
|
100
|
-
const database = await getDb();
|
|
101
|
-
const stmt = database.prepare("SELECT expires_at FROM cache_entries WHERE key = ?");
|
|
102
|
-
const row = stmt.get(key);
|
|
103
|
-
if (!row) return false;
|
|
104
|
-
if (isExpired(row.expires_at)) {
|
|
105
|
-
await deleteKey(key);
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
return true;
|
|
109
|
-
};
|
|
110
|
-
const deleteKey = async (key) => {
|
|
111
|
-
const database = await getDb();
|
|
112
|
-
const result = database.transaction(() => {
|
|
113
|
-
database.prepare("DELETE FROM cache_tags WHERE key = ?").run(key);
|
|
114
|
-
const info = database.prepare("DELETE FROM cache_entries WHERE key = ?").run(key);
|
|
115
|
-
return info.changes > 0;
|
|
116
|
-
})();
|
|
117
|
-
return result;
|
|
118
|
-
};
|
|
119
|
-
const deleteByTags = async (tags) => {
|
|
120
|
-
const database = await getDb();
|
|
121
|
-
const placeholders = tags.map(() => "?").join(",");
|
|
122
|
-
const stmt = database.prepare(`
|
|
123
|
-
SELECT DISTINCT key FROM cache_tags WHERE tag IN (${placeholders})
|
|
124
|
-
`);
|
|
125
|
-
const rows = stmt.all(...tags);
|
|
126
|
-
let deleted = 0;
|
|
127
|
-
for (const row of rows) {
|
|
128
|
-
const success = await deleteKey(row.key);
|
|
129
|
-
if (success) deleted++;
|
|
130
|
-
}
|
|
131
|
-
return deleted;
|
|
132
|
-
};
|
|
133
|
-
const clear = async () => {
|
|
134
|
-
const database = await getDb();
|
|
135
|
-
const result = database.transaction(() => {
|
|
136
|
-
const countStmt = database.prepare("SELECT COUNT(*) as count FROM cache_entries");
|
|
137
|
-
const count = countStmt.get().count;
|
|
138
|
-
database.prepare("DELETE FROM cache_tags").run();
|
|
139
|
-
database.prepare("DELETE FROM cache_entries").run();
|
|
140
|
-
return count;
|
|
141
|
-
})();
|
|
142
|
-
return result;
|
|
143
|
-
};
|
|
144
|
-
const keys = async (pattern) => {
|
|
145
|
-
const database = await getDb();
|
|
146
|
-
const stmt = database.prepare("SELECT key FROM cache_entries");
|
|
147
|
-
const rows = stmt.all();
|
|
148
|
-
const allKeys = rows.map((row) => row.key);
|
|
149
|
-
if (!pattern) return allKeys;
|
|
150
|
-
return allKeys.filter((key) => matchCacheKeyPattern(key, pattern));
|
|
151
|
-
};
|
|
152
|
-
const stats = async () => {
|
|
153
|
-
const database = await getDb();
|
|
154
|
-
const sizeStmt = database.prepare("SELECT COUNT(*) as count FROM cache_entries");
|
|
155
|
-
const size = sizeStmt.get().count;
|
|
156
|
-
const now = Date.now();
|
|
157
|
-
const expiredStmt = database.prepare("SELECT COUNT(*) as count FROM cache_entries WHERE expires_at IS NOT NULL AND expires_at < ?");
|
|
158
|
-
const expired = expiredStmt.get(now).count;
|
|
159
|
-
return { size, expired };
|
|
160
|
-
};
|
|
161
|
-
const cleanup = async () => {
|
|
162
|
-
const database = await getDb();
|
|
163
|
-
const now = Date.now();
|
|
164
|
-
const result = database.transaction(() => {
|
|
165
|
-
const stmt = database.prepare("SELECT key FROM cache_entries WHERE expires_at IS NOT NULL AND expires_at < ?");
|
|
166
|
-
const rows = stmt.all(now);
|
|
167
|
-
for (const row of rows) {
|
|
168
|
-
database.prepare("DELETE FROM cache_tags WHERE key = ?").run(row.key);
|
|
169
|
-
}
|
|
170
|
-
const info = database.prepare("DELETE FROM cache_entries WHERE expires_at IS NOT NULL AND expires_at < ?").run(now);
|
|
171
|
-
return info.changes;
|
|
172
|
-
})();
|
|
173
|
-
return result;
|
|
174
|
-
};
|
|
175
|
-
const close = async () => {
|
|
176
|
-
if (db) {
|
|
177
|
-
db.close();
|
|
178
|
-
db = null;
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
return {
|
|
182
|
-
get,
|
|
183
|
-
set,
|
|
184
|
-
has,
|
|
185
|
-
delete: deleteKey,
|
|
186
|
-
deleteByTags,
|
|
187
|
-
clear,
|
|
188
|
-
keys,
|
|
189
|
-
stats,
|
|
190
|
-
cleanup,
|
|
191
|
-
close
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
export {
|
|
195
|
-
createSqliteStrategy
|
|
196
|
-
};
|
|
1
|
+
export * from "@open-mercato/cache-provider-sqlite";
|
|
197
2
|
//# sourceMappingURL=sqlite.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/strategies/sqlite.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["export * from '@open-mercato/cache-provider-sqlite'\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|