|
1 |
| -import type { CacheAdapter } from "@mikro-orm/core"; |
| 1 | +import { serialize, deserialize } from "node:v8"; |
2 | 2 | import IORedis from "ioredis";
|
| 3 | + |
| 4 | +import type { CacheAdapter } from "@mikro-orm/core"; |
3 | 5 | import type { Redis, RedisOptions } from "ioredis";
|
4 | 6 |
|
5 | 7 | export interface BaseOptions {
|
@@ -67,32 +69,57 @@ export class RedisCacheAdapter implements CacheAdapter {
|
67 | 69 | return `${this.keyPrefix}:${name}`;
|
68 | 70 | }
|
69 | 71 |
|
70 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
71 |
| - async get<T = any>(key: string): Promise<T | undefined> { |
| 72 | + async get<T = unknown>(key: string): Promise<T | undefined> { |
72 | 73 | const completeKey = this._getKey(key);
|
73 |
| - const data = await this.client.get(completeKey); |
74 |
| - this.logDebugMessage(`Get "${completeKey}": "${data}"`); |
75 |
| - if (!data) return undefined; |
76 |
| - return JSON.parse(data); |
| 74 | + const data = await this.client.getBuffer(completeKey); |
| 75 | + |
| 76 | + if (!data) { |
| 77 | + this.logDebugMessage(`Get "${completeKey}": "null"`); |
| 78 | + return undefined; |
| 79 | + } |
| 80 | + |
| 81 | + let deserialized: T; |
| 82 | + try { |
| 83 | + deserialized = deserialize(data) as T; |
| 84 | + } catch (error) { |
| 85 | + this.logDebugMessage(`Failed to deserialize data: ${data}`); |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + |
| 89 | + this.logDebugMessage( |
| 90 | + `Get "${completeKey}": "${JSON.stringify(deserialized)}"` |
| 91 | + ); |
| 92 | + |
| 93 | + return deserialized; |
77 | 94 | }
|
78 | 95 |
|
79 | 96 | async set(
|
80 | 97 | key: string,
|
81 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
82 |
| - data: any, |
| 98 | + data: unknown, |
83 | 99 | _origin: string,
|
84 | 100 | expiration = this.expiration
|
85 | 101 | ): Promise<void> {
|
86 |
| - const stringData = JSON.stringify(data); |
| 102 | + let serialized: Buffer; |
| 103 | + try { |
| 104 | + serialized = serialize(data); |
| 105 | + } catch (error) { |
| 106 | + this.logDebugMessage(`Failed to serialize data: ${data}`); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
87 | 110 | const completeKey = this._getKey(key);
|
88 |
| - this.logDebugMessage( |
89 |
| - `Set "${completeKey}": "${stringData}" with cache expiration ${expiration}ms` |
90 |
| - ); |
| 111 | + |
91 | 112 | if (expiration) {
|
92 |
| - await this.client.set(completeKey, stringData, "PX", expiration); |
| 113 | + await this.client.set(completeKey, serialized, "PX", expiration); |
93 | 114 | } else {
|
94 |
| - await this.client.set(completeKey, stringData); |
| 115 | + await this.client.set(completeKey, serialized); |
95 | 116 | }
|
| 117 | + |
| 118 | + this.logDebugMessage( |
| 119 | + `Set "${completeKey}": "${JSON.stringify( |
| 120 | + data |
| 121 | + )}" with cache expiration ${expiration}ms` |
| 122 | + ); |
96 | 123 | }
|
97 | 124 |
|
98 | 125 | async remove(name: string): Promise<void> {
|
|
0 commit comments