diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 21d0cd0a04996..b4ddc62c41dd8 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -18,6 +18,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, @@ -29,7 +33,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/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) { diff --git a/modules/cache/empty.go b/modules/cache/empty.go new file mode 100644 index 0000000000000..bea0dd778ba8b --- /dev/null +++ b/modules/cache/empty.go @@ -0,0 +1,60 @@ +// 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{} + +// 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 +} + +// 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 +} + +// Ping tests if the cache is alive. +func (c noCache) Ping() error { + return 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