Skip to content
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

Add cache test for admins #31265

Merged
merged 14 commits into from
Jun 17, 2024
4 changes: 4 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ remove_all = Remove All
remove_label_str = Remove item "%s"
edit = Edit
view = View
test = Test

enabled = Enabled
disabled = Disabled
Expand Down Expand Up @@ -3224,6 +3225,9 @@ config.cache_adapter = Cache Adapter
config.cache_interval = Cache Interval
config.cache_conn = Cache Connection
config.cache_item_ttl = Cache Item TTL
config.cache_test = Test Cache
config.cache_test_failed = Failed to probe the cache: %v.
config.cache_test_succeeded = Probe cache succeeded, response took %s.

config.session_config = Session Configuration
config.session_provider = Session Provider
Expand Down
42 changes: 42 additions & 0 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
package admin

import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"

system_model "code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -42,6 +45,45 @@ func SendTestMail(ctx *context.Context) {
ctx.Redirect(setting.AppSubURL + "/admin/config")
}

const testCacheKey = "Admin.TestCacheKey"

// TestCache test the cache settings
func TestCache(ctx *context.Context) {
// prepare cache
c := cache.GetCache()
var elapsed string

err := func() error {
start := time.Now()

if err := c.Delete(testCacheKey); err != nil {
return err
}
if err := c.Put(testCacheKey, testCacheKey, 10); err != nil {
return err
}
testVal, hit := c.Get(testCacheKey)
if !hit {
return fmt.Errorf("Expect cache hit but got none")
}
if testVal != testCacheKey {
return fmt.Errorf("Expect cache to return same value as stored but got other")
}

// we want the meridian of a single response so we divide with 3
elapsed = fmt.Sprint(time.Since(start) / 3)
return nil
}()

if err != nil {
ctx.Flash.Error(ctx.Tr("admin.config.cache_test_failed", err))
} else {
ctx.Flash.Info(ctx.Tr("admin.config.cache_test_succeeded", elapsed))
}

ctx.Redirect(setting.AppSubURL + "/admin/config")
}

func shadowPasswordKV(cfgItem, splitter string) string {
fields := strings.Split(cfgItem, splitter)
for i := 0; i < len(fields); i++ {
Expand Down
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ func registerRoutes(m *web.Route) {
m.Get("", admin.Config)
m.Post("", admin.ChangeConfig)
m.Post("/test_mail", admin.SendTestMail)
m.Post("/test_cache", admin.TestCache)
m.Get("/settings", admin.ConfigSettings)
})

Expand Down
8 changes: 8 additions & 0 deletions templates/admin/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@
<dt>{{ctx.Locale.Tr "admin.config.cache_item_ttl"}}</dt>
<dd><code>{{.CacheItemTTL}}</code></dd>
{{end}}
<div class="divider"></div>
<dt class="tw-py-1">{{ctx.Locale.Tr "admin.config.cache_test"}}</dt>
<dd>
<form class="ui form ignore-dirty" action="{{AppSubUrl}}/admin/config/test_cache" method="post">
{{.CsrfTokenHtml}}
<button class="ui tiny primary button">{{ctx.Locale.Tr "test"}}</button>
</form>
</dd>
</dl>
</div>

Expand Down