From 1548250360d32ee8d66ec1d3f320a1847cf2b65a Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Thu, 6 Jul 2023 11:50:34 -0700 Subject: [PATCH] feat: :zap: cache server --- .config/example.yml | 14 ++++++++++++++ README.md | 9 ++++++++- packages/backend/src/config/types.ts | 10 ++++++++++ packages/backend/src/db/redis.ts | 20 ++++++++++++-------- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/.config/example.yml b/.config/example.yml index 51d380e7e..ba74df8a5 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -67,6 +67,20 @@ redis: #db: 1 #user: default +# ┌─────────────────────────────┐ +#───┘ Cache server configuration └───────────────────────────────────── + +# A Redis-compatible server (DragonflyDB, Keydb, Redis) for caching +# If left blank, it will use the Redis server from above + +#cacheServer: + #host: localhost + #port: 6379 + #family: 0 # 0=Both, 4=IPv4, 6=IPv6 + #pass: example-pass + #prefix: example-prefix + #db: 1 + # Please configure either MeiliSearch *or* Sonic. # If both MeiliSearch and Sonic configurations are present, MeiliSearch will take precedence. diff --git a/README.md b/README.md index f66d14a32..0ad935667 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,10 @@ If you have access to a server that supports one of the sources below, I recomme - 🦔 [Sonic](https://crates.io/crates/sonic-server) - [MeiliSearch](https://www.meilisearch.com/) - [ElasticSearch](https://www.elastic.co/elasticsearch/) - +- Caching server + - 🐲 At least [Dragonfly](https://www.dragonflydb.io/) v1.4.0 (recommended) + - 👻 [KeyDB](https://keydb.dev/) (untested) + - 🍱 Another [Redis](https://redis.io/) server, at least v6 ### 🏗️ Build dependencies - 🦀 At least [Rust](https://www.rust-lang.org/) v1.68.0 @@ -161,6 +164,10 @@ psql postgres -c "create database calckey with encoding = 'UTF8';" In Calckey's directory, fill out the `db` section of `.config/default.yml` with the correct information, where the `db` key is `calckey`. +## 💰 Caching server + +If you experience a lot of traffic, it's a good idea to set up another Redis-compatible caching server. If you don't set one one up, it'll falll back to the mandatory Redis server. + ## 🔎 Set up search ### 🦔 Sonic diff --git a/packages/backend/src/config/types.ts b/packages/backend/src/config/types.ts index 84808413c..43168662c 100644 --- a/packages/backend/src/config/types.ts +++ b/packages/backend/src/config/types.ts @@ -26,6 +26,16 @@ export type Source = { user?: string; tls?: { [y: string]: string }; }; + cacheServer: { + host: string; + port: number; + family?: number; + pass?: string; + db?: number; + prefix?: string; + user?: string; + tls?: { [z: string]: string }; + }; elasticsearch: { host: string; port: number; diff --git a/packages/backend/src/db/redis.ts b/packages/backend/src/db/redis.ts index a1f3279f3..215effd8e 100644 --- a/packages/backend/src/db/redis.ts +++ b/packages/backend/src/db/redis.ts @@ -2,15 +2,19 @@ import Redis from "ioredis"; import config from "@/config/index.js"; export function createConnection() { + let source = config.redis; + if (config.cacheServer) { + source = config.cacheServer; + } return new Redis({ - port: config.redis.port, - host: config.redis.host, - family: config.redis.family ?? 0, - password: config.redis.pass, - username: config.redis.user ?? "default", - keyPrefix: `${config.redis.prefix}:`, - db: config.redis.db || 0, - tls: config.redis.tls, + port: source.port, + host: source.host, + family: source.family ?? 0, + password: source.pass, + username: source.user ?? "default", + keyPrefix: `${source.prefix}:`, + db: source.db || 0, + tls: source.tls, }); }