Skip to content

Commit

Permalink
feat: add Redis as cache (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolu89 authored Feb 11, 2023
1 parent aa5f804 commit f59d46f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PROXY_URL=wss://your-proxy.com
RELAYS=wss://relay1.com,wss://relay2.com,wss://relay.com
RELAYS=wss://relay1.com,wss://relay2.com,wss://relay.com
REDIS=redis://user:password@localhost:6379
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@fastify/sensible": "^5.0.0",
"@fastify/view": "^7.4.1",
"@fastify/websocket": "^7.1.2",
"@keyv/redis": "^2.5.5",
"ejs": "^3.1.8",
"fastify": "^4.0.0",
"fastify-cli": "^5.7.1",
Expand Down
66 changes: 66 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 5 additions & 2 deletions src/services/WebSocketPool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from "events";
import WebSocket from "ws";
import { getRelays } from "./env";
import { getRedis, getRelays } from "./env";
import Keyv from "keyv";
import crypto from "crypto";
import { EventType, parseEvent } from "./Event";
Expand All @@ -20,7 +20,10 @@ class WebSocketPool extends EventEmitter {
this.maxAttempts = maxAttempts;
this.resetTimeout = resetTimeout;
this.sockets = {};
this.cache = new Keyv();
const redis = getRedis()
console.log(redis)
this.cache = redis !== null ? new Keyv(redis) : new Keyv();


this.connectAll();
}
Expand Down
9 changes: 9 additions & 0 deletions src/services/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let proxyUrl = "";
let relays: string[] = [];
let redis: string | null = null;

export function validateEnv() {
if (!process.env.PROXY_URL) {
Expand All @@ -18,6 +19,10 @@ export function validateEnv() {
console.error("A problem occured while parsing relays");
process.exit(1);
}

if (process.env.REDIS) {
redis = process.env.REDIS
}
}

export function getRelays() {
Expand All @@ -26,4 +31,8 @@ export function getRelays() {

export function getProxyUrl() {
return proxyUrl;
}

export function getRedis() {
return redis;
}

0 comments on commit f59d46f

Please sign in to comment.