Skip to content

Commit

Permalink
refactor: Optimize cache operations with concurrent processes
Browse files Browse the repository at this point in the history
- Optimized cache handling logic for better concurrency and efficiency.
- Enhanced error handling for non-existing files.
- Improved cache retrieval and unlocking process.
  • Loading branch information
ITJesse committed Nov 13, 2023
1 parent daa8611 commit 855a743
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ export class Cache {
while (await this.cacheLocker.isLocked()) {
await delay(10)
}
await this.cacheLocker.lock()
const cached = await redisClient.hgetall(this.key)
const [_, cached] = await Promise.all([
this.cacheLocker.lock(),
redisClient.hgetall(this.key),
])
const { file, timestamp } = cached
if (!file) return [null]
const filePath = getCacheFilePath(file)
if (!fs.existsSync(filePath)) {
await redisClient.del(this.key)
await Promise.all([
redisClient.del(this.key),
redisClient.zrem('cache_access_count', this.key),
])
return [null]
}
await this.cacheLocker.unlock()
await Promise.all([
redisClient.zincrby('cache_access_count', 1, this.key),
this.cacheLocker.unlock(),
])
return [filePath, Date.now() - parseInt(timestamp)]
}

Expand Down

0 comments on commit 855a743

Please sign in to comment.