| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- // Author: simon (ynwdlxm@163.com)
- // Date: 2025/7/21 09:56
- // Desc: redis 缓存,实现 Store 接口
- package cache
- import (
- "time"
- "github.com/runningwater/gohub/pkg/config"
- "github.com/runningwater/gohub/pkg/redis"
- )
- type RedisStore struct {
- RedisClient *redis.Client
- KeyPrefix string
- }
- func NewRedisStore(address, username, password string, db int) *RedisStore {
- rs := &RedisStore{
- RedisClient: redis.NewClient(address, username, password, db),
- KeyPrefix: config.GetString("app.name") + ":cache:",
- }
- return rs
- }
- func (r *RedisStore) Set(key, value string, expire time.Duration) {
- r.RedisClient.Set(r.KeyPrefix+key, value, expire)
- }
- func (r *RedisStore) Get(key string) string {
- return r.RedisClient.Get(r.KeyPrefix + key)
- }
- func (r *RedisStore) Has(key string) bool {
- return r.RedisClient.Has(r.KeyPrefix + key)
- }
- func (r *RedisStore) Forget(key string) {
- r.RedisClient.Del(r.KeyPrefix + key)
- }
- func (r *RedisStore) Forever(key, value string) {
- r.RedisClient.Set(r.KeyPrefix+key, value, 0)
- }
- func (r *RedisStore) Flush() {
- r.RedisClient.FlushDb()
- }
- func (r *RedisStore) IsAlive() error {
- return r.RedisClient.Ping()
- }
- func (r *RedisStore) Increment(params ...any) {
- r.RedisClient.Increment(params)
- }
- func (r *RedisStore) Decrement(params ...any) {
- r.RedisClient.Decrement(params)
- }
|