diff --git a/package-lock.json b/package-lock.json index cb97eb1..5fc06e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,7 +43,7 @@ "typescript": "^3.7.2" }, "engines": { - "node": ">=10.15.0" + "node": ">=20.15.0" } }, "node_modules/@acuris/eslint-config": { diff --git a/src/storage/redis-cache-store.ts b/src/storage/redis-cache-store.ts index 6e1b882..9cf4419 100644 --- a/src/storage/redis-cache-store.ts +++ b/src/storage/redis-cache-store.ts @@ -9,89 +9,36 @@ function lockKey(key: string): string { export class RedisCacheStore implements CacheStore { public constructor(private redisClient: RedisClientType) {} - public get(key: string): Promise | null> { - return new Promise | null>((resolve, reject) => { - this.redisClient - .get(key) - .then((result: any) => { - if (!result) { - resolve(null) - } - resolve(JSON.parse(result)) - }) - .catch((error: Error) => { - reject(error) - }) - }) + public async get(key: string): Promise | null> { + const result = await this.redisClient.get(key) + return JSON.parse(result) } - public set(key: string, data: CacheItem, ttl: number): Promise { - return new Promise((resolve, reject) => { - this.redisClient - .set(key, JSON.stringify(data), { PX: ttl }) - .then((result: any) => { - if (result === 'OK') { - resolve(true) - } - resolve(false) - }) - .catch((error: Error) => { - reject(error) - }) - }) + public async set(key: string, data: CacheItem, ttl: number): Promise { + const result = await this.redisClient.set(key, JSON.stringify(data), { PX: ttl }) + return result === 'OK' } - public del(key: string): Promise { - return new Promise((resolve, reject) => { - this.redisClient - .del(key) - .then((result: any) => { - resolve(result > 0) - }) - .catch((error: Error) => { - reject(error) - }) - }) + public async del(key: string): Promise { + const result = await this.redisClient.del(key) + return result > 0 } - public lock(key: string, ttl: number): Promise { + public async lock(key: string, ttl: number): Promise { const lockId = uuidV4() - return new Promise((resolve, reject) => { - this.redisClient - .set(lockKey(key), lockId, { PX: ttl, NX: true }) - .then((result: any) => { - if (result === 'OK') { - resolve(lockId) - } - resolve(false) - }) - .catch((error: Error) => { - reject(error) - }) - }) + const result = await this.redisClient.set(lockKey(key), lockId, { PX: ttl, NX: true }) + if (result === 'OK') { + return lockId + } + return false } - public unlock(key: string, lockId: string): Promise { - return new Promise((resolve, reject) => { - this.redisClient - .get(lockKey(key)) - .then((result: any) => { - if (result && result === lockId) { - this.redisClient - .del(lockKey(key)) - .then(() => { - resolve(true) - }) - .catch((delError: Error) => { - reject(delError) - }) - } else { - resolve(false) - } - }) - .catch((error: Error) => { - reject(error) - }) - }) + public async unlock(key: string, lockId: string): Promise { + const result = await this.redisClient.get(lockKey(key)) + if (result && result === lockId) { + await this.redisClient.del(lockKey(key)) + return true + } + return false } }