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 #db: 1
#user: default #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. # Please configure either MeiliSearch *or* Sonic.
# If both MeiliSearch and Sonic configurations are present, MeiliSearch will take precedence. # 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) - 🦔 [Sonic](https://crates.io/crates/sonic-server)
- [MeiliSearch](https://www.meilisearch.com/) - [MeiliSearch](https://www.meilisearch.com/)
- [ElasticSearch](https://www.elastic.co/elasticsearch/) - [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 ### 🏗️ Build dependencies
- 🦀 At least [Rust](https://www.rust-lang.org/) v1.68.0 - 🦀 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`. 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 ## 🔎 Set up search
### 🦔 Sonic ### 🦔 Sonic

View file

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

View file

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

View file

@ -26,6 +26,16 @@ export type Source = {
user?: string; user?: string;
tls?: { [y: string]: 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: { elasticsearch: {
host: string; host: string;
port: number; port: number;

View file

@ -2,15 +2,19 @@ import Redis from "ioredis";
import config from "@/config/index.js"; import config from "@/config/index.js";
export function createConnection() { export function createConnection() {
let source = config.redis;
if (config.cacheServer) {
source = config.cacheServer;
}
return new Redis({ return new Redis({
port: config.redis.port, port: source.port,
host: config.redis.host, host: source.host,
family: config.redis.family ?? 0, family: source.family ?? 0,
password: config.redis.pass, password: source.pass,
username: config.redis.user ?? "default", username: source.user ?? "default",
keyPrefix: `${config.redis.prefix}:`, keyPrefix: `${source.prefix}:`,
db: config.redis.db || 0, db: source.db || 0,
tls: config.redis.tls, tls: source.tls,
}); });
} }

View file

@ -1,5 +1,5 @@
import { redisClient } from "@/db/redis.js"; import { redisClient } from "@/db/redis.js";
import { encode, decode } from "@msgpack/msgpack"; import { encode, decode } from "msgpackr";
import { ChainableCommander } from "ioredis"; import { ChainableCommander } from "ioredis";
export class Cache<T> { 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 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> { export async function getRelayActor(): Promise<ILocalUser> {
const user = await Users.findOneBy({ const user = await Users.findOneBy({

View file

@ -105,9 +105,6 @@ importers:
'@koa/router': '@koa/router':
specifier: 9.0.1 specifier: 9.0.1
version: 9.0.1 version: 9.0.1
'@msgpack/msgpack':
specifier: 3.0.0-beta2
version: 3.0.0-beta2
'@peertube/http-signature': '@peertube/http-signature':
specifier: 1.7.0 specifier: 1.7.0
version: 1.7.0 version: 1.7.0
@ -276,6 +273,9 @@ importers:
mime-types: mime-types:
specifier: 2.1.35 specifier: 2.1.35
version: 2.1.35 version: 2.1.35
msgpackr:
specifier: 1.9.5
version: 1.9.5
multer: multer:
specifier: 1.4.4-lts.1 specifier: 1.4.4-lts.1
version: 1.4.4-lts.1 version: 1.4.4-lts.1
@ -2597,11 +2597,6 @@ packages:
os-filter-obj: 2.0.0 os-filter-obj: 2.0.0
dev: true 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: /@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.2:
resolution: {integrity: sha512-9bfjwDxIDWmmOKusUcqdS4Rw+SETlp9Dy39Xui9BEGEk19dDwH0jhipwFzEff/pFg95NKymc6TOTbRKcWeRqyQ==} resolution: {integrity: sha512-9bfjwDxIDWmmOKusUcqdS4Rw+SETlp9Dy39Xui9BEGEk19dDwH0jhipwFzEff/pFg95NKymc6TOTbRKcWeRqyQ==}
cpu: [arm64] cpu: [arm64]