fix(structure-stringx): fix GetSet set init nil key

This commit is contained in:
bandl 2021-11-28 19:47:08 +08:00
parent c0da22ef6f
commit 2a556a9db6
1 changed files with 19 additions and 10 deletions

View File

@ -147,19 +147,28 @@ func (d *Dao) GetRange(key *proto.BaseKey, start, end int32) (*proto.GetRangeRes
func (d *Dao) GetSet(key *proto.BaseKey, value string) (*proto.GetSetResponse, error) {
val, ok := d.lru.Get(key)
var oldValue string
if !ok {
return nil, errorx.NotKeyErr(key.Key)
oldValue = ""
strValue := stringx.NewStringSingle()
strValue.Set(value)
err := d.lru.Add(key, strValue)
if err != nil {
return nil, err
}
} else {
strVal, ok := val.(structure.StringXInterface)
if !ok {
return nil, errorx.DaoTypeErr("stringx")
}
oldValue := strVal.Get()
oldValue = strVal.Get()
_, updateLength := strVal.Set(value)
d.lru.UpdateLruSize(updateLength)
}
return &proto.GetSetResponse{Result: oldValue}, nil
}