From bba410cb3c8b2328fe3ea60642c9a13f146b0237 Mon Sep 17 00:00:00 2001 From: zhaocy Date: Thu, 16 Jun 2022 19:00:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96hm=E5=AD=97=E6=AE=B5=E7=9A=84?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego/sys/redis/sys_test.go | 5 ++++- modules/model_comp.go | 40 ++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lego/sys/redis/sys_test.go b/lego/sys/redis/sys_test.go index f78895062..ee6d804f1 100644 --- a/lego/sys/redis/sys_test.go +++ b/lego/sys/redis/sys_test.go @@ -191,12 +191,15 @@ func Test_Redis_Encoder_Hash(t *testing.T) { // redis.HSet("test:1003", "Name", "eeee") name := "" - err := redis.HGet("test:1003", "Name", &name) + err := redis.HGet("test:103", "name", &name) if err != nil { fmt.Println(err) } fmt.Println(name) + + redis.HMSet("",) + // data1 := map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}} // err := redis.HMSet("test:1004", data1) // fmt.Printf("err:%v\n", err) diff --git a/modules/model_comp.go b/modules/model_comp.go index 8bc7fe7eb..8db82d068 100644 --- a/modules/model_comp.go +++ b/modules/model_comp.go @@ -10,6 +10,7 @@ import ( "go_dreamfactory/lego/sys/redis" "go_dreamfactory/sys/cache" "go_dreamfactory/sys/db" + "reflect" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" @@ -111,37 +112,30 @@ func (this *Model_Comp) SetObj(uid string, data proto.Message, attrs ...*cache.O return this.logOpt(uid, data, attrs...) } -//cache hashmap data参数 允许map或protobuf +//缓存多个字段的数据 data参数 允许map或protobuf //eg.map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}} //or &TestData{Name: "liwei1dao", Agr: 12, Sub: &TestAny{SubName: "test", Age: 20}} // attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作,不传表示更新,前提不能传入传入WithDisabledMgoLog() +//如果更新数据,uid作为where条件之一,如果检索结果不能确定唯一,此时data 必需是map[string]interface{}类型,必需包含_id 字段 func (this *Model_Comp) SetHM(uid string, data interface{}, attrs ...*cache.OperationAttr) error { err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.TableName, uid), data) if err != nil { - log.Errorf("HMSet err: %v", err) + log.Errorf("SetHM err: %v", err) return err } return this.logOpt(uid, data, attrs...) } -//更新缓存字段 -//isnew true insertlog , false updatelog -func (this *Model_Comp) Set(uid string, v map[string]interface{}, isnew, islog bool) error { - err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.TableName, uid), v) +//缓存一个字段的数据 +//如果更新数据,uid作为where条件之一,如果检索结果不能确定唯一,此时data 必需是map[string]interface{}类型,必需包含_id 字段 +func (this *Model_Comp) SetH(uid string, field string, data interface{}, attrs ...*cache.OperationAttr) error { + err := this.Redis.HSet(fmt.Sprintf("%s:%s", this.TableName, uid), field, data) if err != nil { - log.Errorf("set err:%v", err) + log.Errorf("SetH err %v", err) return err } - - if islog { - if isnew { - return this.InsertModelLogs(this.TableName, uid, v) - } else { - return this.UpdateModelLogs(this.TableName, uid, bson.M{"uid": uid}, v) - } - } - return nil + return this.logOpt(uid, data, attrs...) } //获取缓存JSON数据 @@ -172,12 +166,12 @@ func (this *Model_Comp) GetObj(uid string, v proto.Message) error { //获取对象数据 //data -func (this *Model_Comp) GetAll(uid string, data interface{}) error { +func (this *Model_Comp) GetHM(uid string, data interface{}) error { return this.Redis.HGetAll(fmt.Sprintf("%s:%s", this.TableName, uid), data) } //获取字段数据 缓存存储的数据为hashmap时 -func (this *Model_Comp) GetField(uid string, field string, v interface{}) error { +func (this *Model_Comp) GetH(uid string, field string, v interface{}) error { return this.Redis.HGet(fmt.Sprintf("%s:%s", this.TableName, uid), field, v) } @@ -198,7 +192,15 @@ func (this *Model_Comp) logOpt(uid string, data interface{}, attrs ...*cache.Ope if ret == nil { ir := cache.OperationAttrs(attrs).Find(cache.ATTR_INSERT).Unwrap_Or(nil) if ir != nil && ir.(bool) { - return this.UpdateModelLogs(this.TableName, uid, bson.M{"uid": uid}, data) + where := bson.M{"uid": uid} + if reflect.ValueOf(data).Kind() == reflect.Map { + if m, ok := data.(map[string]interface{}); ok { + where["_id"] = m["_id"] + } else { + return fmt.Errorf("have %v,but want map[string]interface{}", data) + } + } + return this.UpdateModelLogs(this.TableName, uid, where, data) } else { return this.InsertModelLogs(this.TableName, uid, data) }