Skip to content

Commit

Permalink
fix(RedisAdapter): change redis client
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Sep 1, 2024
1 parent 1e41e44 commit 81b5ae6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 11 deletions.
5 changes: 3 additions & 2 deletions packages/redis-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"dev": "tsc --watch",
"build": "tsc",
"lint": "biome lint --write ./src",
"format": "biome format --write ./src"
"format": "biome format --write ./src",
"test": "node --test ./test/*"
},
"dependencies": {
"ioredis": "^5.4.1",
"iovalkey": "^0.1.0",
"seyfert": "^2.0.0"
},
"devDependencies": {
Expand Down
11 changes: 8 additions & 3 deletions packages/redis-adapter/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RedisOptions } from 'ioredis';
import type { RedisOptions } from 'iovalkey';
import type { Adapter } from 'seyfert/lib/cache';
import { Redis } from 'ioredis';
import { Redis } from 'iovalkey';

interface RedisAdapterOptions {
namespace?: string;
Expand Down Expand Up @@ -63,7 +63,12 @@ export class RedisAdapter implements Adapter {
pipeline.hgetall(this.buildKey(key));
}

return (await pipeline.exec())?.filter(x => !!x[1]).map(x => toNormal(x[1] as Record<string, any>)) ?? [];
return (
(await pipeline.exec())
?.filter(x => !!x[1])
.map(x => toNormal(x[1] as Record<string, any>))
.filter(x => x) ?? []
);
}

async get(keys: string): Promise<any> {
Expand Down
72 changes: 72 additions & 0 deletions packages/redis-adapter/test/adapter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const { strict } = require("node:assert/strict");
const { test, describe, after, before } = require('node:test');
const { RedisAdapter } = require('../lib/adapter');

describe('RedisAdapter', async () => {
const bulk = [['key1', { value: 'value1' }], ['key2', { value: 'value2' }]]

const adapter = new RedisAdapter({
redisOptions: {
host: 'localhost',
port: 6379,
},
namespace: 'test',
});

before(async () => {
// Clean the Redis instance before each test
await adapter.flush();

});

await test('constructor', () => {
strict.strictEqual(adapter.isAsync, true);
strict.strictEqual(adapter.namespace, 'test');
});

await test('get', async () => {
const result = await adapter.get('testKey');
strict.deepStrictEqual(result, undefined);
});

await test('set', async () => {
await strict.doesNotReject(async () => {
await adapter.set('testKey', { value: 'testValue' });
});
});

await test('bulkSet', async () => {
await strict.doesNotReject(async () => {
await adapter.bulkSet(bulk);
});
});

await test('bulkGet', async () => {
const result = await adapter.bulkGet(['key1', 'key2']);
strict.deepStrictEqual(result, bulk.map(x => x[1]));
});



await test('patch', async () => {
await strict.doesNotReject(async () => {
await adapter.patch(false, 'testKey', { newValue: 'updatedValue' });
});
});

await test('bulkPatch', async () => {
await strict.doesNotReject(async () => {
await adapter.bulkPatch(false, [['key1', { newValue: 'updatedValue1' }], ['key2', { newValue: 'updatedValue2' }]]);
});
});

await test('scan', async () => {
const result = await adapter.scan('test*');
strict.strictEqual(result.length, 3);
});

after(async () => {
await adapter.flush();
await adapter.client.quit();
});
});
37 changes: 37 additions & 0 deletions packages/redis-adapter/test/cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { describe, test, after, before } = require('node:test');
const { Cache, Client } = require('seyfert');
const { RedisAdapter } = require('../lib/index');
const { doesNotReject } = require('node:assert/strict');
const { setTimeout } = require('node:timers/promises');
const { doesNotThrow } = require('node:assert/strict');

// all intents
const intents = 53608447;

describe('Test Adapter cache', async t => {
const adapter = new RedisAdapter({
redisOptions: {
host: 'localhost',
port: 6379,
},
namespace: 'test_cache',
});

test('discord cache', async () => {
doesNotThrow(async () => {
const client = new Client({ getRC: () => ({ intents }) });
client.setServices({
cache: {
adapter,
}
})
await client.cache.testAdapter();
});
await setTimeout(5000);
});

after(async () => {
await adapter.flush()
await adapter.client.quit();
});
});
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 81b5ae6

Please sign in to comment.