From 612f371aa14c7e6d4786cd02cb9164452a4904e9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 15 May 2022 11:21:40 +0800 Subject: [PATCH 1/4] Use noCache when cache disabled --- modules/cache/cache.go | 6 ++- modules/cache/empty.go | 54 +++++++++++++++++++ modules/cache/{cache_redis.go => redis.go} | 0 .../cache/{cache_twoqueue.go => twoqueue.go} | 0 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 modules/cache/empty.go rename modules/cache/{cache_redis.go => redis.go} (100%) rename modules/cache/{cache_twoqueue.go => twoqueue.go} (100%) diff --git a/modules/cache/cache.go b/modules/cache/cache.go index fd32aa153b01a..32bb33c7f1669 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -20,6 +20,10 @@ import ( var conn mc.Cache func newCache(cacheConfig setting.Cache) (mc.Cache, error) { + if !cacheConfig.Enabled { + return newNoCache() + } + return mc.NewCacher(mc.Options{ Adapter: cacheConfig.Adapter, AdapterConfig: cacheConfig.Conn, @@ -31,7 +35,7 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) { func NewContext() error { var err error - if conn == nil && setting.CacheService.Enabled { + if conn == nil { if conn, err = newCache(setting.CacheService.Cache); err != nil { return err } diff --git a/modules/cache/empty.go b/modules/cache/empty.go new file mode 100644 index 0000000000000..88cdcc3693ae5 --- /dev/null +++ b/modules/cache/empty.go @@ -0,0 +1,54 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cache + +import mc "gitea.com/go-chi/cache" + +// noCache is the interface that operates the cache data. +type noCache struct{} + +// Put puts value into cache with key and expire time. +func (c noCache) Put(key string, val interface{}, timeout int64) error { + return nil +} + +// Get gets cached value by given key. +func (c noCache) Get(key string) interface{} { + return "" +} + +// Delete deletes cached value by given key. +func (c noCache) Delete(key string) error { + return nil +} + +// Incr increases cached int-type value by given key as a counter. +func (c noCache) Incr(key string) error { + return nil +} + +// Decr decreases cached int-type value by given key as a counter. +func (c noCache) Decr(key string) error { + return nil +} + +// IsExist returns true if cached value exists. +func (c noCache) IsExist(key string) bool { + return false +} + +// Flush deletes all cached data. +func (c noCache) Flush() error { + return nil +} + +// StartAndGC starts GC routine based on config string settings. +func (c noCache) StartAndGC(opt mc.Options) error { + return nil +} + +func newNoCache() (mc.Cache, error) { + return &noCache{}, nil +} diff --git a/modules/cache/cache_redis.go b/modules/cache/redis.go similarity index 100% rename from modules/cache/cache_redis.go rename to modules/cache/redis.go diff --git a/modules/cache/cache_twoqueue.go b/modules/cache/twoqueue.go similarity index 100% rename from modules/cache/cache_twoqueue.go rename to modules/cache/twoqueue.go From 8e193a2c0eab36641c058a608c6c155c8bce246b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 15 May 2022 11:02:44 +0200 Subject: [PATCH 2/4] smal reformat & add ping workaround --- modules/cache/cache.go | 14 +++++++------- modules/cache/empty.go | 13 +++++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 32bb33c7f1669..6c50cbe0106be 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -47,24 +47,24 @@ func NewContext() error { return err } +const pingTestKey = "__gitea_cache_test" +const pingTestVal = "test-value" + // Ping checks if the cache service works or not, it not, it returns an error func Ping() error { if conn == nil { return errors.New("cache not available") } - var err error - const testKey = "__gitea_cache_test" - const testVal = "test-value" - if err = conn.Put(testKey, testVal, 10); err != nil { + if err := conn.Put(pingTestKey, pingTestVal, 10); err != nil { return err } - val := conn.Get(testKey) - if valStr, ok := val.(string); !ok || valStr != testVal { + val := conn.Get(pingTestKey) + if valStr, ok := val.(string); !ok || valStr != pingTestVal { // If the cache is full, the Get may not read the expected value stored by Put. // Since we have checked that Put can success, so we just show a warning here, do not return an error to panic. log.Warn("cache (adapter:%s, config:%s) doesn't seem to work correctly, set test value '%v' but get '%v'", setting.CacheService.Cache.Adapter, setting.CacheService.Cache.Conn, - testVal, val, + pingTestVal, val, ) } return nil diff --git a/modules/cache/empty.go b/modules/cache/empty.go index 88cdcc3693ae5..ab7fff73f5085 100644 --- a/modules/cache/empty.go +++ b/modules/cache/empty.go @@ -9,6 +9,11 @@ import mc "gitea.com/go-chi/cache" // noCache is the interface that operates the cache data. type noCache struct{} +// newNoCache create a noop cache for chi +func newNoCache() (mc.Cache, error) { + return &noCache{}, nil +} + // Put puts value into cache with key and expire time. func (c noCache) Put(key string, val interface{}, timeout int64) error { return nil @@ -16,6 +21,10 @@ func (c noCache) Put(key string, val interface{}, timeout int64) error { // Get gets cached value by given key. func (c noCache) Get(key string) interface{} { + // workaround until https://gitea.com/go-chi/cache/issues/3 is resolved + if key == pingTestKey { + return pingTestVal + } return "" } @@ -48,7 +57,3 @@ func (c noCache) Flush() error { func (c noCache) StartAndGC(opt mc.Options) error { return nil } - -func newNoCache() (mc.Cache, error) { - return &noCache{}, nil -} From c95f63f3788e8282306f4faae229704789dfc7d0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 15 May 2022 11:09:47 +0200 Subject: [PATCH 3/4] fix unittest --- modules/cache/cache.go | 6 ++++-- modules/cache/cache_test.go | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 6c50cbe0106be..10e77d6c9fd30 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -47,8 +47,10 @@ func NewContext() error { return err } -const pingTestKey = "__gitea_cache_test" -const pingTestVal = "test-value" +const ( + pingTestKey = "__gitea_cache_test" + pingTestVal = "test-value" +) // Ping checks if the cache service works or not, it not, it returns an error func Ping() error { diff --git a/modules/cache/cache_test.go b/modules/cache/cache_test.go index f418f77e46ea8..3072d263ebd00 100644 --- a/modules/cache/cache_test.go +++ b/modules/cache/cache_test.go @@ -24,14 +24,20 @@ func createTestCache() { func TestNewContext(t *testing.T) { assert.NoError(t, NewContext()) - setting.CacheService.Cache = setting.Cache{Enabled: true, Adapter: "redis", Conn: "some random string"} con, err := newCache(setting.Cache{ + Enabled: true, Adapter: "rand", Conn: "false conf", Interval: 100, }) assert.Error(t, err) assert.Nil(t, con) + + con, err = newCache(setting.Cache{ + Enabled: false, + }) + assert.NoError(t, err) + assert.NotNil(t, con) } func TestGetCache(t *testing.T) { From 3a04371305f76e7296a174b544e8bafe63499df4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 15 May 2022 20:47:45 +0200 Subject: [PATCH 4/4] Update modules/cache/empty.go --- modules/cache/empty.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/cache/empty.go b/modules/cache/empty.go index c1dd0a56b8c85..bea0dd778ba8b 100644 --- a/modules/cache/empty.go +++ b/modules/cache/empty.go @@ -21,10 +21,6 @@ func (c noCache) Put(key string, val interface{}, timeout int64) error { // Get gets cached value by given key. func (c noCache) Get(key string) interface{} { - // workaround until https://gitea.com/go-chi/cache/issues/3 is resolved - if key == pingTestKey { - return pingTestVal - } return "" }