@knaus94/prisma-extension-cache-manager 1.5.53 → 1.5.55

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.
Files changed (2) hide show
  1. package/dist/index.js +12 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -57,10 +57,10 @@ function createKey(key, namespace) {
57
57
  return namespace ? `${namespace}:${key}` : key;
58
58
  }
59
59
  function serialize(data) {
60
- return mp.encode({ data: data });
60
+ return mp.encode(data).slice();
61
61
  }
62
62
  function deserialize(buffer) {
63
- return mp.decode(buffer).data;
63
+ return mp.decode(buffer);
64
64
  }
65
65
  /**
66
66
  * Обрабатывает удаление ключей из кеша после операций записи.
@@ -180,7 +180,8 @@ exports.default = ({ cache, defaultTTL, debug }) => {
180
180
  ? cacheOption // Если cacheOption — строка, используем её как ключ напрямую
181
181
  : generateComposedKey({ model, queryArgs }); // Иначе генерируем ключ
182
182
  // Если cacheOption — число, оно означает TTL
183
- ttl = typeof cacheOption === "number" ? cacheOption : defaultTTL;
183
+ ttl =
184
+ typeof cacheOption === "number" ? cacheOption : defaultTTL ?? 0;
184
185
  }
185
186
  // 2b) Если cacheOption — объект с key: function,
186
187
  // нужно сначала сделать запрос к БД, чтобы функция могла сгенерировать ключ
@@ -193,10 +194,10 @@ exports.default = ({ cache, defaultTTL, debug }) => {
193
194
  }
194
195
  // Функция генерирует ключ на основе результатов
195
196
  cacheKey = cacheOption.key(result);
196
- ttl = cacheOption.ttl ?? defaultTTL;
197
+ ttl = cacheOption.ttl ?? defaultTTL ?? 0;
197
198
  // Сохраняем результат в кеш, используя msgpack5
198
199
  try {
199
- await cache.set(cacheKey, serialize(result), ttl);
200
+ await cache.store.client.set(cacheKey, serialize(result), "EX", ttl / 1000);
200
201
  if (debug) {
201
202
  console.log("Data cached with key (function):", cacheKey, "encoded:", serialize(result), "decoded:", result);
202
203
  }
@@ -221,11 +222,12 @@ exports.default = ({ cache, defaultTTL, debug }) => {
221
222
  // Используем getBuffer, т.к. сохраняем бинарные данные
222
223
  const cached = await cache.store.client.getBuffer(cacheKey);
223
224
  if (cached) {
225
+ const data = deserialize(cached);
224
226
  if (debug) {
225
- console.log("Cache hit for key:", cacheKey, "data", cached, "decoded", mp.decode(cached));
227
+ console.log("Cache hit for key:", cacheKey, "data", cached, "decoded", data);
226
228
  }
227
229
  // Десериализуем через msgpack5
228
- return deserialize(cached);
230
+ return data;
229
231
  }
230
232
  else {
231
233
  if (debug) {
@@ -247,9 +249,10 @@ exports.default = ({ cache, defaultTTL, debug }) => {
247
249
  }
248
250
  // 6. Сохраняем результат запроса в кеш
249
251
  try {
250
- await cache.set(cacheKey, serialize(result), ttl);
252
+ const data = serialize(result);
253
+ await cache.store.client.set(cacheKey, data, "EX", ttl / 1000);
251
254
  if (debug) {
252
- console.log("Data cached with key:", cacheKey, "encoded:", serialize(result), "decoded:", result);
255
+ console.log("Data cached with key:", cacheKey, "encoded:", data, "decoded:", result);
253
256
  }
254
257
  }
255
258
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaus94/prisma-extension-cache-manager",
3
- "version": "1.5.53",
3
+ "version": "1.5.55",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaus94/prisma-extension-cache-manager.git"