Skip to content

Commit 6b2a252

Browse files
authored
fix(be): set a key before incrby for none existing key (#19)
1 parent 3be02f3 commit 6b2a252

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/redis.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ class buildRedisStoreWithConfig implements RedisStore {
117117
return await this.redisCache.multi().set(key, updateFunction(await this.get(key))).get(key).exec();
118118
}
119119

120-
public async incrBy(key: string, incrementBy: number): Promise<number> {
120+
public async incrBy(key: string, incrementBy: number, ttl?: Milliseconds): Promise<number> {
121+
const ttlValue = this.getTtl(ttl);
122+
await this.redisCache.set(key, 0, {PX: ttlValue, NX: true});
121123
return await this.redisCache.incrBy(key, incrementBy);
122124
}
123125

test/redis.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ describe('Redis Store', () => {
8888

8989
it('should increment a value', async () => {
9090
const key = 'testKey';
91-
const value = 1;
9291

93-
await redisClient.set(key, value);
94-
const numberIncrBy = await redisClient.incrBy(key, 1);
92+
const numberIncrBy = await redisClient.incrBy(key, 2);
9593

9694
const retrievedValue = await redisClient.get(key);
95+
const ttl = await redisClient.ttl(key);
96+
expect(ttl).to.not.equal(-1);
9797
expect(numberIncrBy).toEqual(2);
9898
expect(retrievedValue).toEqual(2);
9999
});

0 commit comments

Comments
 (0)