Skip to content

Commit

Permalink
refactor redis-cache-store to async-await
Browse files Browse the repository at this point in the history
  • Loading branch information
iona-andras-nemes committed Dec 13, 2024
1 parent c37a96e commit 140624a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 22 additions & 75 deletions src/storage/redis-cache-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,36 @@ function lockKey(key: string): string {
export class RedisCacheStore<T extends Cacheable = Cacheable> implements CacheStore<T> {
public constructor(private redisClient: RedisClientType<any>) {}

public get(key: string): Promise<CacheItem<T> | null> {
return new Promise<CacheItem<T> | 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<CacheItem<T> | null> {
const result = await this.redisClient.get(key)
return JSON.parse(result)
}

public set(key: string, data: CacheItem<T>, ttl: number): Promise<boolean> {
return new Promise<boolean>((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<T>, ttl: number): Promise<boolean> {
const result = await this.redisClient.set(key, JSON.stringify(data), { PX: ttl })
return result === 'OK'
}

public del(key: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
this.redisClient
.del(key)
.then((result: any) => {
resolve(result > 0)
})
.catch((error: Error) => {
reject(error)
})
})
public async del(key: string): Promise<boolean> {
const result = await this.redisClient.del(key)
return result > 0
}

public lock(key: string, ttl: number): Promise<string | false> {
public async lock(key: string, ttl: number): Promise<string | false> {
const lockId = uuidV4()
return new Promise<string | false>((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<boolean> {
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<boolean> {
const result = await this.redisClient.get(lockKey(key))
if (result && result === lockId) {
await this.redisClient.del(lockKey(key))
return true
}
return false
}
}

0 comments on commit 140624a

Please sign in to comment.