2017-10-25 21:37:33 -04: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 02:06:34 -05:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2017-10-25 21:37:33 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2019-08-23 12:40:30 -04:00
|
|
|
mc "gitea.com/macaron/cache"
|
2020-01-29 02:47:46 -05:00
|
|
|
|
|
|
|
_ "gitea.com/macaron/cache/memcache" // memcache plugin for cache
|
|
|
|
_ "gitea.com/macaron/cache/redis"
|
2017-10-25 21:37:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var conn mc.Cache
|
|
|
|
|
|
|
|
// NewContext start cache service
|
|
|
|
func NewContext() error {
|
|
|
|
if setting.CacheService == nil || conn != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
conn, err = mc.NewCacher(setting.CacheService.Adapter, mc.Options{
|
|
|
|
Adapter: setting.CacheService.Adapter,
|
|
|
|
AdapterConfig: setting.CacheService.Conn,
|
|
|
|
Interval: setting.CacheService.Interval,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12 15:41:28 -04:00
|
|
|
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-10-25 21:37:33 -04:00
|
|
|
}
|
2017-11-16 02:06:34 -05: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 21:37:33 -04: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
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
err = conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-10-25 21:37:33 -04:00
|
|
|
}
|
2017-11-16 02:06:34 -05: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 21:37:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove key from cache
|
|
|
|
func Remove(key string) {
|
|
|
|
if conn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
_ = conn.Delete(key)
|
2017-10-25 21:37:33 -04:00
|
|
|
}
|