forked from forgejo/forgejo
Update go-chi/cache to utilize Ping() (#19719)
* update gitea.com/go-chi/cache -> v0.2.0 * ajust to new interface * refactor
This commit is contained in:
parent
3a245230f4
commit
00a981d341
6 changed files with 15 additions and 30 deletions
27
modules/cache/cache.go
vendored
27
modules/cache/cache.go
vendored
|
@ -5,11 +5,9 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
mc "gitea.com/go-chi/cache"
|
||||
|
@ -35,7 +33,7 @@ func NewContext() error {
|
|||
if conn, err = newCache(setting.CacheService.Cache); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = Ping(); err != nil {
|
||||
if err = conn.Ping(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -43,29 +41,6 @@ func NewContext() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
val := conn.Get(testKey)
|
||||
if valStr, ok := val.(string); !ok || valStr != testVal {
|
||||
// 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,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCache returns the currently configured cache
|
||||
func GetCache() mc.Cache {
|
||||
return conn
|
||||
|
|
5
modules/cache/cache_redis.go
vendored
5
modules/cache/cache_redis.go
vendored
|
@ -153,6 +153,11 @@ func (c *RedisCacher) StartAndGC(opts cache.Options) error {
|
|||
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
|
||||
}
|
||||
|
||||
// Ping tests if the cache is alive.
|
||||
func (c *RedisCacher) Ping() error {
|
||||
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
|
||||
}
|
||||
|
||||
func init() {
|
||||
cache.Register("redis", &RedisCacher{})
|
||||
}
|
||||
|
|
5
modules/cache/cache_twoqueue.go
vendored
5
modules/cache/cache_twoqueue.go
vendored
|
@ -199,6 +199,11 @@ func (c *TwoQueueCache) StartAndGC(opts mc.Options) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Ping tests if the cache is alive.
|
||||
func (c *TwoQueueCache) Ping() error {
|
||||
return mc.GenericPing(c)
|
||||
}
|
||||
|
||||
func init() {
|
||||
mc.Register("twoqueue", &TwoQueueCache{})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue