2017-10-26 07:07:33 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-26 07:07:33 +05:30
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2017-11-16 12:36:34 +05:30
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-06-23 15:08:35 +05:30
|
|
|
"time"
|
2017-11-16 12:36:34 +05:30
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var conn mc.Cache
|
2020-02-02 00:41:32 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
2023-12-19 14:59:05 +05:30
|
|
|
// Init start cache service
|
|
|
|
func Init() error {
|
2020-02-02 00:41:32 +05:30
|
|
|
var err error
|
|
|
|
|
2023-12-19 14:59:05 +05:30
|
|
|
if conn == nil {
|
2020-02-02 00:41:32 +05:30
|
|
|
if conn, err = newCache(setting.CacheService.Cache); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-16 00:13:27 +05:30
|
|
|
if err = conn.Ping(); err != nil {
|
2021-12-05 21:54:57 +05:30
|
|
|
return err
|
|
|
|
}
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-18 00:52:39 +05:30
|
|
|
const (
|
|
|
|
testCacheKey = "DefaultCache.TestKey"
|
|
|
|
SlowCacheThreshold = 100 * time.Microsecond
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test() (time.Duration, error) {
|
2024-06-23 15:08:35 +05:30
|
|
|
if conn == nil {
|
2024-06-18 00:52:39 +05:30
|
|
|
return 0, fmt.Errorf("default cache not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
testData := fmt.Sprintf("%x", make([]byte, 500))
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
2024-06-23 15:08:35 +05:30
|
|
|
if err := conn.Delete(testCacheKey); err != nil {
|
2024-06-18 00:52:39 +05:30
|
|
|
return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err)
|
|
|
|
}
|
2024-06-23 15:08:35 +05:30
|
|
|
if err := conn.Put(testCacheKey, testData, 10); err != nil {
|
2024-06-18 00:52:39 +05:30
|
|
|
return 0, fmt.Errorf("expect cache to store data but got: %w", err)
|
|
|
|
}
|
2024-06-23 15:08:35 +05:30
|
|
|
testVal := conn.Get(testCacheKey)
|
|
|
|
if testVal == nil {
|
2024-06-18 00:52:39 +05:30
|
|
|
return 0, fmt.Errorf("expect cache hit but got none")
|
|
|
|
}
|
|
|
|
if testVal != testData {
|
|
|
|
return 0, fmt.Errorf("expect cache to return same value as stored but got other")
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Since(start), nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
value, err := getFunc()
|
2020-03-27 18:04:39 +05:30
|
|
|
if err != nil {
|
2022-11-10 12:13:53 +05:30
|
|
|
return value, err
|
2020-03-27 18:04:39 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2020-03-27 18:04:39 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
if value, ok := cached.(string); ok {
|
|
|
|
return value, nil
|
2020-03-27 18:04:39 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
if stringer, ok := cached.(fmt.Stringer); ok {
|
|
|
|
return stringer.String(), nil
|
2020-03-27 18:04:39 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
return fmt.Sprintf("%s", cached), nil
|
2020-03-27 18:04:39 +05:30
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
value, err := getFunc()
|
2019-06-13 01:11:28 +05:30
|
|
|
if err != nil {
|
2022-11-10 12:13:53 +05:30
|
|
|
return value, err
|
2019-06-13 01:11:28 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
switch v := cached.(type) {
|
2017-11-16 12:36:34 +05:30
|
|
|
case int:
|
2022-11-10 12:13:53 +05:30
|
|
|
return v, nil
|
2017-11-16 12:36:34 +05:30
|
|
|
case string:
|
2022-11-10 12:13:53 +05:30
|
|
|
value, err := strconv.Atoi(v)
|
2017-11-16 12:36:34 +05:30
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
return value, nil
|
2017-11-16 12:36:34 +05:30
|
|
|
default:
|
2022-11-10 12:13:53 +05:30
|
|
|
value, err := getFunc()
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-11-16 12:36:34 +05:30
|
|
|
}
|
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()
|
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
cached := conn.Get(key)
|
|
|
|
|
|
|
|
if cached == nil {
|
|
|
|
value, err := getFunc()
|
2019-06-13 01:11:28 +05:30
|
|
|
if err != nil {
|
2022-11-10 12:13:53 +05:30
|
|
|
return value, err
|
2019-06-13 01:11:28 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-10-26 07:07:33 +05:30
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
|
|
|
|
switch v := conn.Get(key).(type) {
|
2017-11-16 12:36:34 +05:30
|
|
|
case int64:
|
2022-11-10 12:13:53 +05:30
|
|
|
return v, nil
|
2017-11-16 12:36:34 +05:30
|
|
|
case string:
|
2022-11-10 12:13:53 +05:30
|
|
|
value, err := strconv.ParseInt(v, 10, 64)
|
2017-11-16 12:36:34 +05:30
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-10 12:13:53 +05:30
|
|
|
return value, nil
|
2017-11-16 12:36:34 +05:30
|
|
|
default:
|
2022-11-10 12:13:53 +05:30
|
|
|
value, err := getFunc()
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-11-16 12:36:34 +05:30
|
|
|
}
|
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
|
|
|
}
|