Skip to content

Commit

Permalink
use single set function with expiry integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghel committed Nov 14, 2023
1 parent de518fe commit e5a5d13
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/cache/src/redlock/redlock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ export class RedlockService {
}

private async lockOnce(key: string, keyExpiration: number): Promise<boolean> {
const result = await this.redis.setnx(key, '1');
if (result === 1) {
await this.redis.pexpire(key, keyExpiration);
}
// Using SET command with NX and PX options
const result = await this.redis.set(key, '1', 'PX', keyExpiration, 'NX');

return result === 1;
// The SET command with NX returns null if the key already exists
return result !== null;
}

private sleep(ms: number): Promise<void> {
Expand Down

0 comments on commit e5a5d13

Please sign in to comment.