Skip to content

Commit

Permalink
added new features
Browse files Browse the repository at this point in the history
- fetch list members
- get client
- get ttl by key
- get/remove by pattern
  • Loading branch information
wjamilaion committed Oct 26, 2023
1 parent 8a44a00 commit 3758af4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
13 changes: 11 additions & 2 deletions __test__/redis-client/redis-client.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ describe('RedisClientService', () => {
});
describe('set/get value value', () => {
const key = 'unit-test',
value = 'ok';
value = 'ok',
ttl = 120;

it('should able to set key in redis', async () => {
expect(await redisClientService.setValue(key, value, 120)).toBe(true);
expect(await redisClientService.setValue(key, value, ttl)).toBe(true);
});

it('should be able to fetch value from redis', async () => {
Expand All @@ -52,6 +53,14 @@ describe('RedisClientService', () => {
expect(await redisClientService.keys()).toStrictEqual([key]);
});

it('should be able to fetch keys with pattern from redis', async () => {
expect(await redisClientService.getKeysWithPattern(`${key}*`)).toStrictEqual([key]);
});

it('should be able to fetch ttl from redis', async () => {
expect(await redisClientService.getTTL(key)).toBe(ttl);
});

it('should be able to del key from redis', async () => {
expect(await redisClientService.delValue(key)).toBeUndefined();
expect(await redisClientService.getValue(key)).toBeUndefined();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-redis-connector",
"version": "1.0.3",
"version": "1.0.4",
"description": "redis connector for NestJs based services",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
27 changes: 21 additions & 6 deletions src/redis-client/redis-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class RedisClientService implements OnModuleDestroy {
constructor(@Inject(CACHE_MANAGER) private cache: Cache) {}

redisClient() {
(this.cache.store as RedisStore).client;
return (this.cache.store as RedisStore).client;
}

onModuleDestroy() {
Expand Down Expand Up @@ -47,19 +47,27 @@ export class RedisClientService implements OnModuleDestroy {
}

async sadd(listName: string, member: string) {
(this.cache.store as RedisStore).client.sadd(listName, member);
this.redisClient().sadd(listName, member);
}

async srem(listName: string, member: string) {
(this.cache.store as RedisStore).client.srem(listName, member);
this.redisClient().srem(listName, member);
}

async smembers(listName: string): Promise<string[]> {
return this.redisClient().smembers(listName);
}

async reset(): Promise<void> {
await this.cache.reset();
}

async keys(): Promise<string[]> {
return this.cache.store.keys();
async keys(pattern?: string): Promise<string[]> {
return this.cache.store.keys(pattern);
}

async getKeysWithPattern(pattern: string): Promise<string[]> {
return this.keys(pattern);
}

async delValue(key: string): Promise<void> {
Expand All @@ -69,7 +77,14 @@ export class RedisClientService implements OnModuleDestroy {
async delMValue(...key: string[]): Promise<void> {
await this.cache.store.mdel(...key);
}
async delValueWithPattern(key: string): Promise<any> {
const keys = await this.keys(key);
return this.delMValue(...keys);
}
async getTTL(key: string): Promise<any> {
return await this.redisClient().ttl(key);
}
disconnect(): void {
(this.cache.store as RedisStore).client.disconnect?.();
this.redisClient().disconnect?.();
}
}

0 comments on commit 3758af4

Please sign in to comment.