summaryrefslogtreecommitdiffstats
path: root/modules/cache
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-03-27 12:34:39 +0000
committerGitHub <noreply@github.com>2020-03-27 14:34:39 +0200
commite6baa656f757fd1f2f6ba20c677e0c83422a8739 (patch)
treeb2a396f41e1b8a08b796084d169f202d593f7357 /modules/cache
parenta3f90948d8fa4dd5c92e15cc10e86d2fec37f6e7 (diff)
downloadgitea-e6baa656f757fd1f2f6ba20c677e0c83422a8739.tar.gz
gitea-e6baa656f757fd1f2f6ba20c677e0c83422a8739.zip
make avatar lookup occur at image request (#10540)
speed up page generation by making avatar lookup occur at the browser not at page generation * Protect against evil email address ".." * hash the complete email address Signed-off-by: Andrew Thornton <art27@cantab.net> Co-Authored-By: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules/cache')
-rw-r--r--modules/cache/cache.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/modules/cache/cache.go b/modules/cache/cache.go
index e3a905e3fa..859f4a4b47 100644
--- a/modules/cache/cache.go
+++ b/modules/cache/cache.go
@@ -41,6 +41,34 @@ func NewContext() error {
return err
}
+// GetString returns the key value from cache with callback when no key exists in cache
+func GetString(key string, getFunc func() (string, error)) (string, error) {
+ if conn == nil || setting.CacheService.TTL == 0 {
+ return getFunc()
+ }
+ if !conn.IsExist(key) {
+ var (
+ value string
+ err error
+ )
+ if value, err = getFunc(); err != nil {
+ return value, err
+ }
+ err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
+ if err != nil {
+ return "", err
+ }
+ }
+ value := conn.Get(key)
+ if v, ok := value.(string); ok {
+ return v, nil
+ }
+ if v, ok := value.(fmt.Stringer); ok {
+ return v.String(), nil
+ }
+ return fmt.Sprintf("%s", conn.Get(key)), nil
+}
+
// GetInt returns key value from cache with callback when no key exists in cache
func GetInt(key string, getFunc func() (int, error)) (int, error) {
if conn == nil || setting.CacheService.TTL == 0 {