2017-10-25 18:37:33 -07:00
|
|
|
// 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 00:06:34 -07:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2017-10-25 18:37:33 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-01-26 08:36:53 -07:00
|
|
|
mc "gitea.com/go-chi/cache"
|
2020-01-29 00:47:46 -07:00
|
|
|
|
2021-01-26 08:36:53 -07:00
|
|
|
_ "gitea.com/go-chi/cache/memcache" // memcache plugin for cache
|
2017-10-25 18:37:33 -07:00
|
|
|
)
|
|
|
|
|
2020-02-01 12:11:32 -07:00
|
|
|
var (
|
|
|
|
conn mc.Cache
|
|
|
|
)
|
|
|
|
|
|
|
|
func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
|
2021-01-26 08:36:53 -07:00
|
|
|
return mc.NewCacher(mc.Options{
|
2020-02-01 12:11:32 -07:00
|
|
|
Adapter: cacheConfig.Adapter,
|
|
|
|
AdapterConfig: cacheConfig.Conn,
|
|
|
|
Interval: cacheConfig.Interval,
|
|
|
|
})
|
|
|
|
}
|
2017-10-25 18:37:33 -07:00
|
|
|
|
|
|
|
// NewContext start cache service
|
|
|
|
func NewContext() error {
|
2020-02-01 12:11:32 -07:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if conn == nil && setting.CacheService.Enabled {
|
|
|
|
if conn, err = newCache(setting.CacheService.Cache); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-25 18:37:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-17 07:00:47 -07:00
|
|
|
// GetCache returns the currently configured cache
|
2021-01-27 07:56:54 -07:00
|
|
|
func GetCache() mc.Cache {
|
2020-12-17 07:00:47 -07:00
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:34:39 -07:00
|
|
|
// 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
|
|
|
|
}
|
2021-02-09 15:29:03 -07:00
|
|
|
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2020-03-27 05:34:39 -07:00
|
|
|
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-25 18:37:33 -07:00
|
|
|
// 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
|
|
|
|
}
|
2021-02-09 15:29:03 -07:00
|
|
|
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2019-06-12 12:41:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-10-25 18:37:33 -07:00
|
|
|
}
|
2017-11-16 00:06:34 -07:00
|
|
|
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-25 18:37:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-02-09 15:29:03 -07:00
|
|
|
err = conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2019-06-12 12:41:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-10-25 18:37:33 -07:00
|
|
|
}
|
2017-11-16 00:06:34 -07:00
|
|
|
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-25 18:37:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove key from cache
|
|
|
|
func Remove(key string) {
|
|
|
|
if conn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-12 12:41:28 -07:00
|
|
|
_ = conn.Delete(key)
|
2017-10-25 18:37:33 -07:00
|
|
|
}
|