feat(storage): add storage opt
This commit is contained in:
parent
a374638758
commit
fe096c9054
|
@ -1,11 +1,13 @@
|
||||||
package single
|
package single
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitee.com/timedb/wheatCache/pkg/event"
|
"gitee.com/timedb/wheatCache/pkg/event"
|
||||||
"gitee.com/timedb/wheatCache/pkg/lru"
|
"gitee.com/timedb/wheatCache/pkg/lru"
|
||||||
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
"gitee.com/timedb/wheatCache/storage/dao"
|
"gitee.com/timedb/wheatCache/storage/dao"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type serverSingle struct {
|
type serverSingle struct {
|
||||||
|
@ -16,7 +18,7 @@ type serverSingle struct {
|
||||||
dao *dao.Dao
|
dao *dao.Dao
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() *serverSingle {
|
func NewServer() proto.CommServerServer {
|
||||||
oneSingleServer.Do(func() {
|
oneSingleServer.Do(func() {
|
||||||
timeOut := viper.GetInt("storage.timeOut")
|
timeOut := viper.GetInt("storage.timeOut")
|
||||||
if timeOut == 0 {
|
if timeOut == 0 {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package single
|
||||||
|
|
||||||
import (
|
import (
|
||||||
context "context"
|
context "context"
|
||||||
|
|
||||||
"gitee.com/timedb/wheatCache/pkg/event"
|
"gitee.com/timedb/wheatCache/pkg/event"
|
||||||
"gitee.com/timedb/wheatCache/pkg/lru"
|
"gitee.com/timedb/wheatCache/pkg/lru"
|
||||||
"gitee.com/timedb/wheatCache/pkg/proto"
|
"gitee.com/timedb/wheatCache/pkg/proto"
|
||||||
|
@ -132,3 +133,85 @@ func (s *serverSingle) Getbit(
|
||||||
Val: flag.(bool),
|
Val: flag.(bool),
|
||||||
}, nil
|
}, 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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue