From 9510962e2521ba77d4391190116c546c83f08db3 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Fri, 26 Jun 2020 00:58:38 +0200 Subject: [PATCH] add redis PubSub web cache as an option --- README.md | 2 ++ backend/app/cmd/server.go | 17 +++++++++++++++-- backend/app/cmd/server_test.go | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b73647c0..514d7775 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,8 @@ _this is the recommended way to run remark42_ | admin.shared.email | ADMIN_SHARED_EMAIL | `admin@${REMARK_URL}` | admin email | | backup | BACKUP_PATH | `./var/backup` | backups location | | max-back | MAX_BACKUP_FILES | `10` | max backup files to keep | +| cache.type | CACHE_TYPE | `mem` | type of cache, `redis_pub_sub` or `mem` or `none` | +| cache.redis_addr | CACHE_REDIS_ADDR | `127.0.0.1:6379` | address of redis PubSub instance, turn `redis_pub_sub` cache on for distributed cache | | cache.max.items | CACHE_MAX_ITEMS | `1000` | max number of cached items, `0` - unlimited | | cache.max.value | CACHE_MAX_VALUE | `65536` | max size of cached value, `0` - unlimited | | cache.max.size | CACHE_MAX_SIZE | `50000000` | max size of all cached values, `0` - unlimited | diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index f5230e87..be109042 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -16,6 +16,7 @@ import ( "github.com/dgrijalva/jwt-go" "github.com/go-pkgz/jrpc" + "github.com/go-pkgz/lcw/eventbus" log "github.com/go-pkgz/lgr" "github.com/kyokomi/emoji" "github.com/pkg/errors" @@ -160,8 +161,9 @@ type AvatarGroup struct { // CacheGroup defines options group for cache params type CacheGroup struct { - Type string `long:"type" env:"TYPE" description:"type of cache" choice:"mem" choice:"none" default:"mem"` // nolint - Max struct { + Type string `long:"type" env:"TYPE" description:"type of cache" choice:"redis_pub_sub" choice:"mem" choice:"none" default:"mem"` // nolint + RedisAddr string `long:"redis_addr" env:"REDIS_ADDR" default:"127.0.0.1:6379" description:"address of redis cache, turn redis cache on for distributed cache"` + Max struct { Items int `long:"items" env:"ITEMS" default:"1000" description:"max cached items"` Value int `long:"value" env:"VALUE" default:"65536" description:"max size of cached value"` Size int64 `long:"size" env:"SIZE" default:"50000000" description:"max size of total cache"` @@ -670,6 +672,17 @@ func (s *ServerCommand) makeAdminStore() (admin.Store, error) { func (s *ServerCommand) makeCache() (LoadingCache, error) { log.Printf("[INFO] make cache, type=%s", s.Cache.Type) switch s.Cache.Type { + case "redis_pub_sub": + redisPubSub, err := eventbus.NewRedisPubSub(s.Cache.RedisAddr, "remark42-cache") + if err != nil { + return nil, errors.Wrap(err, "cache backend initialization, redis PubSub initialisation") + } + backend, err := cache.NewLruCache(cache.MaxCacheSize(s.Cache.Max.Size), cache.MaxValSize(s.Cache.Max.Value), + cache.MaxKeys(s.Cache.Max.Items), cache.EventBus(redisPubSub)) + if err != nil { + return nil, errors.Wrap(err, "cache backend initialization") + } + return cache.NewScache(backend), nil case "mem": backend, err := cache.NewLruCache(cache.MaxCacheSize(s.Cache.Max.Size), cache.MaxValSize(s.Cache.Max.Value), cache.MaxKeys(s.Cache.Max.Items)) diff --git a/backend/app/cmd/server_test.go b/backend/app/cmd/server_test.go index c2efd275..644ce9f4 100644 --- a/backend/app/cmd/server_test.go +++ b/backend/app/cmd/server_test.go @@ -329,6 +329,19 @@ func TestServerApp_Failed(t *testing.T) { _, err = opts.newServerApp() assert.EqualError(t, err, "failed to make data store engine: unsupported store type blah") t.Log(err) + + // wrong redis location + opts = ServerCommand{} + opts.SetCommon(CommonOpts{RemarkURL: "https://demo.remark42.com", SharedSecret: "123456"}) + p = flags.NewParser(&opts, flags.Default) + _, err = p.ParseArgs([]string{"--store.bolt.path=/tmp", "--cache.type=redis_pub_sub", "--cache.redis_addr=wrong_address"}) + assert.NoError(t, err) + _, err = opts.newServerApp() + assert.EqualError(t, err, + "failed to make cache: cache backend initialization, redis PubSub initialisation: "+ + "problem subscribing to channel remark42-cache on address wrong_address: "+ + "dial tcp: address wrong_address: missing port in address") + t.Log(err) } func TestServerApp_Shutdown(t *testing.T) {