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

218 lines
5.3 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(
ctx context.Context,
req *proto.SetRequest,
) (*proto.SetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Set(req.Key, req.Val)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, 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)
})
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, 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, 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, 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, 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
}
func (s *serverSingle) GetRange(
ctx context.Context,
req *proto.GetRangeRequest,
) (*proto.GetRangeResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Getrange(req.Key, req.Start, req.End)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
flag, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.GetRangeResponse{
Result: flag.(string),
}, nil
}
func (s *serverSingle) GetSet(
ctx context.Context,
req *proto.GetSetRequest,
) (*proto.GetSetResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Getset(req.Key, req.Val)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
result, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.GetSetResponse{
Result: result.(string),
}, nil
}
func (s *serverSingle) StrLen(
ctx context.Context,
req *proto.StrLenRequest,
) (*proto.StrLenResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return s.dao.Strlen(req.Key)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
flag, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.StrLenResponse{
Length: flag.(int32),
}, nil
}
func (s *serverSingle) Setnx(
ctx context.Context,
req *proto.SetnxRequest,
) (*proto.SetnxResponse, error) {
work := event.EventWorkFunc(func() (interface{}, error) {
return nil, s.dao.Setnx(req.Key, req.Val)
})
lruEvent := event.NewEvent(lru.OptionEventName)
lruEvent.InitWaitEvent()
lruEvent.SetValue(lru.WorkFuncEventKey, work)
s.lruProduce.Call(ctx, lruEvent)
_, err := lruEvent.StartWaitEvent(s.timeOut)
if err != nil {
return nil, err
}
return &proto.SetnxResponse{}, nil
}