2017-10-26 01:37:33 +00: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 07:06:34 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2017-10-26 01:37:33 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
mc "gitea.com/go-chi/cache"
|
2020-01-29 07:47:46 +00:00
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
_ "gitea.com/go-chi/cache/memcache" // memcache plugin for cache
|
2017-10-26 01:37:33 +00:00
|
|
|
)
|
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
var conn mc.Cache
|
2020-02-01 19:11:32 +00:00
|
|
|
|
|
|
|
func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
|
2021-01-26 15:36:53 +00:00
|
|
|
return mc.NewCacher(mc.Options{
|
2020-02-01 19:11:32 +00:00
|
|
|
Adapter: cacheConfig.Adapter,
|
|
|
|
AdapterConfig: cacheConfig.Conn,
|
|
|
|
Interval: cacheConfig.Interval,
|
|
|
|
})
|
|
|
|
}
|
2017-10-26 01:37:33 +00:00
|
|
|
|
|
|
|
// NewContext start cache service
|
|
|
|
func NewContext() error {
|
2020-02-01 19:11:32 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if conn == nil && setting.CacheService.Enabled {
|
|
|
|
if conn, err = newCache(setting.CacheService.Cache); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-15 18:43:27 +00:00
|
|
|
if err = conn.Ping(); err != nil {
|
2021-12-05 16:24:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-26 01:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:00:47 +00:00
|
|
|
// GetCache returns the currently configured cache
|
2021-01-27 14:56:54 +00:00
|
|
|
func GetCache() mc.Cache {
|
2020-12-17 14:00:47 +00:00
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2022-11-10 11:41:44 +00:00
|
|
|
// Get returns the key value from cache with callback when no key exists in cache
|
|
|
|
func Get[V interface{}](key string, getFunc func() (V, error)) (V, error) {
|
|
|
|
if conn == nil || setting.CacheService.TTL == 0 {
|
|
|
|
return getFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
if value, ok := cached.(V); ok {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := getFunc()
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set updates and returns the key value in the cache with callback. The old value is only removed if the updateFunc() is successful
|
|
|
|
func Set[V interface{}](key string, valueFunc func() (V, error)) (V, error) {
|
|
|
|
if conn == nil || setting.CacheService.TTL == 0 {
|
|
|
|
return valueFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := valueFunc()
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
|
|
|
}
|
|
|
|
|
2020-03-27 12:34:39 +00: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()
|
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
value, err := getFunc()
|
2020-03-27 12:34:39 +00:00
|
|
|
if err != nil {
|
2022-11-10 11:41:44 +00:00
|
|
|
return value, err
|
2020-03-27 12:34:39 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2020-03-27 12:34:39 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
if value, ok := cached.(string); ok {
|
|
|
|
return value, nil
|
2020-03-27 12:34:39 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
if stringer, ok := cached.(fmt.Stringer); ok {
|
|
|
|
return stringer.String(), nil
|
2020-03-27 12:34:39 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf("%s", cached), nil
|
2020-03-27 12:34:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 01:37:33 +00: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()
|
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
value, err := getFunc()
|
2019-06-12 19:41:28 +00:00
|
|
|
if err != nil {
|
2022-11-10 11:41:44 +00:00
|
|
|
return value, err
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-10-26 01:37:33 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
switch v := cached.(type) {
|
2017-11-16 07:06:34 +00:00
|
|
|
case int:
|
2022-11-10 11:41:44 +00:00
|
|
|
return v, nil
|
2017-11-16 07:06:34 +00:00
|
|
|
case string:
|
2022-11-10 11:41:44 +00:00
|
|
|
value, err := strconv.Atoi(v)
|
2017-11-16 07:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
return value, nil
|
2017-11-16 07:06:34 +00:00
|
|
|
default:
|
2022-11-10 11:41:44 +00:00
|
|
|
value, err := getFunc()
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-11-16 07:06:34 +00:00
|
|
|
}
|
2017-10-26 01:37:33 +00: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()
|
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
value, err := getFunc()
|
2019-06-12 19:41:28 +00:00
|
|
|
if err != nil {
|
2022-11-10 11:41:44 +00:00
|
|
|
return value, err
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-10-26 01:37:33 +00:00
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
|
|
|
|
switch v := conn.Get(key).(type) {
|
2017-11-16 07:06:34 +00:00
|
|
|
case int64:
|
2022-11-10 11:41:44 +00:00
|
|
|
return v, nil
|
2017-11-16 07:06:34 +00:00
|
|
|
case string:
|
2022-11-10 11:41:44 +00:00
|
|
|
value, err := strconv.ParseInt(v, 10, 64)
|
2017-11-16 07:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-10 11:41:44 +00:00
|
|
|
return value, nil
|
2017-11-16 07:06:34 +00:00
|
|
|
default:
|
2022-11-10 11:41:44 +00:00
|
|
|
value, err := getFunc()
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-11-16 07:06:34 +00:00
|
|
|
}
|
2017-10-26 01:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove key from cache
|
|
|
|
func Remove(key string) {
|
|
|
|
if conn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-12 19:41:28 +00:00
|
|
|
_ = conn.Delete(key)
|
2017-10-26 01:37:33 +00:00
|
|
|
}
|