feat(dao): add dao server

This commit is contained in:
bandl 2021-10-05 16:40:29 +08:00
parent 67da63ac03
commit 125b137ef6
2 changed files with 133 additions and 0 deletions

132
storage/dao/dao.go Normal file
View File

@ -0,0 +1,132 @@
package dao
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
)
type Dao struct {
lru lru.CacheInterface
}
func NewDao(lru lru.CacheInterface) *Dao {
return &Dao{
lru: lru,
}
}
func (d *Dao) Set(key string, strVal string) (string, 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
} else {
return "", errorx.New("the key:%s is not stringx type", key)
}
}
// 不存在新建
strValue := stringx.NewStringSingle()
result, length := strValue.Set(strVal)
d.lru.Add(key, strValue)
d.lru.UpdateLruSize(length)
return result, nil
}
func (d *Dao) Get(key string) (string, error) {
val, ok := d.lru.Get(key)
if !ok {
return "", errorx.NotKeyErr(key)
}
strVal, ok := val.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
return strVal.Get(), nil
}
func (d *Dao) Add(key string, renewal int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
res, err := val.Add(renewal)
if err != nil {
return "", nil
}
d.lru.Add(key, val)
return res, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
res, err := strVal.Add(renewal)
if err != nil {
return "", err
}
return res, nil
}
func (d *Dao) Reduce(key string, renewal int32) (string, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
val := stringx.NewStringSingle()
res, err := val.Reduce(renewal)
if err != nil {
return "", nil
}
d.lru.Add(key, val)
return res, nil
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return "", errorx.DaoTypeErr("stringx")
}
res, err := strVal.Reduce(renewal)
if err != nil {
return "", err
}
return res, nil
}
func (d *Dao) Setbit(key string, val bool, offer int32) 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
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return errorx.DaoTypeErr("stringx")
}
length := strVal.Setbit(offer, val)
d.lru.UpdateLruSize(length)
return nil
}
func (d *Dao) GetBit(key string, offer int32) (bool, error) {
value, lruOk := d.lru.Get(key)
if !lruOk {
return false, errorx.NotKeyErr(key)
}
strVal, ok := value.(structure.StringXInterface)
if !ok {
return false, errorx.DaoTypeErr("stringx")
}
return strVal.Getbit(offer)
}

1
storage/dao/define.go Normal file
View File

@ -0,0 +1 @@
package dao