添加缓存失效设置

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/cache"
"go_dreamfactory/sys/db" "go_dreamfactory/sys/db"
"reflect" "reflect"
"time"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "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格式的对象 //data 值允许protobuf格式的对象
// attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作不传表示更新前提不能传入传入WithDisabledMgoLog() // attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作不传表示更新前提不能传入传入WithDisabledMgoLog()
func (this *Model_Comp) SetObj(uid string, data proto.Message, attrs ...*cache.OperationAttr) error { 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 { if err != nil {
log.Errorf("set err:%v", err) log.Errorf("set err:%v", err)
return err return err

View File

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

View File

@ -1,8 +1,12 @@
package cache package cache
import "fmt" import (
"fmt"
"time"
)
const ( const (
ATTR_EXPIRE = "expire"
ATTR_MGOLOG = "mgolog" ATTR_MGOLOG = "mgolog"
ATTR_INSERT = "insert" 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)) 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 { func WithDisabledMgoLog() *OperationAttr {
return &OperationAttr{ return &OperationAttr{
Name: ATTR_MGOLOG, Name: ATTR_MGOLOG,
@ -39,6 +52,7 @@ func WithDisabledMgoLog() *OperationAttr {
} }
} }
//新记录插入操作
func WithND() *OperationAttr { func WithND() *OperationAttr {
return &OperationAttr{ return &OperationAttr{
Name: ATTR_INSERT, Name: ATTR_INSERT,