Make it possible to actually use redis for caching through the config flags

This commit is contained in:
Moritz Marquardt 2024-03-26 07:47:39 +01:00
parent c4181d1206
commit e1a22d5f4c
5 changed files with 37 additions and 12 deletions

View file

@ -126,6 +126,13 @@ var (
EnvVars: []string{"BLACKLISTED_PATHS"},
},
&cli.StringFlag{
Name: "redis-url",
Value: "",
Usage: "use redis instead of the built-in memory cache (recommended)",
EnvVars: []string{"REDIS_URL"},
},
&cli.StringFlag{
Name: "log-level",
Value: "warn",

View file

@ -6,6 +6,7 @@ type Config struct {
Gitea GiteaConfig
Database DatabaseConfig
ACME ACMEConfig
RedisURL string `default:""`
}
type ServerConfig struct {

View file

@ -54,6 +54,9 @@ func MergeConfig(ctx *cli.Context, config *Config) {
mergeGiteaConfig(ctx, &config.Gitea)
mergeDatabaseConfig(ctx, &config.Database)
mergeACMEConfig(ctx, &config.ACME)
if ctx.IsSet("redis-url") {
config.RedisURL = ctx.String("redis-url")
}
}
func mergeServerConfig(ctx *cli.Context, config *ServerConfig) {

View file

@ -36,13 +36,9 @@ func (r *RedisCache) Remove(key string) {
}
}
func NewRedisCache() ICache {
func NewRedisCache(opts *redis.Options) ICache {
return &RedisCache{
context.Background(),
redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
}),
redis.NewClient(opts),
}
}

View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/OrlovEvgeny/go-mcache"
"github.com/redis/go-redis/v9"
"net"
"net/http"
"os"
@ -71,16 +72,33 @@ func Serve(ctx *cli.Context) error {
}
defer closeFn()
// keyCache stores the parsed certificate objects (Redis is no advantage here)
keyCache := mcache.New()
challengeCache := cache.NewInMemoryCache()
// canonicalDomainCache stores canonical domains
canonicalDomainCache := cache.NewInMemoryCache()
// dnsLookupCache stores DNS lookups for custom domains
// dnsLookupCache stores DNS lookups for custom domains (Redis is no advantage here)
dnsLookupCache := mcache.New()
var redisErr error = nil
createCache := func() cache.ICache {
if cfg.RedisURL != "" {
opts, err := redis.ParseURL(cfg.RedisURL)
if err != nil {
redisErr = err
}
return cache.NewRedisCache(opts)
}
return cache.NewInMemoryCache()
}
// challengeCache stores the certificate challenges
challengeCache := createCache()
// canonicalDomainCache stores canonical domains
canonicalDomainCache := createCache()
// redirectsCache stores redirects in _redirects files
redirectsCache := cache.NewInMemoryCache()
redirectsCache := createCache()
// clientResponseCache stores responses from the Gitea server
clientResponseCache := cache.NewInMemoryCache()
clientResponseCache := createCache()
if redisErr != nil {
return redisErr
}
giteaClient, err := gitea.NewClient(cfg.Gitea, clientResponseCache)
if err != nil {