forked from p93542168/wheat-cache
feat(stringx): update dao stringx
This commit is contained in:
parent
1f85847243
commit
5642a00f61
|
@ -8,15 +8,15 @@ import (
|
|||
)
|
||||
|
||||
// stringx 相关的方法
|
||||
func (d *Dao) Set(key *proto.BaseKey, strVal string) (string, error) {
|
||||
func (d *Dao) Set(key *proto.BaseKey, strVal string) (*proto.SetResponse, error) {
|
||||
value, ok := d.lru.Get(key)
|
||||
if ok {
|
||||
if val, ok := value.(structure.StringXInterface); ok {
|
||||
res, length := val.Set(strVal)
|
||||
d.lru.UpdateLruSize(length)
|
||||
return res, nil
|
||||
return &proto.SetResponse{Result: res}, nil
|
||||
} else {
|
||||
return "", errorx.New("the key:%s is not stringx type", key)
|
||||
return nil, errorx.New("the key:%s is not stringx type", key)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,161 +25,169 @@ func (d *Dao) Set(key *proto.BaseKey, strVal string) (string, error) {
|
|||
result, _ := strValue.Set(strVal)
|
||||
err := d.lru.Add(key, strValue)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
return &proto.SetResponse{Result: result}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) Get(key *proto.BaseKey) (string, error) {
|
||||
func (d *Dao) Get(key *proto.BaseKey) (*proto.GetResponse, error) {
|
||||
val, ok := d.lru.Get(key)
|
||||
if !ok {
|
||||
return "", errorx.NotKeyErr(key.Key)
|
||||
return nil, errorx.NotKeyErr(key.Key)
|
||||
}
|
||||
|
||||
strVal, ok := val.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return "", errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
return strVal.Get(), nil
|
||||
return &proto.GetResponse{Result: strVal.Get()}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) Add(key *proto.BaseKey, renewal int32) (string, error) {
|
||||
func (d *Dao) Add(key *proto.BaseKey, renewal int32) (*proto.AddResponse, error) {
|
||||
value, lruOk := d.lru.Get(key)
|
||||
if !lruOk {
|
||||
val := stringx.NewStringSingle()
|
||||
res, err := val.Add(renewal)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
return nil, nil
|
||||
}
|
||||
d.lru.Add(key, val)
|
||||
return res, nil
|
||||
return &proto.AddResponse{Result: res}, nil
|
||||
}
|
||||
|
||||
strVal, ok := value.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return "", errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
res, err := strVal.Add(renewal)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
return &proto.AddResponse{Result: res}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) Reduce(key *proto.BaseKey, renewal int32) (string, error) {
|
||||
func (d *Dao) Reduce(key *proto.BaseKey, renewal int32) (*proto.ReduceResponse, error) {
|
||||
value, lruOk := d.lru.Get(key)
|
||||
if !lruOk {
|
||||
val := stringx.NewStringSingle()
|
||||
res, err := val.Reduce(renewal)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
return nil, err
|
||||
}
|
||||
d.lru.Add(key, val)
|
||||
return res, nil
|
||||
return &proto.ReduceResponse{Result: res}, nil
|
||||
}
|
||||
|
||||
strVal, ok := value.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return "", errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
res, err := strVal.Reduce(renewal)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
return &proto.ReduceResponse{Result: res}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) SetBit(key *proto.BaseKey, val bool, offer int32) error {
|
||||
func (d *Dao) SetBit(key *proto.BaseKey, val bool, offer int32) (*proto.SetBitResponse, error) {
|
||||
value, lruOk := d.lru.Get(key)
|
||||
if !lruOk {
|
||||
valStr := stringx.NewStringSingle()
|
||||
length := valStr.Setbit(offer, val)
|
||||
d.lru.UpdateLruSize(length)
|
||||
d.lru.Add(key, valStr)
|
||||
return nil
|
||||
return &proto.SetBitResponse{}, nil
|
||||
}
|
||||
|
||||
strVal, ok := value.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
length := strVal.Setbit(offer, val)
|
||||
d.lru.UpdateLruSize(length)
|
||||
return nil
|
||||
return &proto.SetBitResponse{}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) GetBit(key *proto.BaseKey, offer int32) (bool, error) {
|
||||
func (d *Dao) GetBit(key *proto.BaseKey, offer int32) (*proto.GetBitResponse, error) {
|
||||
value, lruOk := d.lru.Get(key)
|
||||
if !lruOk {
|
||||
return false, errorx.NotKeyErr(key.Key)
|
||||
return nil, errorx.NotKeyErr(key.Key)
|
||||
}
|
||||
strVal, ok := value.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return false, errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
return strVal.Getbit(offer)
|
||||
res, err := strVal.Getbit(offer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &proto.GetBitResponse{Val: res}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) GetRange(key *proto.BaseKey, start, end int32) (string, error) {
|
||||
func (d *Dao) GetRange(key *proto.BaseKey, start, end int32) (*proto.GetRangeResponse, error) {
|
||||
value, lruOk := d.lru.Get(key)
|
||||
if !lruOk {
|
||||
return "", errorx.NotKeyErr(key.Key)
|
||||
return nil, errorx.NotKeyErr(key.Key)
|
||||
}
|
||||
strVal, ok := value.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return "", errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
return strVal.Getrange(start, end)
|
||||
res, err := strVal.Getrange(start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &proto.GetRangeResponse{Result: res}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) GetSet(key *proto.BaseKey, value string) (string, error) {
|
||||
func (d *Dao) GetSet(key *proto.BaseKey, value string) (*proto.GetSetResponse, error) {
|
||||
val, ok := d.lru.Get(key)
|
||||
if !ok {
|
||||
return "", errorx.NotKeyErr(key.Key)
|
||||
return nil, errorx.NotKeyErr(key.Key)
|
||||
}
|
||||
|
||||
strVal, ok := val.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return "", errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
oldValue := strVal.Get()
|
||||
|
||||
_, updateLength := strVal.Set(value)
|
||||
d.lru.UpdateLruSize(updateLength)
|
||||
return oldValue, nil
|
||||
return &proto.GetSetResponse{Result: oldValue}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) StrLen(key *proto.BaseKey) (int32, error) {
|
||||
func (d *Dao) StrLen(key *proto.BaseKey) (*proto.StrLenResponse, error) {
|
||||
val, ok := d.lru.Get(key)
|
||||
if !ok {
|
||||
return 0, errorx.NotKeyErr(key.Key)
|
||||
return nil, errorx.NotKeyErr(key.Key)
|
||||
}
|
||||
|
||||
strVal, ok := val.(structure.StringXInterface)
|
||||
if !ok {
|
||||
return 0, errorx.DaoTypeErr("stringx")
|
||||
return nil, errorx.DaoTypeErr("stringx")
|
||||
}
|
||||
|
||||
return int32(strVal.GetLength()), nil
|
||||
return &proto.StrLenResponse{Length: int32(strVal.GetLength())}, nil
|
||||
}
|
||||
|
||||
func (d *Dao) Setnx(key *proto.BaseKey, val string) error {
|
||||
func (d *Dao) Setnx(key *proto.BaseKey, val string) (*proto.SetnxResponse, error) {
|
||||
_, ok := d.lru.Get(key)
|
||||
if ok {
|
||||
return errorx.New("the key already exists")
|
||||
return nil, 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, err
|
||||
}
|
||||
return nil
|
||||
return &proto.SetnxResponse{}, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue