Skip to content

Use noCache when cache disabled #19708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion modules/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
var conn mc.Cache

func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
if !cacheConfig.Enabled {
return newNoCache()
}

Comment on lines +21 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the unit tests.

return mc.NewCacher(mc.Options{
Adapter: cacheConfig.Adapter,
AdapterConfig: cacheConfig.Conn,
Expand All @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion modules/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
60 changes: 60 additions & 0 deletions modules/cache/empty.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
File renamed without changes.