From 2a556a9db647245ea22d1dae0c7bd31c325c22b4 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Sun, 28 Nov 2021 19:47:08 +0800 Subject: [PATCH] fix(structure-stringx): fix GetSet set init nil key --- storage/dao/stringx.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/storage/dao/stringx.go b/storage/dao/stringx.go index 21cadf9..87de94a 100644 --- a/storage/dao/stringx.go +++ b/storage/dao/stringx.go @@ -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() + _, updateLength := strVal.Set(value) + d.lru.UpdateLruSize(updateLength) } - strVal, ok := val.(structure.StringXInterface) - if !ok { - return nil, errorx.DaoTypeErr("stringx") - } - - oldValue := strVal.Get() - - _, updateLength := strVal.Set(value) - d.lru.UpdateLruSize(updateLength) return &proto.GetSetResponse{Result: oldValue}, nil }