Skip to content

Commit 717d9db

Browse files
author
EyalLevin
committed
feat(all) add-incrBy
1 parent dc026af commit 717d9db

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/redis.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,18 @@ class buildRedisStoreWithConfig implements RedisStore {
109109
};
110110

111111
public async scan(pattern: string, cursor: number = 0, count: number = 10): Promise<ScanReply> {
112-
return await this.redisCache.scan(cursor, { MATCH: pattern, COUNT: count });
112+
return await this.redisCache.scan(cursor, {MATCH: pattern, COUNT: count});
113113
}
114114

115115
public async atomicGetAndSet(key: string, updateFunction: (val: any) => any): Promise<RedisCommandRawReply> {
116116
await this.redisCache.watch(key);
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> {
121+
return await this.redisCache.incrBy(key, incrementBy);
122+
}
123+
120124
public async flushAll() {
121125
await this.redisCache.flushAll();
122126
}

src/types/RedisStore.interface.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { Store } from "cache-manager";
2-
import { RedisClientType, RedisDefaultModules, RedisFunctions, RedisModules, RedisScripts } from "redis";
3-
import { ScanReply } from '@redis/client/dist/lib/commands/SCAN';
4-
import { RedisCommandRawReply } from "@redis/client/dist/lib/commands";
1+
import {Store} from "cache-manager";
2+
import {RedisClientType, RedisDefaultModules, RedisFunctions, RedisModules, RedisScripts} from "redis";
3+
import {ScanReply} from '@redis/client/dist/lib/commands/SCAN';
4+
import {RedisCommandRawReply} from "@redis/client/dist/lib/commands";
55

66
export interface RedisStore extends Store {
77
isCacheableValue: (value: unknown) => boolean;
88

99
getClient(): RedisClientType<RedisDefaultModules & RedisModules, RedisFunctions, RedisScripts>;
1010

11-
scan(pattern: string, cursor? :number, count?: number): Promise<ScanReply>;
11+
scan(pattern: string, cursor?: number, count?: number): Promise<ScanReply>;
1212

13-
atomicGetAndSet(key: string, updateFunction: (val: any) => any): Promise<RedisCommandRawReply>;
13+
atomicGetAndSet(key: string, updateFunction: (val: any) => any): Promise<RedisCommandRawReply[]>;
14+
15+
incrBy(key: string, incrementBy: number): Promise<number>;
1416

1517
flushAll(): Promise<void>
1618

test/redis.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('Redis Store', () => {
8686
expect(retrievedTtl).toBeLessThanOrEqual(ttl / 1000); // Redis returns TTL in seconds
8787
});
8888

89+
it('should increment a value', async () => {
90+
const key = 'testKey';
91+
const value = 1;
92+
93+
await redisClient.set(key, value);
94+
const numberIncrBy = await redisClient.incrBy(key, 1);
95+
96+
const retrievedValue = await redisClient.get(key);
97+
expect(numberIncrBy).toEqual(2);
98+
expect(retrievedValue).toEqual(2);
99+
});
100+
89101
it('should return scan result by pattern', async () => {
90102
const key1 = 'ttl:a:b';
91103
const key2 = 'ttl1:a:b';
@@ -120,7 +132,7 @@ describe('Redis Store', () => {
120132
parsedVal.a = parsedVal.a + 1;
121133
return JSON.stringify(parsedVal);
122134
});
123-
expect(JSON.parse(res[1])).to.deep.equal({ a: 2 });
135+
expect(JSON.parse(res[1] as string)).to.deep.equal({ a: 2 });
124136
expect(res[0]).to.deep.equal("OK");
125137
})
126138

0 commit comments

Comments
 (0)