@knaus94/prisma-extension-cache-manager 1.5.5 → 1.5.6
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/index.js +27 -8
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -56,6 +56,12 @@ function generateComposedKey(options) {
|
|
|
56
56
|
function createKey(key, namespace) {
|
|
57
57
|
return namespace ? `${namespace}:${key}` : key;
|
|
58
58
|
}
|
|
59
|
+
function serialize(data) {
|
|
60
|
+
return mp.encode(data).slice();
|
|
61
|
+
}
|
|
62
|
+
function deserialize(buffer) {
|
|
63
|
+
return mp.decode(buffer);
|
|
64
|
+
}
|
|
59
65
|
/**
|
|
60
66
|
* Обрабатывает удаление ключей из кеша после операций записи.
|
|
61
67
|
* @param cache - Кеш-менеджер.
|
|
@@ -174,7 +180,8 @@ exports.default = ({ cache, defaultTTL, debug }) => {
|
|
|
174
180
|
? cacheOption // Если cacheOption — строка, используем её как ключ напрямую
|
|
175
181
|
: generateComposedKey({ model, queryArgs }); // Иначе генерируем ключ
|
|
176
182
|
// Если cacheOption — число, оно означает TTL
|
|
177
|
-
ttl =
|
|
183
|
+
ttl =
|
|
184
|
+
typeof cacheOption === "number" ? cacheOption : defaultTTL ?? 0;
|
|
178
185
|
}
|
|
179
186
|
// 2b) Если cacheOption — объект с key: function,
|
|
180
187
|
// нужно сначала сделать запрос к БД, чтобы функция могла сгенерировать ключ
|
|
@@ -187,12 +194,17 @@ exports.default = ({ cache, defaultTTL, debug }) => {
|
|
|
187
194
|
}
|
|
188
195
|
// Функция генерирует ключ на основе результатов
|
|
189
196
|
cacheKey = cacheOption.key(result);
|
|
190
|
-
ttl = cacheOption.ttl ?? defaultTTL;
|
|
197
|
+
ttl = cacheOption.ttl ?? defaultTTL ?? 0;
|
|
191
198
|
// Сохраняем результат в кеш, используя msgpack5
|
|
192
199
|
try {
|
|
193
|
-
|
|
200
|
+
if (ttl > 0) {
|
|
201
|
+
await cache.store.client.set(cacheKey, serialize(result), "EX", ttl / 1000);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
await cache.store.client.set(cacheKey, serialize(result));
|
|
205
|
+
}
|
|
194
206
|
if (debug) {
|
|
195
|
-
console.log("Data cached with key (function):", cacheKey, "encoded:",
|
|
207
|
+
console.log("Data cached with key (function):", cacheKey, "encoded:", serialize(result), "decoded:", result);
|
|
196
208
|
}
|
|
197
209
|
}
|
|
198
210
|
catch (e) {
|
|
@@ -215,11 +227,12 @@ exports.default = ({ cache, defaultTTL, debug }) => {
|
|
|
215
227
|
// Используем getBuffer, т.к. сохраняем бинарные данные
|
|
216
228
|
const cached = await cache.store.client.getBuffer(cacheKey);
|
|
217
229
|
if (cached) {
|
|
230
|
+
const data = deserialize(cached);
|
|
218
231
|
if (debug) {
|
|
219
|
-
console.log("Cache hit for key:", cacheKey, "data", cached, "decoded",
|
|
232
|
+
console.log("Cache hit for key:", cacheKey, "data", cached, "decoded", data);
|
|
220
233
|
}
|
|
221
234
|
// Десериализуем через msgpack5
|
|
222
|
-
return
|
|
235
|
+
return data;
|
|
223
236
|
}
|
|
224
237
|
else {
|
|
225
238
|
if (debug) {
|
|
@@ -241,9 +254,15 @@ exports.default = ({ cache, defaultTTL, debug }) => {
|
|
|
241
254
|
}
|
|
242
255
|
// 6. Сохраняем результат запроса в кеш
|
|
243
256
|
try {
|
|
244
|
-
|
|
257
|
+
const data = serialize(result);
|
|
258
|
+
if (ttl > 0) {
|
|
259
|
+
await cache.store.client.set(cacheKey, data, "EX", ttl / 1000);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
await cache.store.client.set(cacheKey, data);
|
|
263
|
+
}
|
|
245
264
|
if (debug) {
|
|
246
|
-
console.log("Data cached with key:", cacheKey, "encoded:",
|
|
265
|
+
console.log("Data cached with key:", cacheKey, "encoded:", data, "decoded:", result);
|
|
247
266
|
}
|
|
248
267
|
}
|
|
249
268
|
catch (e) {
|