49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
|
package myRedis
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/redis/go-redis/v9"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type StringOperation struct {
|
||
|
ctx context.Context
|
||
|
client *redis.Client
|
||
|
}
|
||
|
|
||
|
func NewStringOperation(client *redis.Client) *StringOperation {
|
||
|
return &StringOperation{ctx: context.Background(), client: client}
|
||
|
}
|
||
|
|
||
|
func (t *StringOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *InterfaceResult {
|
||
|
exp := OperationAttrs(attrs).Find(AttrExpr).UnwrapOr(0 * time.Second).(time.Duration)
|
||
|
|
||
|
nx := OperationAttrs(attrs).Find(AttrNx).UnwrapOr(nil)
|
||
|
if nx != nil {
|
||
|
return NewInterfaceResult(t.client.SetNX(t.ctx, key, value, exp).Result())
|
||
|
}
|
||
|
|
||
|
xx := OperationAttrs(attrs).Find(AttrXx).UnwrapOr(nil)
|
||
|
if xx != nil {
|
||
|
return NewInterfaceResult(t.client.SetXX(t.ctx, key, value, exp).Result())
|
||
|
}
|
||
|
|
||
|
return NewInterfaceResult(t.client.Set(t.ctx, key, value, exp).Result())
|
||
|
}
|
||
|
|
||
|
func (t *StringOperation) Get(key string) *StringResult {
|
||
|
return NewStringResult(t.client.Get(t.ctx, key).Result())
|
||
|
}
|
||
|
|
||
|
func (t *StringOperation) MGet(key ...string) *SliceResult {
|
||
|
return NewSliceResult(t.client.MGet(t.ctx, key...).Result())
|
||
|
}
|
||
|
|
||
|
func (t *StringOperation) Del(key string) *IntResult {
|
||
|
return NewIntResult(t.client.Del(t.ctx, key).Result())
|
||
|
}
|
||
|
|
||
|
func (t *StringOperation) Exist(key string) *IntResult {
|
||
|
return NewIntResult(t.client.Exists(t.ctx, key).Result())
|
||
|
}
|