Skip to content

Commit

Permalink
feat(all) add-incrBy (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
icedrone authored Dec 28, 2023
1 parent dc026af commit 6c8ef56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ class buildRedisStoreWithConfig implements RedisStore {
};

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

public async atomicGetAndSet(key: string, updateFunction: (val: any) => any): Promise<RedisCommandRawReply> {

Check failure on line 115 in src/redis.ts

View workflow job for this annotation

GitHub Actions / Release

Property 'atomicGetAndSet' in type 'buildRedisStoreWithConfig' is not assignable to the same property in base type 'RedisStore'.
await this.redisCache.watch(key);
return await this.redisCache.multi().set(key, updateFunction(await this.get(key))).get(key).exec();
}

public async incrBy(key: string, incrementBy: number): Promise<number> {
return await this.redisCache.incrBy(key, incrementBy);
}

public async flushAll() {
await this.redisCache.flushAll();
}
Expand Down
14 changes: 8 additions & 6 deletions src/types/RedisStore.interface.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Store } from "cache-manager";
import { RedisClientType, RedisDefaultModules, RedisFunctions, RedisModules, RedisScripts } from "redis";
import { ScanReply } from '@redis/client/dist/lib/commands/SCAN';
import { RedisCommandRawReply } from "@redis/client/dist/lib/commands";
import {Store} from "cache-manager";
import {RedisClientType, RedisDefaultModules, RedisFunctions, RedisModules, RedisScripts} from "redis";
import {ScanReply} from '@redis/client/dist/lib/commands/SCAN';
import {RedisCommandRawReply} from "@redis/client/dist/lib/commands";

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

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

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

atomicGetAndSet(key: string, updateFunction: (val: any) => any): Promise<RedisCommandRawReply>;
atomicGetAndSet(key: string, updateFunction: (val: any) => any): Promise<RedisCommandRawReply[]>;

incrBy(key: string, incrementBy: number): Promise<number>;

flushAll(): Promise<void>

Expand Down
14 changes: 13 additions & 1 deletion test/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ describe('Redis Store', () => {
expect(retrievedTtl).toBeLessThanOrEqual(ttl / 1000); // Redis returns TTL in seconds
});

it('should increment a value', async () => {
const key = 'testKey';
const value = 1;

await redisClient.set(key, value);
const numberIncrBy = await redisClient.incrBy(key, 1);

const retrievedValue = await redisClient.get(key);
expect(numberIncrBy).toEqual(2);
expect(retrievedValue).toEqual(2);
});

it('should return scan result by pattern', async () => {
const key1 = 'ttl:a:b';
const key2 = 'ttl1:a:b';
Expand Down Expand Up @@ -120,7 +132,7 @@ describe('Redis Store', () => {
parsedVal.a = parsedVal.a + 1;
return JSON.stringify(parsedVal);
});
expect(JSON.parse(res[1])).to.deep.equal({ a: 2 });
expect(JSON.parse(res[1] as string)).to.deep.equal({ a: 2 });
expect(res[0]).to.deep.equal("OK");
})

Expand Down

0 comments on commit 6c8ef56

Please sign in to comment.