mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.1 KiB
141 lines
3.1 KiB
4 years ago
|
// Copyright 2020 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.
|
||
8 years ago
|
|
||
|
package cache
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"code.gitea.io/gitea/modules/nosql"
|
||
8 years ago
|
|
||
5 years ago
|
"gitea.com/macaron/cache"
|
||
4 years ago
|
"github.com/go-redis/redis/v7"
|
||
|
"github.com/unknwon/com"
|
||
8 years ago
|
)
|
||
|
|
||
|
// RedisCacher represents a redis cache adapter implementation.
|
||
|
type RedisCacher struct {
|
||
4 years ago
|
c redis.UniversalClient
|
||
8 years ago
|
prefix string
|
||
|
hsetName string
|
||
|
occupyMode bool
|
||
|
}
|
||
|
|
||
|
// Put puts value into cache with key and expire time.
|
||
|
// If expired is 0, it lives forever.
|
||
|
func (c *RedisCacher) Put(key string, val interface{}, expire int64) error {
|
||
|
key = c.prefix + key
|
||
|
if expire == 0 {
|
||
5 years ago
|
if err := c.c.Set(key, com.ToStr(val), 0).Err(); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
dur, err := time.ParseDuration(com.ToStr(expire) + "s")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
if err = c.c.Set(key, com.ToStr(val), dur).Err(); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if c.occupyMode {
|
||
|
return nil
|
||
|
}
|
||
|
return c.c.HSet(c.hsetName, key, "0").Err()
|
||
|
}
|
||
|
|
||
|
// Get gets cached value by given key.
|
||
|
func (c *RedisCacher) Get(key string) interface{} {
|
||
|
val, err := c.c.Get(c.prefix + key).Result()
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
// Delete deletes cached value by given key.
|
||
|
func (c *RedisCacher) Delete(key string) error {
|
||
|
key = c.prefix + key
|
||
|
if err := c.c.Del(key).Err(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if c.occupyMode {
|
||
|
return nil
|
||
|
}
|
||
|
return c.c.HDel(c.hsetName, key).Err()
|
||
|
}
|
||
|
|
||
|
// Incr increases cached int-type value by given key as a counter.
|
||
|
func (c *RedisCacher) Incr(key string) error {
|
||
|
if !c.IsExist(key) {
|
||
|
return fmt.Errorf("key '%s' not exist", key)
|
||
|
}
|
||
|
return c.c.Incr(c.prefix + key).Err()
|
||
|
}
|
||
|
|
||
|
// Decr decreases cached int-type value by given key as a counter.
|
||
|
func (c *RedisCacher) Decr(key string) error {
|
||
|
if !c.IsExist(key) {
|
||
|
return fmt.Errorf("key '%s' not exist", key)
|
||
|
}
|
||
|
return c.c.Decr(c.prefix + key).Err()
|
||
|
}
|
||
|
|
||
|
// IsExist returns true if cached value exists.
|
||
|
func (c *RedisCacher) IsExist(key string) bool {
|
||
5 years ago
|
if c.c.Exists(c.prefix+key).Val() == 1 {
|
||
8 years ago
|
return true
|
||
|
}
|
||
|
|
||
|
if !c.occupyMode {
|
||
|
c.c.HDel(c.hsetName, c.prefix+key)
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// Flush deletes all cached data.
|
||
|
func (c *RedisCacher) Flush() error {
|
||
|
if c.occupyMode {
|
||
4 years ago
|
return c.c.FlushDB().Err()
|
||
8 years ago
|
}
|
||
|
|
||
|
keys, err := c.c.HKeys(c.hsetName).Result()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err = c.c.Del(keys...).Err(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return c.c.Del(c.hsetName).Err()
|
||
|
}
|
||
|
|
||
|
// StartAndGC starts GC routine based on config string settings.
|
||
|
// AdapterConfig: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180,hset_name=MacaronCache,prefix=cache:
|
||
|
func (c *RedisCacher) StartAndGC(opts cache.Options) error {
|
||
|
c.hsetName = "MacaronCache"
|
||
|
c.occupyMode = opts.OccupyMode
|
||
|
|
||
4 years ago
|
uri := nosql.ToRedisURI(opts.AdapterConfig)
|
||
8 years ago
|
|
||
4 years ago
|
c.c = nosql.GetManager().GetRedisClient(uri.String())
|
||
|
|
||
|
for k, v := range uri.Query() {
|
||
8 years ago
|
switch k {
|
||
|
case "hset_name":
|
||
4 years ago
|
c.hsetName = v[0]
|
||
8 years ago
|
case "prefix":
|
||
4 years ago
|
c.prefix = v[0]
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
return c.c.Ping().Err()
|
||
8 years ago
|
}
|
||
|
|
||
|
func init() {
|
||
|
cache.Register("redis", &RedisCacher{})
|
||
|
}
|