2017-10-26 07:07:33 +05:30
|
|
|
// Copyright 2017 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 (
|
2017-11-16 12:36:34 +05:30
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2017-10-26 07:07:33 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
mc "gitea.com/go-chi/cache"
|
2020-01-29 13:17:46 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
_ "gitea.com/go-chi/cache/memcache" // memcache plugin for cache
|
2017-10-26 07:07:33 +05:30
|
|
|
)
|
|
|
|
|
2020-02-02 00:41:32 +05:30
|
|
|
var (
|
|
|
|
conn mc.Cache
|
|
|
|
)
|
|
|
|
|
|
|
|
func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
|
2021-01-26 21:06:53 +05:30
|
|
|
return mc.NewCacher(mc.Options{
|
2020-02-02 00:41:32 +05:30
|
|
|
Adapter: cacheConfig.Adapter,
|
|
|
|
AdapterConfig: cacheConfig.Conn,
|
|
|
|
Interval: cacheConfig.Interval,
|
|
|
|
})
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
|
|
|
|
// NewContext start cache service
|
|
|
|
func NewContext() error {
|
2020-02-02 00:41:32 +05:30
|
|
|
var err error
|
|
|
|
|
|
|
|
if conn == nil && setting.CacheService.Enabled {
|
|
|
|
if conn, err = newCache(setting.CacheService.Cache); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:30:47 +05:30
|
|
|
// GetCache returns the currently configured cache
|
2021-01-27 20:26:54 +05:30
|
|
|
func GetCache() mc.Cache {
|
2020-12-17 19:30:47 +05:30
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:04:39 +05:30
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-10-26 07:07:33 +05:30
|
|
|
// 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 {
|
|
|
|
return getFunc()
|
|
|
|
}
|
|
|
|
if !conn.IsExist(key) {
|
|
|
|
var (
|
|
|
|
value int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if value, err = getFunc(); err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
2017-11-16 12:36:34 +05:30
|
|
|
switch value := conn.Get(key).(type) {
|
|
|
|
case int:
|
|
|
|
return value, nil
|
|
|
|
case string:
|
|
|
|
v, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("Unsupported cached value type: %v", value)
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetInt64 returns key value from cache with callback when no key exists in cache
|
|
|
|
func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
|
|
|
|
if conn == nil || setting.CacheService.TTL == 0 {
|
|
|
|
return getFunc()
|
|
|
|
}
|
|
|
|
if !conn.IsExist(key) {
|
|
|
|
var (
|
|
|
|
value int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if value, err = getFunc(); err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
2017-11-16 12:36:34 +05:30
|
|
|
switch value := conn.Get(key).(type) {
|
|
|
|
case int64:
|
|
|
|
return value, nil
|
|
|
|
case string:
|
|
|
|
v, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("Unsupported cached value type: %v", value)
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Remove key from cache
|
|
|
|
func Remove(key string) {
|
|
|
|
if conn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = conn.Delete(key)
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|