@knaus94/prisma-extension-cache-manager 1.5.65 → 1.5.67
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.d.ts +1 -1
- package/dist/index.js +15 -17
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ModelExtension, PrismaRedisCacheConfig } from "./types";
|
|
|
4
4
|
* @param config - Конфигурация для кеша Redis и TTL по умолчанию.
|
|
5
5
|
* @returns Prisma расширение.
|
|
6
6
|
*/
|
|
7
|
-
declare const _default: ({ cache, debug }: PrismaRedisCacheConfig) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {
|
|
7
|
+
declare const _default: ({ cache, debug, ttl: defaultTTL }: PrismaRedisCacheConfig) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/library").InternalArgs<{}, {
|
|
8
8
|
$allModels: ModelExtension;
|
|
9
9
|
}, {}, {
|
|
10
10
|
$cache: import("cache-manager-ioredis-yet").RedisCache;
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,6 @@ const msgpack_lite_1 = __importDefault(require("msgpack-lite"));
|
|
|
14
14
|
*/
|
|
15
15
|
const codec = msgpack_lite_1.default.createCodec();
|
|
16
16
|
const TYPE_DECIMAL = 0x21;
|
|
17
|
-
const TYPE_DATE = 0x0d;
|
|
18
17
|
/**
|
|
19
18
|
* Регистрируем тип `Decimal`.
|
|
20
19
|
* - При кодировании превращаем `Decimal` в строку.
|
|
@@ -26,17 +25,6 @@ codec.addExtPacker(TYPE_DECIMAL, library_1.Decimal, (decimal) => {
|
|
|
26
25
|
codec.addExtUnpacker(TYPE_DECIMAL, (buffer) => {
|
|
27
26
|
return new library_1.Decimal(buffer.toString());
|
|
28
27
|
});
|
|
29
|
-
/**
|
|
30
|
-
* Регистрируем тип `Date`.
|
|
31
|
-
* - При кодировании превращаем `Date` в строку.
|
|
32
|
-
* - При декодировании восстанавливаем обратно в `Date`.
|
|
33
|
-
*/
|
|
34
|
-
codec.addExtPacker(TYPE_DATE, Date, (date) => {
|
|
35
|
-
return Buffer.from(date.toISOString());
|
|
36
|
-
});
|
|
37
|
-
codec.addExtUnpacker(TYPE_DATE, (buffer) => {
|
|
38
|
-
return new Date(buffer.toString());
|
|
39
|
-
});
|
|
40
28
|
/**
|
|
41
29
|
* Генерирует уникальный ключ для кеширования на основе модели и аргументов запроса.
|
|
42
30
|
* @param options - Опции для генерации ключа.
|
|
@@ -133,7 +121,7 @@ function shouldUseUncache(uncacheOption) {
|
|
|
133
121
|
* @param config - Конфигурация для кеша Redis и TTL по умолчанию.
|
|
134
122
|
* @returns Prisma расширение.
|
|
135
123
|
*/
|
|
136
|
-
exports.default = ({ cache, debug }) => {
|
|
124
|
+
exports.default = ({ cache, debug, ttl: defaultTTL }) => {
|
|
137
125
|
return extension_1.Prisma.defineExtension({
|
|
138
126
|
name: "prisma-extension-cache-manager",
|
|
139
127
|
client: {
|
|
@@ -200,10 +188,15 @@ exports.default = ({ cache, debug }) => {
|
|
|
200
188
|
// Функция генерирует ключ на основе результатов
|
|
201
189
|
cacheKey = cacheOption.key(result);
|
|
202
190
|
ttl = cacheOption.ttl;
|
|
191
|
+
if (ttl === undefined) {
|
|
192
|
+
ttl = defaultTTL;
|
|
193
|
+
}
|
|
203
194
|
// Сохраняем результат в кеш
|
|
204
195
|
try {
|
|
205
196
|
const encoded = serialize(result);
|
|
206
|
-
await
|
|
197
|
+
await (ttl && ttl > 0
|
|
198
|
+
? cache.store.client.set(cacheKey, encoded, "EX", ttl / 1000)
|
|
199
|
+
: await cache.store.client.set(cacheKey, encoded));
|
|
207
200
|
if (debug) {
|
|
208
201
|
console.log("Data cached with key (function):", cacheKey, "encoded:", encoded, "decoded:", result);
|
|
209
202
|
}
|
|
@@ -226,9 +219,9 @@ exports.default = ({ cache, debug }) => {
|
|
|
226
219
|
if (!isWriteOperation) {
|
|
227
220
|
try {
|
|
228
221
|
// Используем getBuffer, т.к. сохраняем бинарные данные
|
|
229
|
-
const cached = await cache.
|
|
222
|
+
const cached = await cache.store.client.getBuffer(cacheKey);
|
|
230
223
|
if (cached) {
|
|
231
|
-
const data = deserialize(
|
|
224
|
+
const data = deserialize(cached);
|
|
232
225
|
if (debug) {
|
|
233
226
|
console.log("Cache hit for key:", cacheKey, "data", cached, "decoded", data);
|
|
234
227
|
}
|
|
@@ -252,10 +245,15 @@ exports.default = ({ cache, debug }) => {
|
|
|
252
245
|
if (useUncache) {
|
|
253
246
|
await processUncache(cache, uncacheOption, result);
|
|
254
247
|
}
|
|
248
|
+
if (ttl === undefined) {
|
|
249
|
+
ttl = defaultTTL;
|
|
250
|
+
}
|
|
255
251
|
// 6. Сохраняем результат запроса в кеш
|
|
256
252
|
try {
|
|
257
253
|
const encoded = serialize(result);
|
|
258
|
-
await
|
|
254
|
+
await (ttl && ttl > 0
|
|
255
|
+
? cache.store.client.set(cacheKey, encoded, "EX", ttl / 1000)
|
|
256
|
+
: await cache.store.client.set(cacheKey, encoded));
|
|
259
257
|
if (debug) {
|
|
260
258
|
console.log("Data cached with key:", cacheKey, "encoded:", encoded, "decoded:", result);
|
|
261
259
|
}
|
package/dist/types.d.ts
CHANGED