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"}, 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{ &cli.StringFlag{
Name: "log-level", Name: "log-level",
Value: "warn", Value: "warn",

View file

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

View file

@ -54,6 +54,9 @@ func MergeConfig(ctx *cli.Context, config *Config) {
mergeGiteaConfig(ctx, &config.Gitea) mergeGiteaConfig(ctx, &config.Gitea)
mergeDatabaseConfig(ctx, &config.Database) mergeDatabaseConfig(ctx, &config.Database)
mergeACMEConfig(ctx, &config.ACME) mergeACMEConfig(ctx, &config.ACME)
if ctx.IsSet("redis-url") {
config.RedisURL = ctx.String("redis-url")
}
} }
func mergeServerConfig(ctx *cli.Context, config *ServerConfig) { 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{ return &RedisCache{
context.Background(), context.Background(),
redis.NewClient(&redis.Options{ redis.NewClient(opts),
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
}),
} }
} }

View file

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