1 Star 0 Fork 0

masaichi/工具包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
StringOperation.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
邓正锋 提交于 2024-05-17 16:02 +08:00 . 添加redis操作方法
package goredis
import (
"context"
"gitee.com/masaichi/mastool/masredis/mstruct"
"time"
)
type StringOperation struct {
ctx context.Context
mstruct.MCacheConfig
}
func NewStringOperation(config mstruct.MCacheConfig) *StringOperation {
return &StringOperation{ctx: context.Background(), MCacheConfig: config}
}
func (this *StringOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *InterfaceResult {
exp := OperationAttrs(attrs).Find(ATTR_EXPIRE).UnWrapDefault(time.Second * 0).(time.Duration)
return NewInterfaceResult(Redis(this.MCacheConfig).Set(this.ctx, key, value, exp).Result())
}
func (this *StringOperation) SetNx(key string, value interface{}, attrs ...*OperationAttr) *InterfaceResult {
exp := OperationAttrs(attrs).Find(ATTR_EXPIRE).UnWrapDefault(time.Second * 0).(time.Duration)
return NewInterfaceResult(Redis(this.MCacheConfig).SetNX(this.ctx, key, value, exp).Result())
}
// 如果有值,则执行,常用于开关业务,比如新闻缓存,有key的走缓存逻辑
// 标记业务数据,多个步骤删除数据,第一步 软删除 第二步 如果setxx为true,则数据库删除
func (this *StringOperation) SetXx(key string, value interface{}, attrs ...*OperationAttr) *InterfaceResult {
exp := OperationAttrs(attrs).Find(ATTR_EXPIRE).UnWrapDefault(time.Second * 0).(time.Duration)
return NewInterfaceResult(Redis(this.MCacheConfig).SetXX(this.ctx, key, value, exp).Result())
}
func (this *StringOperation) Get(key string) *StringResult {
return NewStringResult(Redis(this.MCacheConfig).Get(this.ctx, key).Result())
}
// 删除缓存
func (this *StringOperation) Del(key string) {
Redis(this.MCacheConfig).Del(this.ctx, key)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/masaichi/mastool.git
git@gitee.com:masaichi/mastool.git
masaichi
mastool
工具包
v0.0.4

搜索帮助