2017-10-26 03:37:33 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-26 03:37:33 +02:00
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2024-06-17 21:22:39 +02:00
|
|
|
"fmt"
|
2017-11-16 08:06:34 +01:00
|
|
|
"strconv"
|
2024-04-13 10:38:44 +02:00
|
|
|
"time"
|
2017-11-16 08:06:34 +01:00
|
|
|
|
2017-10-26 03:37:33 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-05-27 15:43:32 +02:00
|
|
|
|
|
|
|
_ "gitea.com/go-chi/cache/memcache" //nolint:depguard // memcache plugin for cache, it is required for config "ADAPTER=memcache"
|
2017-10-26 03:37:33 +02:00
|
|
|
)
|
|
|
|
|
2024-04-13 10:38:44 +02:00
|
|
|
var defaultCache StringCache
|
2017-10-26 03:37:33 +02:00
|
|
|
|
2023-12-19 10:29:05 +01:00
|
|
|
// Init start cache service
|
|
|
|
func Init() error {
|
2024-04-13 10:38:44 +02:00
|
|
|
if defaultCache == nil {
|
|
|
|
c, err := NewStringCache(setting.CacheService.Cache)
|
|
|
|
if err != nil {
|
2020-02-01 20:11:32 +01:00
|
|
|
return err
|
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if err = c.Ping(); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-12-05 17:24:57 +01:00
|
|
|
return err
|
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
defaultCache = c
|
2017-10-26 03:37:33 +02:00
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
return nil
|
2017-10-26 03:37:33 +02:00
|
|
|
}
|
|
|
|
|
2024-06-17 21:22:39 +02:00
|
|
|
const (
|
|
|
|
testCacheKey = "DefaultCache.TestKey"
|
|
|
|
SlowCacheThreshold = 100 * time.Microsecond
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test() (time.Duration, error) {
|
|
|
|
if defaultCache == nil {
|
|
|
|
return 0, fmt.Errorf("default cache not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
testData := fmt.Sprintf("%x", make([]byte, 500))
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
if err := defaultCache.Delete(testCacheKey); err != nil {
|
|
|
|
return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err)
|
|
|
|
}
|
|
|
|
if err := defaultCache.Put(testCacheKey, testData, 10); err != nil {
|
|
|
|
return 0, fmt.Errorf("expect cache to store data but got: %w", err)
|
|
|
|
}
|
|
|
|
testVal, hit := defaultCache.Get(testCacheKey)
|
|
|
|
if !hit {
|
|
|
|
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 15:00:47 +01:00
|
|
|
// GetCache returns the currently configured cache
|
2024-04-13 10:38:44 +02:00
|
|
|
func GetCache() StringCache {
|
|
|
|
return defaultCache
|
2020-12-17 15:00:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 13:34:39 +01: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) {
|
2024-04-13 10:38:44 +02:00
|
|
|
if defaultCache == nil || setting.CacheService.TTL == 0 {
|
2020-03-27 13:34:39 +01:00
|
|
|
return getFunc()
|
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
cached, exist := defaultCache.Get(key)
|
|
|
|
if !exist {
|
2022-11-10 07:43:53 +01:00
|
|
|
value, err := getFunc()
|
2020-03-27 13:34:39 +01:00
|
|
|
if err != nil {
|
2022-11-10 07:43:53 +01:00
|
|
|
return value, err
|
2020-03-27 13:34:39 +01:00
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
return value, defaultCache.Put(key, value, setting.CacheService.TTLSeconds())
|
2017-11-16 08:06:34 +01:00
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
return cached, nil
|
2017-10-26 03:37:33 +02: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) {
|
2024-04-13 10:38:44 +02:00
|
|
|
s, err := GetString(key, func() (string, error) {
|
|
|
|
v, err := getFunc()
|
|
|
|
return strconv.FormatInt(v, 10), err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2017-10-26 03:37:33 +02:00
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
if s == "" {
|
|
|
|
return 0, nil
|
2017-11-16 08:06:34 +01:00
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
return strconv.ParseInt(s, 10, 64)
|
2017-10-26 03:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove key from cache
|
|
|
|
func Remove(key string) {
|
2024-04-13 10:38:44 +02:00
|
|
|
if defaultCache == nil {
|
2017-10-26 03:37:33 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-13 10:38:44 +02:00
|
|
|
_ = defaultCache.Delete(key)
|
2017-10-26 03:37:33 +02:00
|
|
|
}
|