@hedystia/db 2.0.0 → 2.0.2
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/dist/_virtual/_rolldown/runtime.cjs +13 -0
- package/dist/_virtual/_rolldown/runtime.mjs +17 -0
- package/dist/cache/manager.cjs +137 -0
- package/dist/cache/manager.cjs.map +1 -0
- package/dist/cache/manager.d.cts +72 -0
- package/dist/cache/manager.d.mts +72 -0
- package/dist/cache/manager.mjs +140 -0
- package/dist/cache/manager.mjs.map +1 -0
- package/dist/cache/memory-store.cjs +122 -0
- package/dist/cache/memory-store.cjs.map +1 -0
- package/dist/cache/memory-store.mjs +122 -0
- package/dist/cache/memory-store.mjs.map +1 -0
- package/dist/cli/commands/migration.cjs +22 -0
- package/dist/cli/commands/migration.cjs.map +1 -0
- package/dist/cli/commands/migration.mjs +32 -0
- package/dist/cli/commands/migration.mjs.map +1 -0
- package/dist/cli/commands/schema.cjs +21 -0
- package/dist/cli/commands/schema.cjs.map +1 -0
- package/dist/cli/commands/schema.mjs +30 -0
- package/dist/cli/commands/schema.mjs.map +1 -0
- package/dist/cli.cjs +50 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +57 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/constants.cjs +16 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.mjs +16 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/core/database.cjs +158 -0
- package/dist/core/database.cjs.map +1 -0
- package/dist/core/database.d.cts +74 -0
- package/dist/core/database.d.mts +74 -0
- package/dist/core/database.mjs +159 -0
- package/dist/core/database.mjs.map +1 -0
- package/dist/core/repository.cjs +416 -0
- package/dist/core/repository.cjs.map +1 -0
- package/dist/core/repository.d.cts +110 -0
- package/dist/core/repository.d.mts +110 -0
- package/dist/core/repository.mjs +416 -0
- package/dist/core/repository.mjs.map +1 -0
- package/dist/drivers/driver.cjs +11 -0
- package/dist/drivers/driver.cjs.map +1 -0
- package/dist/drivers/driver.mjs +11 -0
- package/dist/drivers/driver.mjs.map +1 -0
- package/dist/drivers/file.cjs +336 -0
- package/dist/drivers/file.cjs.map +1 -0
- package/dist/drivers/file.mjs +337 -0
- package/dist/drivers/file.mjs.map +1 -0
- package/dist/drivers/index.cjs +28 -0
- package/dist/drivers/index.cjs.map +1 -0
- package/dist/drivers/index.d.cts +14 -0
- package/dist/drivers/index.d.mts +14 -0
- package/dist/drivers/index.mjs +28 -0
- package/dist/drivers/index.mjs.map +1 -0
- package/dist/drivers/mysql.cjs +272 -0
- package/dist/drivers/mysql.cjs.map +1 -0
- package/dist/drivers/mysql.mjs +272 -0
- package/dist/drivers/mysql.mjs.map +1 -0
- package/dist/drivers/sql-compiler.cjs +284 -0
- package/dist/drivers/sql-compiler.cjs.map +1 -0
- package/dist/drivers/sql-compiler.d.cts +66 -0
- package/dist/drivers/sql-compiler.d.mts +66 -0
- package/dist/drivers/sql-compiler.mjs +276 -0
- package/dist/drivers/sql-compiler.mjs.map +1 -0
- package/dist/drivers/sqlite.cjs +262 -0
- package/dist/drivers/sqlite.cjs.map +1 -0
- package/dist/drivers/sqlite.mjs +262 -0
- package/dist/drivers/sqlite.mjs.map +1 -0
- package/dist/errors.cjs +74 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +46 -0
- package/dist/errors.d.mts +46 -0
- package/dist/errors.mjs +68 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/index.cjs +69 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +21 -0
- package/dist/index.mjs.map +1 -0
- package/dist/migrations/definition.cjs +20 -0
- package/dist/migrations/definition.cjs.map +1 -0
- package/dist/migrations/definition.d.cts +18 -0
- package/dist/migrations/definition.d.mts +18 -0
- package/dist/migrations/definition.mjs +23 -0
- package/dist/migrations/definition.mjs.map +1 -0
- package/dist/migrations/index.mjs +12 -0
- package/dist/migrations/index.mjs.map +1 -0
- package/dist/migrations/templates.cjs +39 -0
- package/dist/migrations/templates.cjs.map +1 -0
- package/dist/migrations/templates.d.cts +16 -0
- package/dist/migrations/templates.d.mts +16 -0
- package/dist/migrations/templates.mjs +41 -0
- package/dist/migrations/templates.mjs.map +1 -0
- package/dist/schema/column.cjs +161 -0
- package/dist/schema/column.cjs.map +1 -0
- package/dist/schema/column.d.cts +120 -0
- package/dist/schema/column.d.mts +120 -0
- package/dist/schema/column.mjs +161 -0
- package/dist/schema/column.mjs.map +1 -0
- package/dist/schema/columns/index.cjs +202 -0
- package/dist/schema/columns/index.cjs.map +1 -0
- package/dist/schema/columns/index.d.cts +141 -0
- package/dist/schema/columns/index.d.mts +141 -0
- package/dist/schema/columns/index.mjs +182 -0
- package/dist/schema/columns/index.mjs.map +1 -0
- package/dist/schema/registry.cjs +125 -0
- package/dist/schema/registry.cjs.map +1 -0
- package/dist/schema/registry.d.cts +66 -0
- package/dist/schema/registry.d.mts +66 -0
- package/dist/schema/registry.mjs +125 -0
- package/dist/schema/registry.mjs.map +1 -0
- package/dist/schema/table.cjs +39 -0
- package/dist/schema/table.cjs.map +1 -0
- package/dist/schema/table.d.cts +17 -0
- package/dist/schema/table.d.mts +17 -0
- package/dist/schema/table.mjs +39 -0
- package/dist/schema/table.mjs.map +1 -0
- package/dist/sync/synchronizer.cjs +43 -0
- package/dist/sync/synchronizer.cjs.map +1 -0
- package/dist/sync/synchronizer.d.cts +22 -0
- package/dist/sync/synchronizer.d.mts +22 -0
- package/dist/sync/synchronizer.mjs +43 -0
- package/dist/sync/synchronizer.mjs.map +1 -0
- package/dist/types.d.cts +229 -0
- package/dist/types.d.mts +229 -0
- package/dist/utils/fs.cjs +24 -0
- package/dist/utils/fs.cjs.map +1 -0
- package/dist/utils/fs.mjs +26 -0
- package/dist/utils/fs.mjs.map +1 -0
- package/dist/utils/index.mjs +14 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/naming.cjs +13 -0
- package/dist/utils/naming.cjs.map +1 -0
- package/dist/utils/naming.mjs +16 -0
- package/dist/utils/naming.mjs.map +1 -0
- package/dist/utils/stable-stringify.cjs +19 -0
- package/dist/utils/stable-stringify.cjs.map +1 -0
- package/dist/utils/stable-stringify.mjs +22 -0
- package/dist/utils/stable-stringify.mjs.map +1 -0
- package/package.json +64 -27
- package/readme.md +87 -105
- package/index.d.ts +0 -65
- package/index.js +0 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
exports.__exportAll = __exportAll;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __esmMin = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
5
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
6
|
+
var __exportAll = (all, no_symbols) => {
|
|
7
|
+
let target = {};
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
16
|
+
//#endregion
|
|
17
|
+
export { __commonJSMin, __esmMin, __exportAll, __require };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const require_constants = require("../constants.cjs");
|
|
2
|
+
const require_stable_stringify = require("../utils/stable-stringify.cjs");
|
|
3
|
+
const require_memory_store = require("./memory-store.cjs");
|
|
4
|
+
//#region src/cache/manager.ts
|
|
5
|
+
/**
|
|
6
|
+
* Cache manager that wraps query execution with caching behavior
|
|
7
|
+
*/
|
|
8
|
+
var CacheManager = class {
|
|
9
|
+
store;
|
|
10
|
+
baseTtl;
|
|
11
|
+
maxTtl;
|
|
12
|
+
enabled;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
if (typeof config === "boolean") {
|
|
15
|
+
this.enabled = config;
|
|
16
|
+
this.baseTtl = require_constants.DEFAULT_CACHE_TTL;
|
|
17
|
+
this.maxTtl = require_constants.DEFAULT_CACHE_MAX_TTL;
|
|
18
|
+
this.store = new require_memory_store.MemoryStore(require_constants.DEFAULT_CACHE_MAX_ENTRIES);
|
|
19
|
+
} else if (config) {
|
|
20
|
+
this.enabled = config.enabled;
|
|
21
|
+
this.baseTtl = config.ttl ?? 6e4;
|
|
22
|
+
this.maxTtl = config.maxTtl ?? 3e5;
|
|
23
|
+
this.store = new require_memory_store.MemoryStore(config.maxEntries ?? 1e4);
|
|
24
|
+
} else {
|
|
25
|
+
this.enabled = false;
|
|
26
|
+
this.baseTtl = require_constants.DEFAULT_CACHE_TTL;
|
|
27
|
+
this.maxTtl = require_constants.DEFAULT_CACHE_MAX_TTL;
|
|
28
|
+
this.store = new require_memory_store.MemoryStore();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get a cached query result or execute the query and cache the result
|
|
33
|
+
* @param {string} table - Table name
|
|
34
|
+
* @param {string} method - Method name (find, findFirst, etc.)
|
|
35
|
+
* @param {unknown} options - Query options
|
|
36
|
+
* @param {() => Promise<T>} executor - Function that executes the query
|
|
37
|
+
* @returns {Promise<T>} The query result
|
|
38
|
+
*/
|
|
39
|
+
async getOrSet(table, method, options, executor) {
|
|
40
|
+
if (!this.enabled) return executor();
|
|
41
|
+
const key = this.buildKey(table, method, options);
|
|
42
|
+
const cached = this.store.get(key);
|
|
43
|
+
if (cached !== void 0) {
|
|
44
|
+
this.store.extendTtl(key, this.baseTtl, this.maxTtl);
|
|
45
|
+
return cached;
|
|
46
|
+
}
|
|
47
|
+
const result = await executor();
|
|
48
|
+
this.store.set(key, result, this.baseTtl);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Set an entity in the cache by primary key
|
|
53
|
+
* @param {string} table - Table name
|
|
54
|
+
* @param {string | number} id - Primary key value
|
|
55
|
+
* @param {unknown} data - Entity data
|
|
56
|
+
*/
|
|
57
|
+
setEntity(table, id, data) {
|
|
58
|
+
if (!this.enabled) return;
|
|
59
|
+
const key = `entity:${table}:${id}`;
|
|
60
|
+
this.store.set(key, data, this.baseTtl);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get a cached entity by primary key
|
|
64
|
+
* @param {string} table - Table name
|
|
65
|
+
* @param {string | number} id - Primary key value
|
|
66
|
+
* @returns {unknown | undefined} Cached entity or undefined
|
|
67
|
+
*/
|
|
68
|
+
getEntity(table, id) {
|
|
69
|
+
if (!this.enabled) return;
|
|
70
|
+
const key = `entity:${table}:${id}`;
|
|
71
|
+
const result = this.store.get(key);
|
|
72
|
+
if (result !== void 0) this.store.extendTtl(key, this.baseTtl, this.maxTtl);
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Invalidate all cache entries for a table
|
|
77
|
+
* @param {string} table - Table name
|
|
78
|
+
*/
|
|
79
|
+
invalidateTable(table) {
|
|
80
|
+
if (!this.enabled) return;
|
|
81
|
+
this.store.invalidateByPrefix(`query:${table}:`);
|
|
82
|
+
this.store.invalidateByPrefix(`entity:${table}:`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Invalidate a specific entity cache entry
|
|
86
|
+
* @param {string} table - Table name
|
|
87
|
+
* @param {string | number} id - Primary key value
|
|
88
|
+
*/
|
|
89
|
+
invalidateEntity(table, id) {
|
|
90
|
+
if (!this.enabled) return;
|
|
91
|
+
this.store.delete(`entity:${table}:${id}`);
|
|
92
|
+
this.store.invalidateByPrefix(`query:${table}:`);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Update an entity in cache if it exists
|
|
96
|
+
* @param {string} table - Table name
|
|
97
|
+
* @param {string | number} id - Primary key value
|
|
98
|
+
* @param {Record<string, unknown>} data - Updated data
|
|
99
|
+
*/
|
|
100
|
+
updateEntity(table, id, data) {
|
|
101
|
+
if (!this.enabled) return;
|
|
102
|
+
const key = `entity:${table}:${id}`;
|
|
103
|
+
const existing = this.store.get(key);
|
|
104
|
+
if (existing && typeof existing === "object") this.store.set(key, {
|
|
105
|
+
...existing,
|
|
106
|
+
...data
|
|
107
|
+
}, this.baseTtl);
|
|
108
|
+
this.store.invalidateByPrefix(`query:${table}:`);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Clear the entire cache
|
|
112
|
+
*/
|
|
113
|
+
clear() {
|
|
114
|
+
this.store.clear();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the number of cached entries
|
|
118
|
+
* @returns {number} Cache size
|
|
119
|
+
*/
|
|
120
|
+
get size() {
|
|
121
|
+
return this.store.size;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Check if caching is enabled
|
|
125
|
+
* @returns {boolean} Whether caching is enabled
|
|
126
|
+
*/
|
|
127
|
+
get isEnabled() {
|
|
128
|
+
return this.enabled;
|
|
129
|
+
}
|
|
130
|
+
buildKey(table, method, options) {
|
|
131
|
+
return `query:${table}:${method}:${require_stable_stringify.stableStringify(options)}`;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
//#endregion
|
|
135
|
+
exports.CacheManager = CacheManager;
|
|
136
|
+
|
|
137
|
+
//# sourceMappingURL=manager.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.cjs","names":["DEFAULT_CACHE_TTL","DEFAULT_CACHE_MAX_TTL","MemoryStore","DEFAULT_CACHE_MAX_ENTRIES","stableStringify"],"sources":["../../src/cache/manager.ts"],"sourcesContent":["import { DEFAULT_CACHE_MAX_ENTRIES, DEFAULT_CACHE_MAX_TTL, DEFAULT_CACHE_TTL } from \"../constants\";\nimport type { CacheConfig } from \"../types\";\nimport { stableStringify } from \"../utils\";\nimport { MemoryStore } from \"./memory-store\";\n\n/**\n * Cache manager that wraps query execution with caching behavior\n */\nexport class CacheManager {\n private store: MemoryStore;\n private baseTtl: number;\n private maxTtl: number;\n private enabled: boolean;\n\n constructor(config?: boolean | CacheConfig) {\n if (typeof config === \"boolean\") {\n this.enabled = config;\n this.baseTtl = DEFAULT_CACHE_TTL;\n this.maxTtl = DEFAULT_CACHE_MAX_TTL;\n this.store = new MemoryStore(DEFAULT_CACHE_MAX_ENTRIES);\n } else if (config) {\n this.enabled = config.enabled;\n this.baseTtl = config.ttl ?? DEFAULT_CACHE_TTL;\n this.maxTtl = config.maxTtl ?? DEFAULT_CACHE_MAX_TTL;\n this.store = new MemoryStore(config.maxEntries ?? DEFAULT_CACHE_MAX_ENTRIES);\n } else {\n this.enabled = false;\n this.baseTtl = DEFAULT_CACHE_TTL;\n this.maxTtl = DEFAULT_CACHE_MAX_TTL;\n this.store = new MemoryStore();\n }\n }\n\n /**\n * Get a cached query result or execute the query and cache the result\n * @param {string} table - Table name\n * @param {string} method - Method name (find, findFirst, etc.)\n * @param {unknown} options - Query options\n * @param {() => Promise<T>} executor - Function that executes the query\n * @returns {Promise<T>} The query result\n */\n async getOrSet<T>(\n table: string,\n method: string,\n options: unknown,\n executor: () => Promise<T>,\n ): Promise<T> {\n if (!this.enabled) {\n return executor();\n }\n\n const key = this.buildKey(table, method, options);\n const cached = this.store.get(key);\n if (cached !== undefined) {\n this.store.extendTtl(key, this.baseTtl, this.maxTtl);\n return cached as T;\n }\n\n const result = await executor();\n this.store.set(key, result, this.baseTtl);\n return result;\n }\n\n /**\n * Set an entity in the cache by primary key\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n * @param {unknown} data - Entity data\n */\n setEntity(table: string, id: string | number, data: unknown): void {\n if (!this.enabled) {\n return;\n }\n const key = `entity:${table}:${id}`;\n this.store.set(key, data, this.baseTtl);\n }\n\n /**\n * Get a cached entity by primary key\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n * @returns {unknown | undefined} Cached entity or undefined\n */\n getEntity(table: string, id: string | number): unknown | undefined {\n if (!this.enabled) {\n return undefined;\n }\n const key = `entity:${table}:${id}`;\n const result = this.store.get(key);\n if (result !== undefined) {\n this.store.extendTtl(key, this.baseTtl, this.maxTtl);\n }\n return result;\n }\n\n /**\n * Invalidate all cache entries for a table\n * @param {string} table - Table name\n */\n invalidateTable(table: string): void {\n if (!this.enabled) {\n return;\n }\n this.store.invalidateByPrefix(`query:${table}:`);\n this.store.invalidateByPrefix(`entity:${table}:`);\n }\n\n /**\n * Invalidate a specific entity cache entry\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n */\n invalidateEntity(table: string, id: string | number): void {\n if (!this.enabled) {\n return;\n }\n this.store.delete(`entity:${table}:${id}`);\n this.store.invalidateByPrefix(`query:${table}:`);\n }\n\n /**\n * Update an entity in cache if it exists\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n * @param {Record<string, unknown>} data - Updated data\n */\n updateEntity(table: string, id: string | number, data: Record<string, unknown>): void {\n if (!this.enabled) {\n return;\n }\n const key = `entity:${table}:${id}`;\n const existing = this.store.get(key);\n if (existing && typeof existing === \"object\") {\n this.store.set(key, { ...existing, ...data }, this.baseTtl);\n }\n this.store.invalidateByPrefix(`query:${table}:`);\n }\n\n /**\n * Clear the entire cache\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Get the number of cached entries\n * @returns {number} Cache size\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Check if caching is enabled\n * @returns {boolean} Whether caching is enabled\n */\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n private buildKey(table: string, method: string, options: unknown): string {\n return `query:${table}:${method}:${stableStringify(options)}`;\n }\n}\n"],"mappings":";;;;;;;AAQA,IAAa,eAAb,MAA0B;CACxB;CACA;CACA;CACA;CAEA,YAAY,QAAgC;AAC1C,MAAI,OAAO,WAAW,WAAW;AAC/B,QAAK,UAAU;AACf,QAAK,UAAUA,kBAAAA;AACf,QAAK,SAASC,kBAAAA;AACd,QAAK,QAAQ,IAAIC,qBAAAA,YAAYC,kBAAAA,0BAA0B;aAC9C,QAAQ;AACjB,QAAK,UAAU,OAAO;AACtB,QAAK,UAAU,OAAO,OAAA;AACtB,QAAK,SAAS,OAAO,UAAA;AACrB,QAAK,QAAQ,IAAID,qBAAAA,YAAY,OAAO,cAAA,IAAwC;SACvE;AACL,QAAK,UAAU;AACf,QAAK,UAAUF,kBAAAA;AACf,QAAK,SAASC,kBAAAA;AACd,QAAK,QAAQ,IAAIC,qBAAAA,aAAa;;;;;;;;;;;CAYlC,MAAM,SACJ,OACA,QACA,SACA,UACY;AACZ,MAAI,CAAC,KAAK,QACR,QAAO,UAAU;EAGnB,MAAM,MAAM,KAAK,SAAS,OAAO,QAAQ,QAAQ;EACjD,MAAM,SAAS,KAAK,MAAM,IAAI,IAAI;AAClC,MAAI,WAAW,KAAA,GAAW;AACxB,QAAK,MAAM,UAAU,KAAK,KAAK,SAAS,KAAK,OAAO;AACpD,UAAO;;EAGT,MAAM,SAAS,MAAM,UAAU;AAC/B,OAAK,MAAM,IAAI,KAAK,QAAQ,KAAK,QAAQ;AACzC,SAAO;;;;;;;;CAST,UAAU,OAAe,IAAqB,MAAqB;AACjE,MAAI,CAAC,KAAK,QACR;EAEF,MAAM,MAAM,UAAU,MAAM,GAAG;AAC/B,OAAK,MAAM,IAAI,KAAK,MAAM,KAAK,QAAQ;;;;;;;;CASzC,UAAU,OAAe,IAA0C;AACjE,MAAI,CAAC,KAAK,QACR;EAEF,MAAM,MAAM,UAAU,MAAM,GAAG;EAC/B,MAAM,SAAS,KAAK,MAAM,IAAI,IAAI;AAClC,MAAI,WAAW,KAAA,EACb,MAAK,MAAM,UAAU,KAAK,KAAK,SAAS,KAAK,OAAO;AAEtD,SAAO;;;;;;CAOT,gBAAgB,OAAqB;AACnC,MAAI,CAAC,KAAK,QACR;AAEF,OAAK,MAAM,mBAAmB,SAAS,MAAM,GAAG;AAChD,OAAK,MAAM,mBAAmB,UAAU,MAAM,GAAG;;;;;;;CAQnD,iBAAiB,OAAe,IAA2B;AACzD,MAAI,CAAC,KAAK,QACR;AAEF,OAAK,MAAM,OAAO,UAAU,MAAM,GAAG,KAAK;AAC1C,OAAK,MAAM,mBAAmB,SAAS,MAAM,GAAG;;;;;;;;CASlD,aAAa,OAAe,IAAqB,MAAqC;AACpF,MAAI,CAAC,KAAK,QACR;EAEF,MAAM,MAAM,UAAU,MAAM,GAAG;EAC/B,MAAM,WAAW,KAAK,MAAM,IAAI,IAAI;AACpC,MAAI,YAAY,OAAO,aAAa,SAClC,MAAK,MAAM,IAAI,KAAK;GAAE,GAAG;GAAU,GAAG;GAAM,EAAE,KAAK,QAAQ;AAE7D,OAAK,MAAM,mBAAmB,SAAS,MAAM,GAAG;;;;;CAMlD,QAAc;AACZ,OAAK,MAAM,OAAO;;;;;;CAOpB,IAAI,OAAe;AACjB,SAAO,KAAK,MAAM;;;;;;CAOpB,IAAI,YAAqB;AACvB,SAAO,KAAK;;CAGd,SAAiB,OAAe,QAAgB,SAA0B;AACxE,SAAO,SAAS,MAAM,GAAG,OAAO,GAAGE,yBAAAA,gBAAgB,QAAQ"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { CacheConfig } from "../types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cache/manager.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Cache manager that wraps query execution with caching behavior
|
|
6
|
+
*/
|
|
7
|
+
declare class CacheManager {
|
|
8
|
+
private store;
|
|
9
|
+
private baseTtl;
|
|
10
|
+
private maxTtl;
|
|
11
|
+
private enabled;
|
|
12
|
+
constructor(config?: boolean | CacheConfig);
|
|
13
|
+
/**
|
|
14
|
+
* Get a cached query result or execute the query and cache the result
|
|
15
|
+
* @param {string} table - Table name
|
|
16
|
+
* @param {string} method - Method name (find, findFirst, etc.)
|
|
17
|
+
* @param {unknown} options - Query options
|
|
18
|
+
* @param {() => Promise<T>} executor - Function that executes the query
|
|
19
|
+
* @returns {Promise<T>} The query result
|
|
20
|
+
*/
|
|
21
|
+
getOrSet<T>(table: string, method: string, options: unknown, executor: () => Promise<T>): Promise<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Set an entity in the cache by primary key
|
|
24
|
+
* @param {string} table - Table name
|
|
25
|
+
* @param {string | number} id - Primary key value
|
|
26
|
+
* @param {unknown} data - Entity data
|
|
27
|
+
*/
|
|
28
|
+
setEntity(table: string, id: string | number, data: unknown): void;
|
|
29
|
+
/**
|
|
30
|
+
* Get a cached entity by primary key
|
|
31
|
+
* @param {string} table - Table name
|
|
32
|
+
* @param {string | number} id - Primary key value
|
|
33
|
+
* @returns {unknown | undefined} Cached entity or undefined
|
|
34
|
+
*/
|
|
35
|
+
getEntity(table: string, id: string | number): unknown | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Invalidate all cache entries for a table
|
|
38
|
+
* @param {string} table - Table name
|
|
39
|
+
*/
|
|
40
|
+
invalidateTable(table: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Invalidate a specific entity cache entry
|
|
43
|
+
* @param {string} table - Table name
|
|
44
|
+
* @param {string | number} id - Primary key value
|
|
45
|
+
*/
|
|
46
|
+
invalidateEntity(table: string, id: string | number): void;
|
|
47
|
+
/**
|
|
48
|
+
* Update an entity in cache if it exists
|
|
49
|
+
* @param {string} table - Table name
|
|
50
|
+
* @param {string | number} id - Primary key value
|
|
51
|
+
* @param {Record<string, unknown>} data - Updated data
|
|
52
|
+
*/
|
|
53
|
+
updateEntity(table: string, id: string | number, data: Record<string, unknown>): void;
|
|
54
|
+
/**
|
|
55
|
+
* Clear the entire cache
|
|
56
|
+
*/
|
|
57
|
+
clear(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Get the number of cached entries
|
|
60
|
+
* @returns {number} Cache size
|
|
61
|
+
*/
|
|
62
|
+
get size(): number;
|
|
63
|
+
/**
|
|
64
|
+
* Check if caching is enabled
|
|
65
|
+
* @returns {boolean} Whether caching is enabled
|
|
66
|
+
*/
|
|
67
|
+
get isEnabled(): boolean;
|
|
68
|
+
private buildKey;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { CacheManager };
|
|
72
|
+
//# sourceMappingURL=manager.d.cts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { CacheConfig } from "../types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cache/manager.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Cache manager that wraps query execution with caching behavior
|
|
6
|
+
*/
|
|
7
|
+
declare class CacheManager {
|
|
8
|
+
private store;
|
|
9
|
+
private baseTtl;
|
|
10
|
+
private maxTtl;
|
|
11
|
+
private enabled;
|
|
12
|
+
constructor(config?: boolean | CacheConfig);
|
|
13
|
+
/**
|
|
14
|
+
* Get a cached query result or execute the query and cache the result
|
|
15
|
+
* @param {string} table - Table name
|
|
16
|
+
* @param {string} method - Method name (find, findFirst, etc.)
|
|
17
|
+
* @param {unknown} options - Query options
|
|
18
|
+
* @param {() => Promise<T>} executor - Function that executes the query
|
|
19
|
+
* @returns {Promise<T>} The query result
|
|
20
|
+
*/
|
|
21
|
+
getOrSet<T>(table: string, method: string, options: unknown, executor: () => Promise<T>): Promise<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Set an entity in the cache by primary key
|
|
24
|
+
* @param {string} table - Table name
|
|
25
|
+
* @param {string | number} id - Primary key value
|
|
26
|
+
* @param {unknown} data - Entity data
|
|
27
|
+
*/
|
|
28
|
+
setEntity(table: string, id: string | number, data: unknown): void;
|
|
29
|
+
/**
|
|
30
|
+
* Get a cached entity by primary key
|
|
31
|
+
* @param {string} table - Table name
|
|
32
|
+
* @param {string | number} id - Primary key value
|
|
33
|
+
* @returns {unknown | undefined} Cached entity or undefined
|
|
34
|
+
*/
|
|
35
|
+
getEntity(table: string, id: string | number): unknown | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Invalidate all cache entries for a table
|
|
38
|
+
* @param {string} table - Table name
|
|
39
|
+
*/
|
|
40
|
+
invalidateTable(table: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Invalidate a specific entity cache entry
|
|
43
|
+
* @param {string} table - Table name
|
|
44
|
+
* @param {string | number} id - Primary key value
|
|
45
|
+
*/
|
|
46
|
+
invalidateEntity(table: string, id: string | number): void;
|
|
47
|
+
/**
|
|
48
|
+
* Update an entity in cache if it exists
|
|
49
|
+
* @param {string} table - Table name
|
|
50
|
+
* @param {string | number} id - Primary key value
|
|
51
|
+
* @param {Record<string, unknown>} data - Updated data
|
|
52
|
+
*/
|
|
53
|
+
updateEntity(table: string, id: string | number, data: Record<string, unknown>): void;
|
|
54
|
+
/**
|
|
55
|
+
* Clear the entire cache
|
|
56
|
+
*/
|
|
57
|
+
clear(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Get the number of cached entries
|
|
60
|
+
* @returns {number} Cache size
|
|
61
|
+
*/
|
|
62
|
+
get size(): number;
|
|
63
|
+
/**
|
|
64
|
+
* Check if caching is enabled
|
|
65
|
+
* @returns {boolean} Whether caching is enabled
|
|
66
|
+
*/
|
|
67
|
+
get isEnabled(): boolean;
|
|
68
|
+
private buildKey;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { CacheManager };
|
|
72
|
+
//# sourceMappingURL=manager.d.mts.map
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { DEFAULT_CACHE_MAX_ENTRIES, DEFAULT_CACHE_MAX_TTL, DEFAULT_CACHE_TTL, init_constants } from "../constants.mjs";
|
|
2
|
+
import { stableStringify } from "../utils/stable-stringify.mjs";
|
|
3
|
+
import { init_utils } from "../utils/index.mjs";
|
|
4
|
+
import { MemoryStore } from "./memory-store.mjs";
|
|
5
|
+
//#region src/cache/manager.ts
|
|
6
|
+
init_constants();
|
|
7
|
+
init_utils();
|
|
8
|
+
/**
|
|
9
|
+
* Cache manager that wraps query execution with caching behavior
|
|
10
|
+
*/
|
|
11
|
+
var CacheManager = class {
|
|
12
|
+
store;
|
|
13
|
+
baseTtl;
|
|
14
|
+
maxTtl;
|
|
15
|
+
enabled;
|
|
16
|
+
constructor(config) {
|
|
17
|
+
if (typeof config === "boolean") {
|
|
18
|
+
this.enabled = config;
|
|
19
|
+
this.baseTtl = DEFAULT_CACHE_TTL;
|
|
20
|
+
this.maxTtl = DEFAULT_CACHE_MAX_TTL;
|
|
21
|
+
this.store = new MemoryStore(DEFAULT_CACHE_MAX_ENTRIES);
|
|
22
|
+
} else if (config) {
|
|
23
|
+
this.enabled = config.enabled;
|
|
24
|
+
this.baseTtl = config.ttl ?? 6e4;
|
|
25
|
+
this.maxTtl = config.maxTtl ?? 3e5;
|
|
26
|
+
this.store = new MemoryStore(config.maxEntries ?? 1e4);
|
|
27
|
+
} else {
|
|
28
|
+
this.enabled = false;
|
|
29
|
+
this.baseTtl = DEFAULT_CACHE_TTL;
|
|
30
|
+
this.maxTtl = DEFAULT_CACHE_MAX_TTL;
|
|
31
|
+
this.store = new MemoryStore();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get a cached query result or execute the query and cache the result
|
|
36
|
+
* @param {string} table - Table name
|
|
37
|
+
* @param {string} method - Method name (find, findFirst, etc.)
|
|
38
|
+
* @param {unknown} options - Query options
|
|
39
|
+
* @param {() => Promise<T>} executor - Function that executes the query
|
|
40
|
+
* @returns {Promise<T>} The query result
|
|
41
|
+
*/
|
|
42
|
+
async getOrSet(table, method, options, executor) {
|
|
43
|
+
if (!this.enabled) return executor();
|
|
44
|
+
const key = this.buildKey(table, method, options);
|
|
45
|
+
const cached = this.store.get(key);
|
|
46
|
+
if (cached !== void 0) {
|
|
47
|
+
this.store.extendTtl(key, this.baseTtl, this.maxTtl);
|
|
48
|
+
return cached;
|
|
49
|
+
}
|
|
50
|
+
const result = await executor();
|
|
51
|
+
this.store.set(key, result, this.baseTtl);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Set an entity in the cache by primary key
|
|
56
|
+
* @param {string} table - Table name
|
|
57
|
+
* @param {string | number} id - Primary key value
|
|
58
|
+
* @param {unknown} data - Entity data
|
|
59
|
+
*/
|
|
60
|
+
setEntity(table, id, data) {
|
|
61
|
+
if (!this.enabled) return;
|
|
62
|
+
const key = `entity:${table}:${id}`;
|
|
63
|
+
this.store.set(key, data, this.baseTtl);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get a cached entity by primary key
|
|
67
|
+
* @param {string} table - Table name
|
|
68
|
+
* @param {string | number} id - Primary key value
|
|
69
|
+
* @returns {unknown | undefined} Cached entity or undefined
|
|
70
|
+
*/
|
|
71
|
+
getEntity(table, id) {
|
|
72
|
+
if (!this.enabled) return;
|
|
73
|
+
const key = `entity:${table}:${id}`;
|
|
74
|
+
const result = this.store.get(key);
|
|
75
|
+
if (result !== void 0) this.store.extendTtl(key, this.baseTtl, this.maxTtl);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Invalidate all cache entries for a table
|
|
80
|
+
* @param {string} table - Table name
|
|
81
|
+
*/
|
|
82
|
+
invalidateTable(table) {
|
|
83
|
+
if (!this.enabled) return;
|
|
84
|
+
this.store.invalidateByPrefix(`query:${table}:`);
|
|
85
|
+
this.store.invalidateByPrefix(`entity:${table}:`);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Invalidate a specific entity cache entry
|
|
89
|
+
* @param {string} table - Table name
|
|
90
|
+
* @param {string | number} id - Primary key value
|
|
91
|
+
*/
|
|
92
|
+
invalidateEntity(table, id) {
|
|
93
|
+
if (!this.enabled) return;
|
|
94
|
+
this.store.delete(`entity:${table}:${id}`);
|
|
95
|
+
this.store.invalidateByPrefix(`query:${table}:`);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Update an entity in cache if it exists
|
|
99
|
+
* @param {string} table - Table name
|
|
100
|
+
* @param {string | number} id - Primary key value
|
|
101
|
+
* @param {Record<string, unknown>} data - Updated data
|
|
102
|
+
*/
|
|
103
|
+
updateEntity(table, id, data) {
|
|
104
|
+
if (!this.enabled) return;
|
|
105
|
+
const key = `entity:${table}:${id}`;
|
|
106
|
+
const existing = this.store.get(key);
|
|
107
|
+
if (existing && typeof existing === "object") this.store.set(key, {
|
|
108
|
+
...existing,
|
|
109
|
+
...data
|
|
110
|
+
}, this.baseTtl);
|
|
111
|
+
this.store.invalidateByPrefix(`query:${table}:`);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Clear the entire cache
|
|
115
|
+
*/
|
|
116
|
+
clear() {
|
|
117
|
+
this.store.clear();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get the number of cached entries
|
|
121
|
+
* @returns {number} Cache size
|
|
122
|
+
*/
|
|
123
|
+
get size() {
|
|
124
|
+
return this.store.size;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Check if caching is enabled
|
|
128
|
+
* @returns {boolean} Whether caching is enabled
|
|
129
|
+
*/
|
|
130
|
+
get isEnabled() {
|
|
131
|
+
return this.enabled;
|
|
132
|
+
}
|
|
133
|
+
buildKey(table, method, options) {
|
|
134
|
+
return `query:${table}:${method}:${stableStringify(options)}`;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
export { CacheManager };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=manager.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.mjs","names":[],"sources":["../../src/cache/manager.ts"],"sourcesContent":["import { DEFAULT_CACHE_MAX_ENTRIES, DEFAULT_CACHE_MAX_TTL, DEFAULT_CACHE_TTL } from \"../constants\";\nimport type { CacheConfig } from \"../types\";\nimport { stableStringify } from \"../utils\";\nimport { MemoryStore } from \"./memory-store\";\n\n/**\n * Cache manager that wraps query execution with caching behavior\n */\nexport class CacheManager {\n private store: MemoryStore;\n private baseTtl: number;\n private maxTtl: number;\n private enabled: boolean;\n\n constructor(config?: boolean | CacheConfig) {\n if (typeof config === \"boolean\") {\n this.enabled = config;\n this.baseTtl = DEFAULT_CACHE_TTL;\n this.maxTtl = DEFAULT_CACHE_MAX_TTL;\n this.store = new MemoryStore(DEFAULT_CACHE_MAX_ENTRIES);\n } else if (config) {\n this.enabled = config.enabled;\n this.baseTtl = config.ttl ?? DEFAULT_CACHE_TTL;\n this.maxTtl = config.maxTtl ?? DEFAULT_CACHE_MAX_TTL;\n this.store = new MemoryStore(config.maxEntries ?? DEFAULT_CACHE_MAX_ENTRIES);\n } else {\n this.enabled = false;\n this.baseTtl = DEFAULT_CACHE_TTL;\n this.maxTtl = DEFAULT_CACHE_MAX_TTL;\n this.store = new MemoryStore();\n }\n }\n\n /**\n * Get a cached query result or execute the query and cache the result\n * @param {string} table - Table name\n * @param {string} method - Method name (find, findFirst, etc.)\n * @param {unknown} options - Query options\n * @param {() => Promise<T>} executor - Function that executes the query\n * @returns {Promise<T>} The query result\n */\n async getOrSet<T>(\n table: string,\n method: string,\n options: unknown,\n executor: () => Promise<T>,\n ): Promise<T> {\n if (!this.enabled) {\n return executor();\n }\n\n const key = this.buildKey(table, method, options);\n const cached = this.store.get(key);\n if (cached !== undefined) {\n this.store.extendTtl(key, this.baseTtl, this.maxTtl);\n return cached as T;\n }\n\n const result = await executor();\n this.store.set(key, result, this.baseTtl);\n return result;\n }\n\n /**\n * Set an entity in the cache by primary key\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n * @param {unknown} data - Entity data\n */\n setEntity(table: string, id: string | number, data: unknown): void {\n if (!this.enabled) {\n return;\n }\n const key = `entity:${table}:${id}`;\n this.store.set(key, data, this.baseTtl);\n }\n\n /**\n * Get a cached entity by primary key\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n * @returns {unknown | undefined} Cached entity or undefined\n */\n getEntity(table: string, id: string | number): unknown | undefined {\n if (!this.enabled) {\n return undefined;\n }\n const key = `entity:${table}:${id}`;\n const result = this.store.get(key);\n if (result !== undefined) {\n this.store.extendTtl(key, this.baseTtl, this.maxTtl);\n }\n return result;\n }\n\n /**\n * Invalidate all cache entries for a table\n * @param {string} table - Table name\n */\n invalidateTable(table: string): void {\n if (!this.enabled) {\n return;\n }\n this.store.invalidateByPrefix(`query:${table}:`);\n this.store.invalidateByPrefix(`entity:${table}:`);\n }\n\n /**\n * Invalidate a specific entity cache entry\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n */\n invalidateEntity(table: string, id: string | number): void {\n if (!this.enabled) {\n return;\n }\n this.store.delete(`entity:${table}:${id}`);\n this.store.invalidateByPrefix(`query:${table}:`);\n }\n\n /**\n * Update an entity in cache if it exists\n * @param {string} table - Table name\n * @param {string | number} id - Primary key value\n * @param {Record<string, unknown>} data - Updated data\n */\n updateEntity(table: string, id: string | number, data: Record<string, unknown>): void {\n if (!this.enabled) {\n return;\n }\n const key = `entity:${table}:${id}`;\n const existing = this.store.get(key);\n if (existing && typeof existing === \"object\") {\n this.store.set(key, { ...existing, ...data }, this.baseTtl);\n }\n this.store.invalidateByPrefix(`query:${table}:`);\n }\n\n /**\n * Clear the entire cache\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Get the number of cached entries\n * @returns {number} Cache size\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Check if caching is enabled\n * @returns {boolean} Whether caching is enabled\n */\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n private buildKey(table: string, method: string, options: unknown): string {\n return `query:${table}:${method}:${stableStringify(options)}`;\n }\n}\n"],"mappings":";;;;;gBAAmG;YAExD;;;;AAM3C,IAAa,eAAb,MAA0B;CACxB;CACA;CACA;CACA;CAEA,YAAY,QAAgC;AAC1C,MAAI,OAAO,WAAW,WAAW;AAC/B,QAAK,UAAU;AACf,QAAK,UAAU;AACf,QAAK,SAAS;AACd,QAAK,QAAQ,IAAI,YAAY,0BAA0B;aAC9C,QAAQ;AACjB,QAAK,UAAU,OAAO;AACtB,QAAK,UAAU,OAAO,OAAA;AACtB,QAAK,SAAS,OAAO,UAAA;AACrB,QAAK,QAAQ,IAAI,YAAY,OAAO,cAAA,IAAwC;SACvE;AACL,QAAK,UAAU;AACf,QAAK,UAAU;AACf,QAAK,SAAS;AACd,QAAK,QAAQ,IAAI,aAAa;;;;;;;;;;;CAYlC,MAAM,SACJ,OACA,QACA,SACA,UACY;AACZ,MAAI,CAAC,KAAK,QACR,QAAO,UAAU;EAGnB,MAAM,MAAM,KAAK,SAAS,OAAO,QAAQ,QAAQ;EACjD,MAAM,SAAS,KAAK,MAAM,IAAI,IAAI;AAClC,MAAI,WAAW,KAAA,GAAW;AACxB,QAAK,MAAM,UAAU,KAAK,KAAK,SAAS,KAAK,OAAO;AACpD,UAAO;;EAGT,MAAM,SAAS,MAAM,UAAU;AAC/B,OAAK,MAAM,IAAI,KAAK,QAAQ,KAAK,QAAQ;AACzC,SAAO;;;;;;;;CAST,UAAU,OAAe,IAAqB,MAAqB;AACjE,MAAI,CAAC,KAAK,QACR;EAEF,MAAM,MAAM,UAAU,MAAM,GAAG;AAC/B,OAAK,MAAM,IAAI,KAAK,MAAM,KAAK,QAAQ;;;;;;;;CASzC,UAAU,OAAe,IAA0C;AACjE,MAAI,CAAC,KAAK,QACR;EAEF,MAAM,MAAM,UAAU,MAAM,GAAG;EAC/B,MAAM,SAAS,KAAK,MAAM,IAAI,IAAI;AAClC,MAAI,WAAW,KAAA,EACb,MAAK,MAAM,UAAU,KAAK,KAAK,SAAS,KAAK,OAAO;AAEtD,SAAO;;;;;;CAOT,gBAAgB,OAAqB;AACnC,MAAI,CAAC,KAAK,QACR;AAEF,OAAK,MAAM,mBAAmB,SAAS,MAAM,GAAG;AAChD,OAAK,MAAM,mBAAmB,UAAU,MAAM,GAAG;;;;;;;CAQnD,iBAAiB,OAAe,IAA2B;AACzD,MAAI,CAAC,KAAK,QACR;AAEF,OAAK,MAAM,OAAO,UAAU,MAAM,GAAG,KAAK;AAC1C,OAAK,MAAM,mBAAmB,SAAS,MAAM,GAAG;;;;;;;;CASlD,aAAa,OAAe,IAAqB,MAAqC;AACpF,MAAI,CAAC,KAAK,QACR;EAEF,MAAM,MAAM,UAAU,MAAM,GAAG;EAC/B,MAAM,WAAW,KAAK,MAAM,IAAI,IAAI;AACpC,MAAI,YAAY,OAAO,aAAa,SAClC,MAAK,MAAM,IAAI,KAAK;GAAE,GAAG;GAAU,GAAG;GAAM,EAAE,KAAK,QAAQ;AAE7D,OAAK,MAAM,mBAAmB,SAAS,MAAM,GAAG;;;;;CAMlD,QAAc;AACZ,OAAK,MAAM,OAAO;;;;;;CAOpB,IAAI,OAAe;AACjB,SAAO,KAAK,MAAM;;;;;;CAOpB,IAAI,YAAqB;AACvB,SAAO,KAAK;;CAGd,SAAiB,OAAe,QAAgB,SAA0B;AACxE,SAAO,SAAS,MAAM,GAAG,OAAO,GAAG,gBAAgB,QAAQ"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
//#region src/cache/memory-store.ts
|
|
2
|
+
/**
|
|
3
|
+
* In-memory cache store with TTL and hit-count tracking
|
|
4
|
+
*/
|
|
5
|
+
var MemoryStore = class {
|
|
6
|
+
store = /* @__PURE__ */ new Map();
|
|
7
|
+
maxEntries;
|
|
8
|
+
constructor(maxEntries = 1e4) {
|
|
9
|
+
this.maxEntries = maxEntries;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get a value from cache
|
|
13
|
+
* @param {string} key - Cache key
|
|
14
|
+
* @returns {unknown | undefined} Cached value or undefined
|
|
15
|
+
*/
|
|
16
|
+
get(key) {
|
|
17
|
+
const entry = this.store.get(key);
|
|
18
|
+
if (!entry) return;
|
|
19
|
+
if (Date.now() > entry.expiresAt) {
|
|
20
|
+
this.store.delete(key);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
entry.hitCount++;
|
|
24
|
+
entry.lastAccess = Date.now();
|
|
25
|
+
return entry.value;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Set a value in cache with TTL
|
|
29
|
+
* @param {string} key - Cache key
|
|
30
|
+
* @param {unknown} value - Value to cache
|
|
31
|
+
* @param {number} ttl - Time to live in milliseconds
|
|
32
|
+
*/
|
|
33
|
+
set(key, value, ttl) {
|
|
34
|
+
if (this.store.size >= this.maxEntries) this.evict();
|
|
35
|
+
this.store.set(key, {
|
|
36
|
+
value,
|
|
37
|
+
expiresAt: Date.now() + ttl,
|
|
38
|
+
hitCount: 1,
|
|
39
|
+
lastAccess: Date.now()
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if a key exists in cache and is not expired
|
|
44
|
+
* @param {string} key - Cache key
|
|
45
|
+
* @returns {boolean} Whether the key exists
|
|
46
|
+
*/
|
|
47
|
+
has(key) {
|
|
48
|
+
const entry = this.store.get(key);
|
|
49
|
+
if (!entry) return false;
|
|
50
|
+
if (Date.now() > entry.expiresAt) {
|
|
51
|
+
this.store.delete(key);
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Delete a key from cache
|
|
58
|
+
* @param {string} key - Cache key
|
|
59
|
+
*/
|
|
60
|
+
delete(key) {
|
|
61
|
+
this.store.delete(key);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Invalidate all cache entries matching a prefix
|
|
65
|
+
* @param {string} prefix - Key prefix
|
|
66
|
+
*/
|
|
67
|
+
invalidateByPrefix(prefix) {
|
|
68
|
+
for (const key of this.store.keys()) if (key.startsWith(prefix)) this.store.delete(key);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Clear all cached entries
|
|
72
|
+
*/
|
|
73
|
+
clear() {
|
|
74
|
+
this.store.clear();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get the number of cached entries
|
|
78
|
+
* @returns {number} Number of entries
|
|
79
|
+
*/
|
|
80
|
+
get size() {
|
|
81
|
+
return this.store.size;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get hit count for a cache key
|
|
85
|
+
* @param {string} key - Cache key
|
|
86
|
+
* @returns {number} Number of hits
|
|
87
|
+
*/
|
|
88
|
+
getHitCount(key) {
|
|
89
|
+
return this.store.get(key)?.hitCount ?? 0;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Extend the TTL of a cache entry based on hit count
|
|
93
|
+
* @param {string} key - Cache key
|
|
94
|
+
* @param {number} baseTtl - Base TTL in milliseconds
|
|
95
|
+
* @param {number} maxTtl - Maximum TTL in milliseconds
|
|
96
|
+
*/
|
|
97
|
+
extendTtl(key, baseTtl, maxTtl) {
|
|
98
|
+
const entry = this.store.get(key);
|
|
99
|
+
if (!entry) return;
|
|
100
|
+
const adaptiveTtl = Math.min(maxTtl, baseTtl * (1 + Math.log2(entry.hitCount + 1)));
|
|
101
|
+
entry.expiresAt = Date.now() + adaptiveTtl;
|
|
102
|
+
}
|
|
103
|
+
evict() {
|
|
104
|
+
let oldestKey = null;
|
|
105
|
+
let oldestAccess = Number.POSITIVE_INFINITY;
|
|
106
|
+
for (const [key, entry] of this.store) {
|
|
107
|
+
if (Date.now() > entry.expiresAt) {
|
|
108
|
+
this.store.delete(key);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (entry.lastAccess < oldestAccess) {
|
|
112
|
+
oldestAccess = entry.lastAccess;
|
|
113
|
+
oldestKey = key;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (oldestKey) this.store.delete(oldestKey);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
//#endregion
|
|
120
|
+
exports.MemoryStore = MemoryStore;
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=memory-store.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-store.cjs","names":[],"sources":["../../src/cache/memory-store.ts"],"sourcesContent":["interface CacheEntry {\n value: unknown;\n expiresAt: number;\n hitCount: number;\n lastAccess: number;\n}\n\n/**\n * In-memory cache store with TTL and hit-count tracking\n */\nexport class MemoryStore {\n private store = new Map<string, CacheEntry>();\n private maxEntries: number;\n\n constructor(maxEntries = 10_000) {\n this.maxEntries = maxEntries;\n }\n\n /**\n * Get a value from cache\n * @param {string} key - Cache key\n * @returns {unknown | undefined} Cached value or undefined\n */\n get(key: string): unknown | undefined {\n const entry = this.store.get(key);\n if (!entry) {\n return undefined;\n }\n if (Date.now() > entry.expiresAt) {\n this.store.delete(key);\n return undefined;\n }\n entry.hitCount++;\n entry.lastAccess = Date.now();\n return entry.value;\n }\n\n /**\n * Set a value in cache with TTL\n * @param {string} key - Cache key\n * @param {unknown} value - Value to cache\n * @param {number} ttl - Time to live in milliseconds\n */\n set(key: string, value: unknown, ttl: number): void {\n if (this.store.size >= this.maxEntries) {\n this.evict();\n }\n this.store.set(key, {\n value,\n expiresAt: Date.now() + ttl,\n hitCount: 1,\n lastAccess: Date.now(),\n });\n }\n\n /**\n * Check if a key exists in cache and is not expired\n * @param {string} key - Cache key\n * @returns {boolean} Whether the key exists\n */\n has(key: string): boolean {\n const entry = this.store.get(key);\n if (!entry) {\n return false;\n }\n if (Date.now() > entry.expiresAt) {\n this.store.delete(key);\n return false;\n }\n return true;\n }\n\n /**\n * Delete a key from cache\n * @param {string} key - Cache key\n */\n delete(key: string): void {\n this.store.delete(key);\n }\n\n /**\n * Invalidate all cache entries matching a prefix\n * @param {string} prefix - Key prefix\n */\n invalidateByPrefix(prefix: string): void {\n for (const key of this.store.keys()) {\n if (key.startsWith(prefix)) {\n this.store.delete(key);\n }\n }\n }\n\n /**\n * Clear all cached entries\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Get the number of cached entries\n * @returns {number} Number of entries\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Get hit count for a cache key\n * @param {string} key - Cache key\n * @returns {number} Number of hits\n */\n getHitCount(key: string): number {\n return this.store.get(key)?.hitCount ?? 0;\n }\n\n /**\n * Extend the TTL of a cache entry based on hit count\n * @param {string} key - Cache key\n * @param {number} baseTtl - Base TTL in milliseconds\n * @param {number} maxTtl - Maximum TTL in milliseconds\n */\n extendTtl(key: string, baseTtl: number, maxTtl: number): void {\n const entry = this.store.get(key);\n if (!entry) {\n return;\n }\n const adaptiveTtl = Math.min(maxTtl, baseTtl * (1 + Math.log2(entry.hitCount + 1)));\n entry.expiresAt = Date.now() + adaptiveTtl;\n }\n\n private evict(): void {\n let oldestKey: string | null = null;\n let oldestAccess = Number.POSITIVE_INFINITY;\n for (const [key, entry] of this.store) {\n if (Date.now() > entry.expiresAt) {\n this.store.delete(key);\n return;\n }\n if (entry.lastAccess < oldestAccess) {\n oldestAccess = entry.lastAccess;\n oldestKey = key;\n }\n }\n if (oldestKey) {\n this.store.delete(oldestKey);\n }\n }\n}\n"],"mappings":";;;;AAUA,IAAa,cAAb,MAAyB;CACvB,wBAAgB,IAAI,KAAyB;CAC7C;CAEA,YAAY,aAAa,KAAQ;AAC/B,OAAK,aAAa;;;;;;;CAQpB,IAAI,KAAkC;EACpC,MAAM,QAAQ,KAAK,MAAM,IAAI,IAAI;AACjC,MAAI,CAAC,MACH;AAEF,MAAI,KAAK,KAAK,GAAG,MAAM,WAAW;AAChC,QAAK,MAAM,OAAO,IAAI;AACtB;;AAEF,QAAM;AACN,QAAM,aAAa,KAAK,KAAK;AAC7B,SAAO,MAAM;;;;;;;;CASf,IAAI,KAAa,OAAgB,KAAmB;AAClD,MAAI,KAAK,MAAM,QAAQ,KAAK,WAC1B,MAAK,OAAO;AAEd,OAAK,MAAM,IAAI,KAAK;GAClB;GACA,WAAW,KAAK,KAAK,GAAG;GACxB,UAAU;GACV,YAAY,KAAK,KAAK;GACvB,CAAC;;;;;;;CAQJ,IAAI,KAAsB;EACxB,MAAM,QAAQ,KAAK,MAAM,IAAI,IAAI;AACjC,MAAI,CAAC,MACH,QAAO;AAET,MAAI,KAAK,KAAK,GAAG,MAAM,WAAW;AAChC,QAAK,MAAM,OAAO,IAAI;AACtB,UAAO;;AAET,SAAO;;;;;;CAOT,OAAO,KAAmB;AACxB,OAAK,MAAM,OAAO,IAAI;;;;;;CAOxB,mBAAmB,QAAsB;AACvC,OAAK,MAAM,OAAO,KAAK,MAAM,MAAM,CACjC,KAAI,IAAI,WAAW,OAAO,CACxB,MAAK,MAAM,OAAO,IAAI;;;;;CAQ5B,QAAc;AACZ,OAAK,MAAM,OAAO;;;;;;CAOpB,IAAI,OAAe;AACjB,SAAO,KAAK,MAAM;;;;;;;CAQpB,YAAY,KAAqB;AAC/B,SAAO,KAAK,MAAM,IAAI,IAAI,EAAE,YAAY;;;;;;;;CAS1C,UAAU,KAAa,SAAiB,QAAsB;EAC5D,MAAM,QAAQ,KAAK,MAAM,IAAI,IAAI;AACjC,MAAI,CAAC,MACH;EAEF,MAAM,cAAc,KAAK,IAAI,QAAQ,WAAW,IAAI,KAAK,KAAK,MAAM,WAAW,EAAE,EAAE;AACnF,QAAM,YAAY,KAAK,KAAK,GAAG;;CAGjC,QAAsB;EACpB,IAAI,YAA2B;EAC/B,IAAI,eAAe,OAAO;AAC1B,OAAK,MAAM,CAAC,KAAK,UAAU,KAAK,OAAO;AACrC,OAAI,KAAK,KAAK,GAAG,MAAM,WAAW;AAChC,SAAK,MAAM,OAAO,IAAI;AACtB;;AAEF,OAAI,MAAM,aAAa,cAAc;AACnC,mBAAe,MAAM;AACrB,gBAAY;;;AAGhB,MAAI,UACF,MAAK,MAAM,OAAO,UAAU"}
|