feat(dao): add dao

This commit is contained in:
bandl 2021-10-20 21:35:27 +08:00
parent 56d69f63af
commit a374638758
1 changed files with 61 additions and 0 deletions

View File

@ -18,6 +18,7 @@ func NewDao(lru lru.CacheInterface) *Dao {
} }
} }
// stringx 相关的方法
func (d *Dao) Set(key *proto.BaseKey, strVal string) (string, error) { func (d *Dao) Set(key *proto.BaseKey, strVal string) (string, error) {
value, ok := d.lru.Get(key) value, ok := d.lru.Get(key)
if ok { if ok {
@ -133,3 +134,63 @@ func (d *Dao) GetBit(key *proto.BaseKey, offer int32) (bool, error) {
} }
return strVal.Getbit(offer) return strVal.Getbit(offer)
} }
func (d *Dao) Getrange(key *proto.BaseKey, start, end int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return "", errorx.NotKeyErr(key.Key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
return strVal.Getrange(start, end)
}
func (d *Dao) Getset(key *proto.BaseKey, value string) (string, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
oldValue := strVal.Get()
_, updateLength := strVal.Set(value)
d.lru.UpdateLruSize(updateLength)
return oldValue, nil
}
func (d *Dao) Strlen(key *proto.BaseKey) (int32, error) {
val, ok := d.lru.Get(key)
if !ok {
return 0, errorx.NotKeyErr(key.Key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return 0, errorx.DaoTypeErr("stringx")
}
return int32(strVal.GetLength()), nil
}
func (d *Dao) Setnx(key *proto.BaseKey, val string) error {
_, ok := d.lru.Get(key)
if ok {
return errorx.New("the key already exists")
}
strValue := stringx.NewStringSingle()
strValue.Set(val)
err := d.lru.Add(key, strValue)
if err != nil {
return err
}
return nil
}