forked from p93542168/wheat-cache
133 lines
2.7 KiB
Go
133 lines
2.7 KiB
Go
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)
|
|
}
|