Skip to content

Commit 00a981d

Browse files
authored
Update go-chi/cache to utilize Ping() (#19719)
* update gitea.com/go-chi/cache -> v0.2.0 * ajust to new interface * refactor
1 parent 3a24523 commit 00a981d

File tree

6 files changed

+15
-30
lines changed

6 files changed

+15
-30
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b
77
code.gitea.io/sdk/gitea v0.15.1
88
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb
9-
gitea.com/go-chi/cache v0.0.0-20211201020628-dcb774c4ffea
9+
gitea.com/go-chi/cache v0.2.0
1010
gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5
1111
gitea.com/go-chi/session v0.0.0-20211218221615-e3605d8b28b8
1212
gitea.com/lunny/levelqueue v0.4.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
7272
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb h1:Yy0Bxzc8R2wxiwXoG/rECGplJUSpXqCsog9PuJFgiHs=
7373
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb/go.mod h1:77TZu701zMXWJFvB8gvTbQ92zQ3DQq/H7l5wAEjQRKc=
7474
gitea.com/go-chi/cache v0.0.0-20210110083709-82c4c9ce2d5e/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0=
75-
gitea.com/go-chi/cache v0.0.0-20211201020628-dcb774c4ffea h1:Fq/f4hjigb5v5WpA5TfBCZOSavpHPBiB4Wkj/WsITYM=
76-
gitea.com/go-chi/cache v0.0.0-20211201020628-dcb774c4ffea/go.mod h1:k2V/gPDEtXGjjMGuBJiapffAXTv76H4snSmlJRLUhH0=
75+
gitea.com/go-chi/cache v0.2.0 h1:E0npuTfDW6CT1yD8NMDVc1SK6IeRjfmRL2zlEsCEd7w=
76+
gitea.com/go-chi/cache v0.2.0/go.mod h1:iQlVK2aKTZ/rE9UcHyz9pQWGvdP9i1eI2spOpzgCrtE=
7777
gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5 h1:J/1i8u40TbcLP/w2w2KCkgW2PelIqYkD5UOwu8IOvVU=
7878
gitea.com/go-chi/captcha v0.0.0-20211013065431-70641c1a35d5/go.mod h1:hQ9SYHKdOX968wJglb/NMQ+UqpOKwW4L+EYdvkWjHSo=
7979
gitea.com/go-chi/session v0.0.0-20211218221615-e3605d8b28b8 h1:tJQRXgZigkLeeW9LPlps9G9aMoE6LAmqigLA+wxmd1Q=

modules/cache/cache.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
package cache
66

77
import (
8-
"errors"
98
"fmt"
109
"strconv"
1110

12-
"code.gitea.io/gitea/modules/log"
1311
"code.gitea.io/gitea/modules/setting"
1412

1513
mc "gitea.com/go-chi/cache"
@@ -35,37 +33,14 @@ func NewContext() error {
3533
if conn, err = newCache(setting.CacheService.Cache); err != nil {
3634
return err
3735
}
38-
if err = Ping(); err != nil {
36+
if err = conn.Ping(); err != nil {
3937
return err
4038
}
4139
}
4240

4341
return err
4442
}
4543

46-
// Ping checks if the cache service works or not, it not, it returns an error
47-
func Ping() error {
48-
if conn == nil {
49-
return errors.New("cache not available")
50-
}
51-
var err error
52-
const testKey = "__gitea_cache_test"
53-
const testVal = "test-value"
54-
if err = conn.Put(testKey, testVal, 10); err != nil {
55-
return err
56-
}
57-
val := conn.Get(testKey)
58-
if valStr, ok := val.(string); !ok || valStr != testVal {
59-
// If the cache is full, the Get may not read the expected value stored by Put.
60-
// Since we have checked that Put can success, so we just show a warning here, do not return an error to panic.
61-
log.Warn("cache (adapter:%s, config:%s) doesn't seem to work correctly, set test value '%v' but get '%v'",
62-
setting.CacheService.Cache.Adapter, setting.CacheService.Cache.Conn,
63-
testVal, val,
64-
)
65-
}
66-
return nil
67-
}
68-
6944
// GetCache returns the currently configured cache
7045
func GetCache() mc.Cache {
7146
return conn

modules/cache/cache_redis.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ func (c *RedisCacher) StartAndGC(opts cache.Options) error {
153153
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
154154
}
155155

156+
// Ping tests if the cache is alive.
157+
func (c *RedisCacher) Ping() error {
158+
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
159+
}
160+
156161
func init() {
157162
cache.Register("redis", &RedisCacher{})
158163
}

modules/cache/cache_twoqueue.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ func (c *TwoQueueCache) StartAndGC(opts mc.Options) error {
199199
return err
200200
}
201201

202+
// Ping tests if the cache is alive.
203+
func (c *TwoQueueCache) Ping() error {
204+
return mc.GenericPing(c)
205+
}
206+
202207
func init() {
203208
mc.Register("twoqueue", &TwoQueueCache{})
204209
}

routers/web/healthcheck/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func checkCache(checks checks) status {
126126
}
127127

128128
st := componentStatus{}
129-
if err := cache.Ping(); err != nil {
129+
if err := cache.GetCache().Ping(); err != nil {
130130
st.Status = fail
131131
st.Time = getCheckTime()
132132
log.Error("cache ping failed with error: %v", err)

0 commit comments

Comments
 (0)