Merge pull request '[PR]: Cache server' (#10419) from refactor/cache-server into develop

Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10419
This commit is contained in:
Kainoa Kanter 2023-07-06 21:55:44 +00:00
commit 655cc039f3
9 changed files with 55 additions and 21 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 [DragonflyDB](https://www.dragonflydb.io/) v1.4.0 (recommended)
- 🍱 Another [Redis](https://redis.io/) server, at least v6
- 👻 [KeyDB](https://keydb.dev/) (untested)
### 🏗️ Build dependencies
- 🦀 At least [Rust](https://www.rust-lang.org/) v1.68.0
@ -161,6 +164,12 @@ 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 fall back to the mandatory Redis server.
For DragonflyDB, launch with the flag `--default_lua_flags='allow-undeclared-keys'`.
## 🔎 Set up search
### 🦔 Sonic

View File

@ -28,13 +28,11 @@
"@bull-board/api": "5.2.0",
"@bull-board/koa": "5.2.0",
"@bull-board/ui": "5.2.0",
"megalodon": "workspace:*",
"@discordapp/twemoji": "14.1.2",
"@elastic/elasticsearch": "7.17.0",
"@koa/cors": "3.4.3",
"@koa/multer": "3.0.2",
"@koa/router": "9.0.1",
"@msgpack/msgpack": "3.0.0-beta2",
"@peertube/http-signature": "1.7.0",
"@redocly/openapi-core": "1.0.0-beta.120",
"@sinonjs/fake-timers": "9.1.2",
@ -87,9 +85,11 @@
"koa-send": "5.0.1",
"koa-slow": "2.1.0",
"koa-views": "7.0.2",
"megalodon": "workspace:*",
"meilisearch": "0.33.0",
"mfm-js": "0.23.3",
"mime-types": "2.1.35",
"msgpackr": "1.9.5",
"multer": "1.4.4-lts.1",
"native-utils": "link:native-utils",
"nested-property": "4.0.0",

View File

@ -55,6 +55,8 @@ export default function load() {
mixin.clientEntry = clientManifest["src/init.ts"];
if (!config.redis.prefix) config.redis.prefix = mixin.host;
if (config.cacheServer && !config.cacheServer.prefix)
config.cacheServer.prefix = mixin.host;
return Object.assign(config, mixin);
}

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

View File

@ -1,5 +1,5 @@
import { redisClient } from "@/db/redis.js";
import { encode, decode } from "@msgpack/msgpack";
import { encode, decode } from "msgpackr";
import { ChainableCommander } from "ioredis";
export class Cache<T> {

View File

@ -15,7 +15,7 @@ import { createSystemUser } from "./create-system-user.js";
const ACTOR_USERNAME = "relay.actor" as const;
const relaysCache = new Cache<Relay[]>("relay", 60 * 10);
const relaysCache = new Cache<Relay[]>("relay", 60 * 60);
export async function getRelayActor(): Promise<ILocalUser> {
const user = await Users.findOneBy({

View File

@ -105,9 +105,6 @@ importers:
'@koa/router':
specifier: 9.0.1
version: 9.0.1
'@msgpack/msgpack':
specifier: 3.0.0-beta2
version: 3.0.0-beta2
'@peertube/http-signature':
specifier: 1.7.0
version: 1.7.0
@ -276,6 +273,9 @@ importers:
mime-types:
specifier: 2.1.35
version: 2.1.35
msgpackr:
specifier: 1.9.5
version: 1.9.5
multer:
specifier: 1.4.4-lts.1
version: 1.4.4-lts.1
@ -2597,11 +2597,6 @@ packages:
os-filter-obj: 2.0.0
dev: true
/@msgpack/msgpack@3.0.0-beta2:
resolution: {integrity: sha512-y+l1PNV0XDyY8sM3YtuMLK5vE3/hkfId+Do8pLo/OPxfxuFAUwcGz3oiiUuV46/aBpwTzZ+mRWVMtlSKbradhw==}
engines: {node: '>= 14'}
dev: false
/@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.2:
resolution: {integrity: sha512-9bfjwDxIDWmmOKusUcqdS4Rw+SETlp9Dy39Xui9BEGEk19dDwH0jhipwFzEff/pFg95NKymc6TOTbRKcWeRqyQ==}
cpu: [arm64]