添加缓存失效设置

This commit is contained in:
zhaocy 2022-06-17 16:33:46 +08:00
parent 7de30d1a69
commit 028727449b
3 changed files with 19 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import (
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"reflect"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -108,7 +109,8 @@ func (this *Model_Comp) UpdateModelLogs(table string, uID string, where bson.M,
//data 值允许protobuf格式的对象
// attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作不传表示更新前提不能传入传入WithDisabledMgoLog()
func (this *Model_Comp) SetObj(uid string, data proto.Message, attrs ...*cache.OperationAttr) error {
err := this.Redis.Set(this.ukey(uid), data, 0)
expr := cache.OperationAttrs(attrs).Find(cache.ATTR_EXPIRE).Unwrap_Or(time.Second * 0).(time.Duration)
err := this.Redis.Set(this.ukey(uid), data, expr)
if err != nil {
log.Errorf("set err:%v", err)
return err

View File

@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"go_dreamfactory/modules/dbservice"
"go_dreamfactory/modules/friend"
"go_dreamfactory/modules/mail"
"go_dreamfactory/modules/pack"
@ -41,7 +40,7 @@ func main() {
pack.NewModule(),
mail.NewModule(),
friend.NewModule(),
dbservice.NewModule(),
// dbservice.NewModule(),
)
}

View File

@ -1,8 +1,12 @@
package cache
import "fmt"
import (
"fmt"
"time"
)
const (
ATTR_EXPIRE = "expire"
ATTR_MGOLOG = "mgolog"
ATTR_INSERT = "insert"
@ -32,6 +36,15 @@ func (this OperationAttrs) Find(name string) *InterfaceResult {
return NewInterfaceResult(nil, fmt.Errorf("Operationattrs not found err: %v", name))
}
//缓存过期时间设置
func WithExpire(t time.Duration) *OperationAttr {
return &OperationAttr{
Name: ATTR_EXPIRE,
Value: t,
}
}
//禁用Mgolog操作
func WithDisabledMgoLog() *OperationAttr {
return &OperationAttr{
Name: ATTR_MGOLOG,
@ -39,6 +52,7 @@ func WithDisabledMgoLog() *OperationAttr {
}
}
//新记录插入操作
func WithND() *OperationAttr {
return &OperationAttr{
Name: ATTR_INSERT,