wheat-cache/storage/dao/channelx.go

95 lines
2.2 KiB
Go
Raw Normal View History

2021-11-29 00:16:04 +08:00
package dao
import (
"gitee.com/wheat-os/wheatCache/pkg/errorx"
2021-11-29 23:36:53 +08:00
"gitee.com/wheat-os/wheatCache/pkg/event2"
2021-11-29 00:16:04 +08:00
"gitee.com/wheat-os/wheatCache/pkg/proto"
"gitee.com/wheat-os/wheatCache/pkg/structure"
2021-11-29 23:36:53 +08:00
"gitee.com/wheat-os/wheatCache/pkg/structure/channelx"
2021-11-29 00:16:04 +08:00
)
func (d *Dao) CPush(key *proto.BaseKey, Value []string) (interface{}, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
2021-11-29 23:36:53 +08:00
chanVal, ok := val.(structure.ChannelXInterface)
if !ok {
return nil, errorx.DaoTypeErr("channelx")
}
return event2.EventAwaitFunc(func() (interface{}, error) {
var sumUp structure.UpdateLength
for _, v := range Value {
sumUp += chanVal.Push(v)
}
d.lru.UpdateLruSize(sumUp)
return &proto.CPushResponse{}, nil
}), nil
2021-11-29 00:16:04 +08:00
}
2021-11-29 23:36:53 +08:00
func (d *Dao) CPop(key *proto.BaseKey, count int32) (interface{}, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
chanVal, ok := val.(structure.ChannelXInterface)
if !ok {
return nil, errorx.DaoTypeErr("channelx")
}
return event2.EventAwaitFunc(func() (interface{}, error) {
var sumUp structure.UpdateLength
result := make([]string, 0, count)
for i := int32(0); i < count; i++ {
v, up := chanVal.Pop()
sumUp += up
result = append(result, v)
}
d.lru.UpdateLruSize(sumUp)
return &proto.CPopResponse{Result: result}, nil
}), nil
2021-11-29 00:16:04 +08:00
}
func (d *Dao) CMake(key *proto.BaseKey, length int32) (*proto.CMakeResponse, error) {
2021-11-29 23:36:53 +08:00
chanVal := channelx.MakeChannelX(int(length))
err := d.lru.Add(key, chanVal)
if err != nil {
return nil, err
}
return &proto.CMakeResponse{}, nil
2021-11-29 00:16:04 +08:00
}
2021-11-29 23:36:53 +08:00
func (d *Dao) CLen(key *proto.BaseKey) (*proto.CLenResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
chanVal, ok := val.(structure.ChannelXInterface)
if !ok {
return nil, errorx.DaoTypeErr("channelx")
}
return &proto.CLenResponse{Length: int32(chanVal.Length())}, nil
2021-11-29 00:16:04 +08:00
}
2021-11-29 23:36:53 +08:00
func (d *Dao) CClean(key *proto.BaseKey) (*proto.CCleanResponse, error) {
val, ok := d.lru.Get(key)
if !ok {
return nil, errorx.NotKeyErr(key.Key)
}
chanVal, ok := val.(structure.ChannelXInterface)
if !ok {
return nil, errorx.DaoTypeErr("channelx")
}
up := chanVal.Clean()
d.lru.UpdateLruSize(up)
return &proto.CCleanResponse{}, nil
2021-11-29 00:16:04 +08:00
}