53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
|
package myRedis
|
||
|
|
||
|
import (
|
||
|
"github.com/redis/go-redis/v9"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type StringCache struct {
|
||
|
Operation *StringOperation
|
||
|
Expire time.Duration
|
||
|
DefaultString string
|
||
|
}
|
||
|
|
||
|
func NewStringCache(redisClient *redis.Client) *StringCache {
|
||
|
return &StringCache{
|
||
|
Operation: NewStringOperation(redisClient),
|
||
|
Expire: time.Second * 0,
|
||
|
DefaultString: "",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (t *StringCache) SetExpire(expire time.Duration) *StringCache {
|
||
|
t.Expire = expire
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *StringCache) SetDefaultString(defaultString string) *StringCache {
|
||
|
t.DefaultString = defaultString
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *StringCache) SetCache(key string, value string) {
|
||
|
t.Operation.Set(key, value, WithExpire(t.Expire))
|
||
|
}
|
||
|
|
||
|
func (t *StringCache) GetCache(key string) (ret string) {
|
||
|
ret = t.Operation.Get(key).UnwrapOrElse(func() string {
|
||
|
if t.DefaultString != "" {
|
||
|
t.SetCache(key, t.DefaultString)
|
||
|
}
|
||
|
return t.DefaultString
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (t *StringCache) IsExist(key string) bool {
|
||
|
return t.Operation.Exist(key).UnwrapOr(0) != 0
|
||
|
}
|
||
|
|
||
|
func (t *StringCache) DelCache(key string) int64 {
|
||
|
return t.Operation.Del(key).UnwrapOr(0)
|
||
|
}
|