Skip to content

Commit

Permalink
add useRedisSets option in clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Llontop committed Jun 22, 2023
1 parent bcd3b0e commit c1c561b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@typescript-eslint/no-unsafe-assignment": 0,
"@typescript-eslint/no-unsafe-return": 0,
"unicorn/prefer-ternary": 0,
"unicorn/no-array-callback-reference": 0,
"ava/no-ignored-test-files": [
"error",
{
Expand Down
38 changes: 24 additions & 14 deletions packages/redis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,27 @@ class KeyvRedis<Value = any> extends EventEmitter {
options = {...(typeof uri === 'string' ? {uri} : uri as KeyvRedisOptions), ...options};
// @ts-expect-error - uri is a string or RedisOptions
this.redis = new Redis(options.uri!, options);
this.opts.useRedisSets = options.useRedisSets;
}

this.redis.on('error', (error: Error) => this.emit('error', error));
}

_getNamespace(key?: string): string {
if (key && this.opts.useRedisSets) {
return `namespace:sets:${key}`;
}

_getNamespace(): string {
return `namespace:${this.namespace!}`;
}

_getKeyName = (key: string): string => {
if (this.opts.useRedisSets) {
return `sets:${key}`;
}

return key;
};

async get(key: string): GetOutput<Value> {
key = this._getKeyName(key);

const value: Value = await this.redis.get(key);
if (value === null) {
return undefined;
Expand All @@ -53,6 +60,7 @@ class KeyvRedis<Value = any> extends EventEmitter {
}

async getMany(keys: string[]): GetManyOutput<Value> {
keys = keys.map(this._getKeyName);
return this.redis.mget(keys);
}

Expand All @@ -61,32 +69,34 @@ class KeyvRedis<Value = any> extends EventEmitter {
return undefined;
}

if (this.opts.useRedisSets) {
await this.redis.set(this._getNamespace(key), value);
key = this._getKeyName(key);

if (typeof ttl === 'number') {
await this.redis.set(key, value, 'PX', ttl);
} else {
if (typeof ttl === 'number') {
await this.redis.set(key, value, 'PX', ttl);
} else {
await this.redis.set(key, value);
}
await this.redis.set(key, value);
}

if (!this.opts.useRedisSets) {
await this.redis.sadd(this._getNamespace(), key);
}
}

async delete(key: string): DeleteOutput {
key = this._getKeyName(key);
const items: number = await this.redis.del(key);
await this.redis.srem(this._getNamespace(), key);
return items > 0;
}

async deleteMany(key: string): DeleteManyOutput {
async deleteMany(keys: string): DeleteManyOutput {
const key = this._getKeyName(keys);
return this.delete(key);
}

async clear(): ClearOutput {
if (this.opts.useRedisSets) {
const pattern = 'namespace:sets*';
const pattern = 'sets:*';
const keys: string[] = await this.redis.keys(pattern);
await this.redis.del(keys);
} else {
Expand Down
25 changes: 25 additions & 0 deletions packages/redis/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,28 @@ test.serial('should handle KeyvOptions with family option', t => {
const keyv = new KeyvRedis(options);
t.true(keyv.redis instanceof Redis);
});

test.serial('set method should use Redis sets when useRedisSets is true', async t => {
const options = {useRedisSets: true};
const keyv = new KeyvRedis(options);

await keyv.set('foo', 'bar');

const value = await keyv.get('foo');
t.is(value, 'bar');
});

test.serial('clear method using useRedisSets should clear all keys', async t => {
const options = {useRedisSets: true};
const keyv = new KeyvRedis(options);

await keyv.set('foo', 'bar');
await keyv.set('foo2', 'bar2');

await keyv.clear();

const value = await keyv.get('demo');
const value2 = await keyv.get('demo2');
t.is(value, undefined);
t.is(value2, undefined);
});

0 comments on commit c1c561b

Please sign in to comment.