Skip to content

Commit 1391a31

Browse files
authored
Merge pull request #503 from elizaOS/fix/swap-compressed-cache
atomic set and expire
2 parents 6582ef1 + 0c24078 commit 1391a31

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

packages/server/src/redis.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createPool, Pool } from "generic-pool";
22
import { Redis } from "ioredis";
33
import { logger } from "./util";
4-
import { gzipSync, gunzipSync } from 'zlib';
4+
import { gzipSync, gunzipSync } from "zlib";
55

66
let globalRedisCachePromise: Promise<RedisCacheService> | null = null;
77

@@ -45,7 +45,7 @@ const DEFAULT_REDIS_PORT = 6379;
4545
const DEFAULT_REDIS_PASSWORD = "MDikUKnhRHlURlnORexvVztDTrNCUBze";
4646

4747
export class RedisCacheService {
48-
constructor(public redisPool: RedisPool) { }
48+
constructor(public redisPool: RedisPool) {}
4949

5050
async isPoolReady(): Promise<boolean> {
5151
try {
@@ -78,7 +78,7 @@ export class RedisCacheService {
7878
}
7979

8080
getKey(key: string) {
81-
if (!key) throw new Error('Redis key must be a non-empty string');
81+
if (!key) throw new Error("Redis key must be a non-empty string");
8282
const prefix = `${process.env.NETWORK}:`;
8383
if (key.startsWith(prefix)) {
8484
return key;
@@ -201,20 +201,21 @@ export class RedisCacheService {
201201
const raw = Buffer.from(JSON.stringify(obj), "utf8");
202202
const comp = gzipSync(raw);
203203

204-
await this.redisPool.useClient(async client => {
205-
await client.set(this.getKey(key), comp);
204+
await this.redisPool.useClient(async (client) => {
206205
if (ttlInSeconds) {
207-
await client.expire(this.getKey(key), ttlInSeconds);
206+
await client.set(this.getKey(key), comp, "EX", ttlInSeconds);
207+
} else {
208+
await client.set(this.getKey(key), comp);
208209
}
209210
});
210211
}
211212
async getCompressed<T>(key: string): Promise<T | null> {
212-
const buf: Buffer | null = await this.redisPool.useClient(c =>
213+
const buf: Buffer | null = await this.redisPool.useClient((c) =>
213214
c.getBuffer(this.getKey(key))
214215
);
215216
if (!buf) return null;
216217

217-
const json = gunzipSync(buf).toString('utf8');
218+
const json = gunzipSync(buf).toString("utf8");
218219
return JSON.parse(json) as T;
219220
}
220221

@@ -354,7 +355,6 @@ export class RedisPool {
354355
max: options.max || 500,
355356
min: options.min || 200,
356357
idleTimeoutMillis: options.idleTimeoutMillis || 60_000,
357-
358358
};
359359

360360
logger.info(
@@ -368,7 +368,7 @@ export class RedisPool {
368368
host: this.options.host,
369369
port: this.options.port,
370370
password: this.options.password || undefined, // Pass undefined if no password
371-
retryStrategy: attempts => Math.min(attempts * 50, 2000),
371+
retryStrategy: (attempts) => Math.min(attempts * 50, 2000),
372372
maxRetriesPerRequest: 3,
373373
connectTimeout: 3000,
374374
enableReadyCheck: true,

0 commit comments

Comments
 (0)