优化hm字段的更新

This commit is contained in:
zhaocy 2022-06-16 19:00:50 +08:00
parent 67190818ae
commit bba410cb3c
2 changed files with 25 additions and 20 deletions

View File

@ -191,12 +191,15 @@ func Test_Redis_Encoder_Hash(t *testing.T) {
// redis.HSet("test:1003", "Name", "eeee") // redis.HSet("test:1003", "Name", "eeee")
name := "" name := ""
err := redis.HGet("test:1003", "Name", &name) err := redis.HGet("test:103", "name", &name)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Println(name) fmt.Println(name)
redis.HMSet("",)
// data1 := map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}} // data1 := map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}}
// err := redis.HMSet("test:1004", data1) // err := redis.HMSet("test:1004", data1)
// fmt.Printf("err:%v\n", err) // fmt.Printf("err:%v\n", err)

View File

@ -10,6 +10,7 @@ import (
"go_dreamfactory/lego/sys/redis" "go_dreamfactory/lego/sys/redis"
"go_dreamfactory/sys/cache" "go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db" "go_dreamfactory/sys/db"
"reflect"
"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"
@ -111,37 +112,30 @@ func (this *Model_Comp) SetObj(uid string, data proto.Message, attrs ...*cache.O
return this.logOpt(uid, data, attrs...) 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}} //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}} //or &TestData{Name: "liwei1dao", Agr: 12, Sub: &TestAny{SubName: "test", Age: 20}}
// attrs 操作可选项目 eg.传入WithDisabledMgoLog() 表示关闭日志,否则开启;WithND() 传入表示插入操作不传表示更新前提不能传入传入WithDisabledMgoLog() // 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 { 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) err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.TableName, uid), data)
if err != nil { if err != nil {
log.Errorf("HMSet err: %v", err) log.Errorf("SetHM err: %v", err)
return err return err
} }
return this.logOpt(uid, data, attrs...) return this.logOpt(uid, data, attrs...)
} }
//更新缓存字段 //缓存一个字段的数据
//isnew true insertlog , false updatelog //如果更新数据uid作为where条件之一如果检索结果不能确定唯一此时data 必需是map[string]interface{}类型必需包含_id 字段
func (this *Model_Comp) Set(uid string, v map[string]interface{}, isnew, islog bool) error { func (this *Model_Comp) SetH(uid string, field string, data interface{}, attrs ...*cache.OperationAttr) error {
err := this.Redis.HMSet(fmt.Sprintf("%s:%s", this.TableName, uid), v) err := this.Redis.HSet(fmt.Sprintf("%s:%s", this.TableName, uid), field, data)
if err != nil { if err != nil {
log.Errorf("set err:%v", err) log.Errorf("SetH err %v", err)
return err return err
} }
return this.logOpt(uid, data, attrs...)
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
} }
//获取缓存JSON数据 //获取缓存JSON数据
@ -172,12 +166,12 @@ func (this *Model_Comp) GetObj(uid string, v proto.Message) error {
//获取对象数据 //获取对象数据
//data //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) return this.Redis.HGetAll(fmt.Sprintf("%s:%s", this.TableName, uid), data)
} }
//获取字段数据 缓存存储的数据为hashmap时 //获取字段数据 缓存存储的数据为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) 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 { if ret == nil {
ir := cache.OperationAttrs(attrs).Find(cache.ATTR_INSERT).Unwrap_Or(nil) ir := cache.OperationAttrs(attrs).Find(cache.ATTR_INSERT).Unwrap_Or(nil)
if ir != nil && ir.(bool) { 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 { } else {
return this.InsertModelLogs(this.TableName, uid, data) return this.InsertModelLogs(this.TableName, uid, data)
} }