Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Dockerfile
docker-compose.yml

.agents
.claude
.claude
CLAUDE.md
7 changes: 6 additions & 1 deletion bun.lock

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
"hono-pino": "^0.10.3",
"hono-rate-limiter": "^0.5.3",
"hono-zod-openapi": "^1.1.1",
"ioredis": "^5.10.1",
"nodemailer": "^8.0.7",
"pg": "^8.21.0",
"pino": "^10.3.1"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/bcryptjs": "^3.0.0",
"@types/bun": "^1.3.14",
"@types/crypto-js": "^4.2.2",
Expand All @@ -63,6 +63,7 @@
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-prettier": "^5.5.5",
"eslint-plugin-simple-import-sort": "^13.0.0",
"globals": "^17.6.0",
"husky": "^9.1.7",
"lint-staged": "^17.0.5",
"prettier": "^3.8.3",
Expand Down
4 changes: 1 addition & 3 deletions src/bull/queue/send-email.queue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Queue } from "bullmq";
import { RedisClient } from "@database";

const queueRedis = RedisClient.getQueueRedisClient();

export const sendEmailQueue = new Queue("send-email", {
connection: queueRedis,
connection: RedisClient.getQueueConnection(),
});
4 changes: 1 addition & 3 deletions src/bull/worker/send-email.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { RedisClient } from "@database";
import { logger } from "@utils";
import { EmailOptions, EmailService } from "@mail/mail.service";

const queueRedis = RedisClient.getQueueRedisClient();

const worker = new Worker<EmailOptions>(
"send-email",
async (job) => {
Expand All @@ -17,7 +15,7 @@ const worker = new Worker<EmailOptions>(
}
},
{
connection: queueRedis,
connection: RedisClient.getQueueConnection(),
},
);

Expand Down
44 changes: 17 additions & 27 deletions src/libs/cache/cache.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import Redis from "ioredis";
import type { RedisClient as BunRedisClient } from "bun";
import { logger } from "@utils";
import { RedisClient } from "@database";

class Cache {
private static redis: Redis | null = null;

private static getRedisClient(): Redis {
if (!this.redis) {
this.redis = RedisClient.getRedisClient();
}

return this.redis;
private static getClient(): BunRedisClient {
return RedisClient.getRedisClient();
}

static async get<T>(key: string): Promise<T | null> {
try {
const client = this.getRedisClient();
const value = await client.get(key);
const value = await this.getClient().get(key);
return value ? (JSON.parse(value) as T) : null;
} catch (error) {
logger.error(error, `Error getting cache for key ${key}:`);
Expand All @@ -30,36 +23,37 @@ class Cache {
ttl: number = 3600,
): Promise<void> {
try {
const client = this.getRedisClient();
await client.set(key, JSON.stringify(value), "EX", ttl);
await this.getClient().send("SET", [
key,
JSON.stringify(value),
"EX",
String(ttl),
]);
} catch (error) {
logger.error(error, `Error setting cache for key ${key}:`);
}
}

static async delete(key: string): Promise<void> {
try {
const client = this.getRedisClient();
await client.del(key);
await this.getClient().del(key);
} catch (error) {
logger.error(error, `Error deleting cache for key ${key}:`);
}
}

static async flush(): Promise<void> {
try {
const client = this.getRedisClient();
await client.flushdb();
await this.getClient().send("FLUSHDB", []);
} catch (error) {
logger.error(error, "Error flushing Redis cache:");
}
}

static async exists(key: string): Promise<boolean> {
try {
const client = this.getRedisClient();
const exists = await client.exists(key);
return exists === 1;
const exists = await this.getClient().exists(key);
return exists === true;
} catch (error) {
logger.error(error, `Error checking existence of key ${key}:`);
return false;
Expand Down Expand Up @@ -87,21 +81,17 @@ class Cache {

static async getKeys(pattern: string): Promise<string[]> {
try {
const client = this.getRedisClient();
const keys = await client.keys(pattern);
const keys = await this.getClient().keys(pattern);
return keys;
} catch (error) {
logger.error(error, `Error getting keys with pattern ${pattern}:`);
return [];
}
}

static async disconnect(): Promise<void> {
static disconnect(): void {
try {
if (this.redis) {
await this.redis.quit();
this.redis = null;
}
this.getClient().close();
} catch (error) {
logger.error(error, "Error disconnecting from Redis:");
}
Expand Down
6 changes: 3 additions & 3 deletions src/libs/database/clickhouse/scripts/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ async function main() {
const migrator = new ClickHouseMigrator();
const command = process.argv[2];

let executed: string[] = [];
let all: Array<IMigrationFile> = [];
let pending: Array<IMigrationFile> = [];
let executed: string[];
let all: Array<IMigrationFile>;
let pending: Array<IMigrationFile>;

switch (command) {
case "migrate":
Expand Down
46 changes: 25 additions & 21 deletions src/libs/database/redis/redis-client.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import { RedisClient as BunRedisClient } from "bun";
import { RedisConfig } from "@config";
import Redis from "ioredis";

const buildUrl = (): string => {
const auth = RedisConfig.REDIS_PASSWORD
? `:${encodeURIComponent(RedisConfig.REDIS_PASSWORD)}@`
: "";
return `redis://${auth}${RedisConfig.REDIS_HOST}:${RedisConfig.REDIS_PORT}`;
};

export interface QueueConnectionOptions {
host: string;
port: number;
password?: string;
maxRetriesPerRequest: null;
}

export class RedisClient {
private static redis: Redis | null = null;
private static queueRedis: Redis | null = null;
private static redis: BunRedisClient | null = null;

static getRedisClient(): Redis {
static getRedisClient(): BunRedisClient {
if (!this.redis) {
this.redis = new Redis({
host: RedisConfig.REDIS_HOST,
port: RedisConfig.REDIS_PORT,
password: RedisConfig.REDIS_PASSWORD || undefined,
});
this.redis = new BunRedisClient(buildUrl());
}

return this.redis;
}

static getQueueRedisClient(): Redis {
if (!this.queueRedis) {
this.queueRedis = new Redis({
host: RedisConfig.REDIS_HOST,
port: RedisConfig.REDIS_PORT,
password: RedisConfig.REDIS_PASSWORD || undefined,
maxRetriesPerRequest: null,
});
}

return this.queueRedis;
static getQueueConnection(): QueueConnectionOptions {
return {
host: RedisConfig.REDIS_HOST,
port: RedisConfig.REDIS_PORT,
password: RedisConfig.REDIS_PASSWORD || undefined,
maxRetriesPerRequest: null,
};
}
}
4 changes: 2 additions & 2 deletions src/modules/home/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ HomeRoutes.openapi(HealthRoute, async (c) => {
// Redis remains unhealthy
}

// Check Redis Queue
// Check Redis Queue (shares the same Redis instance)
try {
const start = Date.now();
await RedisClient.getQueueRedisClient().ping();
await RedisClient.getRedisClient().ping();
services.redisQueue = {
status: "healthy",
responseTime: Date.now() - start,
Expand Down
Loading