Files
Gitea/modules/cache/cache_redis.go
T
silverwind bea6fcaa84 refactor: move go-chi/cache into Gitea (#39530)
The `gitea.com/go-chi/cache` package only exists for Gitea, so it moves
into `modules/cache`.

- `ITEM_TTL = -1` disables caching as documented
- Sub-second TTLs round up instead of never expiring
- The Redis adapter no longer grows a `MacaronCache` hash
- Use two-queue cache for in-memory cache

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-10-02 18:16:34 +02:00

48 lines
1.2 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package cache
import (
"time"
"gitea.dev/modules/graceful"
"gitea.dev/modules/nosql"
"github.com/redis/go-redis/v9"
)
type redisCache struct {
client redis.UniversalClient
prefix string
}
func newRedisCache(conn string) backend {
uri := nosql.ToRedisURI(conn)
return &redisCache{
client: nosql.GetManager().GetRedisClient(uri.String()),
prefix: uri.Query().Get("prefix"),
}
}
func (c *redisCache) Get(key string) (string, bool) {
value, err := c.client.Get(graceful.GetManager().HammerContext(), c.prefix+key).Result()
return value, err == nil
}
func (c *redisCache) Put(key, value string, ttl int64) error {
return c.client.Set(graceful.GetManager().HammerContext(), c.prefix+key, value, time.Duration(ttl)*time.Second).Err()
}
func (c *redisCache) Delete(key string) error {
return c.client.Del(graceful.GetManager().HammerContext(), c.prefix+key).Err()
}
func (c *redisCache) IsExist(key string) bool {
return c.client.Exists(graceful.GetManager().HammerContext(), c.prefix+key).Val() == 1
}
func (c *redisCache) Ping() error {
return c.client.Ping(graceful.GetManager().HammerContext()).Err()
}