feat: cache server

This commit is contained in:
ThatOneCalculator 2023-07-06 11:50:34 -07:00
parent 53ec5f7048
commit f5c7b6f55f
4 changed files with 44 additions and 9 deletions

View file

@ -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.

View file

@ -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

View file

@ -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;

View file

@ -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,
});
}