wheat-cache/storage/server/single/stringx.go

135 lines
3.2 KiB
Go

package single
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/lru"
"gitee.com/timedb/wheatCache/pkg/proto"
)
func (s *serverSingle) Set(
cxt context.Context,
req *proto.SetRequest,
) (*proto.SetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Set(req.Key.Key, req.Val)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.SetResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) Get(
cxt context.Context,
req *proto.GetRequest,
) (*proto.GetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Get(req.Key.Key)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.GetResponse{
Result: resp.(string),
}, nil
}
func (s serverSingle) Add(
cxt context.Context,
req *proto.AddRequest,
) (*proto.AddResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key.Key, req.Renewal)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.AddResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) Reduce(
cxt context.Context,
req *proto.ReduceRequest,
) (*proto.ReduceResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Add(req.Key.Key, req.Renewal)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.ReduceResponse{
Result: resp.(string),
}, nil
}
func (s *serverSingle) Setbit(
cxt context.Context,
req *proto.SetbitRequest,
) (*proto.SetbitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.Setbit(req.Key.Key, req.Val, req.Offer)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.SetbitResponse{}, nil
}
func (s *serverSingle) Getbit(
cxt context.Context,
req *proto.GetbitRequest,
) (*proto.GetbitResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.GetBit(req.Key.Key, req.Offer)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(cxt, lruEvent)
flag, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.GetbitResponse{
Val: flag.(bool),
}, nil
}