forked from p93542168/wheat-cache
commit
bbc4c27027
|
@ -6,10 +6,18 @@ type Consumer struct {
|
||||||
driver DriverInterface
|
driver DriverInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Consumer) Receive(ctx context.Context) *Event {
|
func (c *Consumer) Receive(ctx context.Context) *event {
|
||||||
return c.driver.Get()
|
return c.driver.Get()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Consumer) NewEvent(name string) *event {
|
||||||
|
return c.driver.NewEvent(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Consumer) Recovery(e *event) {
|
||||||
|
c.driver.Recovery(e)
|
||||||
|
}
|
||||||
|
|
||||||
func NewConsumer(driver DriverInterface) ConsumerInterface {
|
func NewConsumer(driver DriverInterface) ConsumerInterface {
|
||||||
return &Consumer{
|
return &Consumer{
|
||||||
driver: driver,
|
driver: driver,
|
||||||
|
|
|
@ -4,28 +4,30 @@ import (
|
||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type eventType int8
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultEventState = eventType(iota) //默认情况下的状态
|
defaultEventState = int32(iota) //默认情况下的状态
|
||||||
waitEventState // 等待状态
|
waitEventState // 等待状态
|
||||||
workEventState //工作状态
|
workEventState //工作状态
|
||||||
closeEventState //事件关闭状态
|
closeEventState //事件关闭状态
|
||||||
)
|
)
|
||||||
|
|
||||||
type EventWorkFunc func() (interface{}, error)
|
type EventWorkFunc func() (interface{}, error)
|
||||||
|
|
||||||
type DriverInterface interface {
|
type DriverInterface interface {
|
||||||
Get() *Event
|
Get() *event
|
||||||
Put(event *Event)
|
Put(*event)
|
||||||
GetLength() int
|
GetLength() int
|
||||||
|
NewEvent(string) *event
|
||||||
|
Recovery(*event)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProduceInterface interface {
|
type ProduceInterface interface {
|
||||||
Call(ctx context.Context, event *Event)
|
Call(context.Context, *event)
|
||||||
|
NewEvent(string) *event
|
||||||
|
Recovery(*event)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConsumerInterface interface {
|
type ConsumerInterface interface {
|
||||||
Receive(ctx context.Context) *Event
|
Receive(ctx context.Context) *event
|
||||||
|
Recovery(*event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,139 +1,166 @@
|
||||||
package event
|
package event
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitee.com/timedb/wheatCache/pkg/errorx"
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Active func() ([]string, error) // 事件带函数
|
// 事件 poll 降低 new 对象的频率
|
||||||
|
type eventPoll struct {
|
||||||
|
poll chan *event
|
||||||
|
maxSize int32
|
||||||
|
nowSize *int32
|
||||||
|
}
|
||||||
|
|
||||||
type Event struct {
|
func (e *eventPoll) getEvent() *event {
|
||||||
|
issSize := atomic.LoadInt32(e.nowSize)
|
||||||
|
if issSize < e.maxSize {
|
||||||
|
atomic.AddInt32(e.nowSize, 1)
|
||||||
|
return newEvent()
|
||||||
|
}
|
||||||
|
|
||||||
|
return <-e.poll
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *eventPoll) recovery(rEvent *event) {
|
||||||
|
rEvent.Reset()
|
||||||
|
e.poll <- rEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
func newEventPoll(maxSize int) *eventPoll {
|
||||||
|
return &eventPoll{
|
||||||
|
poll: make(chan *event, maxSize),
|
||||||
|
maxSize: int32(maxSize),
|
||||||
|
nowSize: new(int32),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type event struct {
|
||||||
msgCtx map[string]interface{}
|
msgCtx map[string]interface{}
|
||||||
eventName string
|
eventName string
|
||||||
WorkTime time.Duration // 工作时间
|
|
||||||
msg map[string]string // 消息
|
msg map[string]string // 消息
|
||||||
waitResult chan interface{} // 等待返回
|
waitResult chan interface{} // 等待返回
|
||||||
err error
|
err error
|
||||||
ru sync.RWMutex
|
eventStatus *int32
|
||||||
muClose sync.Mutex //关闭锁
|
ttlManage *time.Timer
|
||||||
eventStatus eventType
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) SetMsg(key string, val string) {
|
func newEvent() *event {
|
||||||
e.ru.Lock()
|
status := defaultEventState
|
||||||
defer e.ru.Unlock()
|
return &event{
|
||||||
|
eventStatus: &status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) Reset() {
|
||||||
|
if e.ttlManage != nil {
|
||||||
|
e.ttlManage.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
e.err = nil
|
||||||
|
|
||||||
|
atomic.SwapInt32(e.eventStatus, defaultEventState)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *event) SetMsg(key string, val string) {
|
||||||
if e.msg == nil {
|
if e.msg == nil {
|
||||||
e.msg = make(map[string]string)
|
e.msg = make(map[string]string)
|
||||||
}
|
}
|
||||||
e.msg[key] = val
|
e.msg[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) GetMsg(key string) string {
|
func (e *event) GetMsg(key string) string {
|
||||||
e.ru.RLock()
|
|
||||||
defer e.ru.RUnlock()
|
|
||||||
return e.msg[key]
|
return e.msg[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) GetEventName() string {
|
func (e *event) GetEventName() string {
|
||||||
return e.eventName
|
return e.eventName
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue 写入 ctx 传递用参数
|
// SetValue 写入 ctx 传递用参数
|
||||||
func (e *Event) SetValue(key string, value interface{}) {
|
func (e *event) SetValue(key string, value interface{}) {
|
||||||
e.ru.Lock()
|
|
||||||
defer e.ru.Unlock()
|
|
||||||
if e.msgCtx == nil {
|
if e.msgCtx == nil {
|
||||||
e.msgCtx = make(map[string]interface{})
|
e.msgCtx = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
e.msgCtx[key] = value
|
e.msgCtx[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) GetValue(key string) (interface{}, bool) {
|
func (e *event) GetValue(key string) (interface{}, bool) {
|
||||||
e.ru.RLock()
|
|
||||||
defer e.ru.RUnlock()
|
|
||||||
val, ok := e.msgCtx[key]
|
val, ok := e.msgCtx[key]
|
||||||
return val, ok
|
return val, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitWaitEvent 初始化 wait event 必须调用才拥有等待特性
|
// InitWaitEvent 初始化 wait event 必须调用才拥有等待特性
|
||||||
func (e *Event) InitWaitEvent() {
|
func (e *event) InitWaitEvent() {
|
||||||
e.muClose.Lock()
|
if e.waitResult == nil || len(e.waitResult) > 0 {
|
||||||
defer e.muClose.Unlock()
|
e.waitResult = make(chan interface{})
|
||||||
e.waitResult = make(chan interface{})
|
}
|
||||||
e.eventStatus = waitEventState
|
|
||||||
|
// 清理残留
|
||||||
|
if e.ttlManage == nil {
|
||||||
|
e.ttlManage = time.NewTimer(0)
|
||||||
|
}
|
||||||
|
e.ttlManage.Stop()
|
||||||
|
if len(e.ttlManage.C) > 0 {
|
||||||
|
<-e.ttlManage.C
|
||||||
|
}
|
||||||
|
|
||||||
|
atomic.CompareAndSwapInt32(e.eventStatus, defaultEventState, waitEventState)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartWaitEvent 开始一个等待任务
|
// StartWaitEvent 开始一个等待任务
|
||||||
func (e *Event) StartWaitEvent(ttl time.Duration) (interface{}, error) {
|
func (e *event) StartWaitEvent(ttl time.Duration) (interface{}, error) {
|
||||||
t := time.NewTimer(ttl)
|
e.ttlManage.Reset(ttl)
|
||||||
select {
|
|
||||||
case <-t.C:
|
for {
|
||||||
e.muClose.Lock()
|
select {
|
||||||
defer e.muClose.Unlock()
|
case <-e.ttlManage.C:
|
||||||
if e.eventStatus == workEventState {
|
if atomic.CompareAndSwapInt32(e.eventStatus, waitEventState, closeEventState) {
|
||||||
return <-e.waitResult, e.err
|
return nil, errorx.TimeOutErr()
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
|
||||||
|
case result := <-e.waitResult:
|
||||||
|
atomic.CompareAndSwapInt32(e.eventStatus, workEventState, closeEventState)
|
||||||
|
return result, e.err
|
||||||
}
|
}
|
||||||
|
|
||||||
e.eventStatus = closeEventState
|
|
||||||
return nil, errorx.TimeOutErr()
|
|
||||||
|
|
||||||
case result := <-e.waitResult:
|
|
||||||
return result, e.err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) ExecWorkAndSendResult(work EventWorkFunc) (interface{}, error) {
|
func (e *event) ExecWorkAndSendResult(work EventWorkFunc) (interface{}, error) {
|
||||||
e.muClose.Lock()
|
if !atomic.CompareAndSwapInt32(e.eventStatus, waitEventState, workEventState) {
|
||||||
defer e.muClose.Unlock()
|
|
||||||
if e.eventStatus != waitEventState {
|
|
||||||
return nil, errorx.New("not wait status, exec err")
|
return nil, errorx.New("not wait status, exec err")
|
||||||
}
|
}
|
||||||
|
|
||||||
e.eventStatus = workEventState
|
|
||||||
|
|
||||||
res, err := work()
|
res, err := work()
|
||||||
e.err = err
|
e.err = err
|
||||||
e.waitResult <- res
|
e.waitResult <- res
|
||||||
|
|
||||||
close(e.waitResult)
|
|
||||||
e.eventStatus = closeEventState
|
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) SetResultErr(err error) {
|
func (e *event) SetResultErr(err error) {
|
||||||
e.muClose.Lock()
|
if !atomic.CompareAndSwapInt32(e.eventStatus, waitEventState, workEventState) {
|
||||||
defer e.muClose.Unlock()
|
|
||||||
if e.eventStatus != waitEventState {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.eventStatus = workEventState
|
|
||||||
e.err = err
|
e.err = err
|
||||||
e.waitResult <- nil
|
e.waitResult <- nil
|
||||||
close(e.waitResult)
|
|
||||||
e.eventStatus = closeEventState
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEvent(eventName string) *Event {
|
|
||||||
return &Event{
|
|
||||||
eventName: eventName,
|
|
||||||
eventStatus: defaultEventState,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
maxQueueSize int
|
maxQueueSize int
|
||||||
queue chan *Event
|
queue chan *event
|
||||||
|
poll *eventPoll
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get 获取驱动
|
// Get 获取驱动
|
||||||
func (d *Driver) Get() *Event {
|
func (d *Driver) Get() *event {
|
||||||
return <-d.queue
|
return <-d.queue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Put(event *Event) {
|
func (d *Driver) Put(event *event) {
|
||||||
d.queue <- event
|
d.queue <- event
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,10 +168,22 @@ func (d *Driver) GetLength() int {
|
||||||
return len(d.queue)
|
return len(d.queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Driver) NewEvent(name string) *event {
|
||||||
|
event := d.poll.getEvent()
|
||||||
|
event.eventName = name
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
// 任何时候回收事件都应该由 最后使用者回收
|
||||||
|
func (d *Driver) Recovery(e *event) {
|
||||||
|
d.poll.recovery(e)
|
||||||
|
}
|
||||||
|
|
||||||
// NewDriver 新建 Driver
|
// NewDriver 新建 Driver
|
||||||
func NewDriver(maxSize int) DriverInterface {
|
func NewDriver(maxSize int) DriverInterface {
|
||||||
return &Driver{
|
return &Driver{
|
||||||
maxQueueSize: maxSize,
|
maxQueueSize: maxSize,
|
||||||
queue: make(chan *Event, maxSize),
|
queue: make(chan *event, maxSize),
|
||||||
|
poll: newEventPoll(maxSize),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,8 @@ package event
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitee.com/timedb/wheatCache/pkg/errorx"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -13,87 +14,76 @@ import (
|
||||||
const testEvent = "1001"
|
const testEvent = "1001"
|
||||||
const waitTestEvent = "1002"
|
const waitTestEvent = "1002"
|
||||||
|
|
||||||
// 简单 非等待响应模式, 使用 event driver
|
// 简单的 单向 event 使用
|
||||||
func TestEvent_DriverEventTest(t *testing.T) {
|
func Test_EventDriver(t *testing.T) {
|
||||||
ctx := context.Background()
|
driver := NewDriver(2000)
|
||||||
driver := NewDriver(500)
|
|
||||||
produce := NewProduce(driver)
|
produce := NewProduce(driver)
|
||||||
consumer := NewConsumer(driver)
|
consumer := NewConsumer(driver)
|
||||||
|
|
||||||
go produceEvent(t, ctx, produce)
|
|
||||||
consumerEvent(t, ctx, consumer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func produceEvent(t *testing.T, ctx context.Context, v ProduceInterface) {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
event := NewEvent(testEvent)
|
|
||||||
event.SetValue("test", i)
|
|
||||||
v.Call(ctx, event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func consumerEvent(t *testing.T, ctx context.Context, v ConsumerInterface) {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
event := v.Receive(ctx)
|
|
||||||
res, ok := event.GetValue("test")
|
|
||||||
require.True(t, ok)
|
|
||||||
fmt.Println(res)
|
|
||||||
require.Equal(t, res, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 响应等待用法
|
|
||||||
func TestEvent_SpanWaitEvent(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
driver := NewDriver(500)
|
|
||||||
produce := NewProduce(driver)
|
|
||||||
consumer := NewConsumer(driver)
|
|
||||||
|
|
||||||
go waitConsumer(t, ctx, consumer)
|
wait := sync.WaitGroup{}
|
||||||
|
wait.Add(30000)
|
||||||
|
|
||||||
waitProduce(t, ctx, produce)
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitProduce(t *testing.T, ctx context.Context, v ProduceInterface) {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
event := NewEvent(waitTestEvent)
|
|
||||||
|
|
||||||
event.InitWaitEvent()
|
|
||||||
event.SetValue("test", i)
|
|
||||||
v.Call(ctx, event) // 推送给 consumer
|
|
||||||
res, err := event.StartWaitEvent(2 * time.Second) // 最多等待 consumer 回复 2s
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, fmt.Sprintf("test:%v", i), res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitConsumer(t *testing.T, ctx context.Context, v ConsumerInterface) {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
event := v.Receive(ctx) // 接受 produce 的 event
|
|
||||||
res, ok := event.GetValue("test")
|
|
||||||
require.True(t, ok)
|
|
||||||
require.Equal(t, res, i)
|
|
||||||
|
|
||||||
// 发送返回值给 produce
|
|
||||||
event.ExecWorkAndSendResult(func() (interface{}, error) {
|
|
||||||
return fmt.Sprintf("test:%v", res), nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEvent_SetResultErr(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
event := NewEvent("dddd")
|
|
||||||
driver := NewDriver(100)
|
|
||||||
produce := NewProduce(driver)
|
|
||||||
consumer := NewConsumer(driver)
|
|
||||||
go func() {
|
go func() {
|
||||||
event := consumer.Receive(ctx)
|
for i := 0; i < 30000; i++ {
|
||||||
event.SetResultErr(errorx.New("err"))
|
event := produce.NewEvent(testEvent)
|
||||||
|
event.SetMsg("k", strconv.Itoa(i))
|
||||||
|
produce.Call(ctx, event)
|
||||||
|
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
event.InitWaitEvent()
|
|
||||||
produce.Call(ctx, event)
|
go func() {
|
||||||
_, err := event.StartWaitEvent(2 * time.Second)
|
for {
|
||||||
fmt.Println(err)
|
event := consumer.Receive(ctx)
|
||||||
require.Error(t, err)
|
fmt.Println(event.GetMsg("k"))
|
||||||
|
consumer.Recovery(event)
|
||||||
|
wait.Done()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
|
|
||||||
|
fmt.Println(*driver.(*Driver).poll.nowSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 双向 event
|
||||||
|
func Test_WaitEventDriver(t *testing.T) {
|
||||||
|
driver := NewDriver(200)
|
||||||
|
produce := NewProduce(driver)
|
||||||
|
consumer := NewConsumer(driver)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
wait := sync.WaitGroup{}
|
||||||
|
wait.Add(300000)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < 300000; i++ {
|
||||||
|
event := produce.NewEvent(testEvent)
|
||||||
|
event.SetMsg("k", strconv.Itoa(i))
|
||||||
|
event.InitWaitEvent()
|
||||||
|
produce.Call(ctx, event)
|
||||||
|
val, err := event.StartWaitEvent(2 * time.Second)
|
||||||
|
require.NoError(t, err)
|
||||||
|
fmt.Println(val)
|
||||||
|
produce.Recovery(event)
|
||||||
|
wait.Done()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
event := consumer.Receive(ctx)
|
||||||
|
event.ExecWorkAndSendResult(func() (interface{}, error) {
|
||||||
|
msg := event.GetMsg("k")
|
||||||
|
return "hello: " + msg, nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
|
|
||||||
|
fmt.Println(*driver.(*Driver).poll.nowSize)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,16 @@ type Produce struct {
|
||||||
driver DriverInterface
|
driver DriverInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Produce) Call(ctx context.Context, event *Event) {
|
func (p *Produce) NewEvent(name string) *event {
|
||||||
p.driver.Put(event)
|
return p.driver.NewEvent(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Produce) Recovery(e *event) {
|
||||||
|
p.driver.Recovery(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Produce) Call(ctx context.Context, e *event) {
|
||||||
|
p.driver.Put(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProduce(driver DriverInterface) ProduceInterface {
|
func NewProduce(driver DriverInterface) ProduceInterface {
|
||||||
|
|
|
@ -61,7 +61,7 @@ func TestNewLRUCache2(t *testing.T) {
|
||||||
|
|
||||||
func TestLruProcess(t *testing.T) {
|
func TestLruProcess(t *testing.T) {
|
||||||
lru := NewLRUCache()
|
lru := NewLRUCache()
|
||||||
lru.clearSize = 1000
|
lru.clearSize = 3600
|
||||||
|
|
||||||
for i := 100; i < 200; i++ {
|
for i := 100; i < 200; i++ {
|
||||||
lru.Add(&proto.BaseKey{
|
lru.Add(&proto.BaseKey{
|
||||||
|
@ -78,16 +78,17 @@ func TestLruProcess(t *testing.T) {
|
||||||
}, stringx.NewStringSingle())
|
}, stringx.NewStringSingle())
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, lru.nowSize, int64(200*8))
|
require.Equal(t, lru.nowSize, int64(200*24))
|
||||||
|
|
||||||
// 自动清理测试
|
// 自动清理测试
|
||||||
|
fmt.Println(lru.clearSize)
|
||||||
|
require.Equal(t, lru.li.Len(), 200)
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
fmt.Println(lru.nowSize)
|
|
||||||
require.Less(t, lru.nowSize, lru.clearSize+1)
|
require.Less(t, lru.nowSize, lru.clearSize+1)
|
||||||
|
|
||||||
// TTL 测试
|
// TTL 测试, 100-200 key 发生自动清理 留下 50-100 共 100(0-100) + 20个 key,5s 后,前 0-100的 key 过期,剩下
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
require.Equal(t, lru.li.Len(), 25)
|
require.Equal(t, lru.li.Len(), 50)
|
||||||
|
|
||||||
// 过期全部的 Key
|
// 过期全部的 Key
|
||||||
for i := 100; i < 200; i++ {
|
for i := 100; i < 200; i++ {
|
||||||
|
|
|
@ -28,9 +28,9 @@ func Test_LruTTl(t *testing.T) {
|
||||||
Key: "990",
|
Key: "990",
|
||||||
Ttl: 10,
|
Ttl: 10,
|
||||||
}, s)
|
}, s)
|
||||||
require.Equal(t, lru.nowSize, int64(16))
|
require.Equal(t, lru.nowSize, int64(48))
|
||||||
|
|
||||||
time.Sleep(4 * time.Second)
|
time.Sleep(4 * time.Second)
|
||||||
require.Equal(t, lru.nowSize, int64(8))
|
require.Equal(t, lru.nowSize, int64(24))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ func TestWorker(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
lru := NewLRUCache()
|
lru := NewLRUCache()
|
||||||
produce := event.NewProduce(lru.GetDriver())
|
produce := event.NewProduce(lru.GetDriver())
|
||||||
workEvent := event.NewEvent(OptionEventName)
|
workEvent := produce.NewEvent(OptionEventName)
|
||||||
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
||||||
v1 := stringx.NewStringSingle()
|
v1 := stringx.NewStringSingle()
|
||||||
key := proto.BaseKey{
|
key := proto.BaseKey{
|
||||||
|
@ -39,7 +39,7 @@ func TestSingleCache_DelToClearSize(t *testing.T) {
|
||||||
produce := event.NewProduce(lru.GetDriver())
|
produce := event.NewProduce(lru.GetDriver())
|
||||||
|
|
||||||
for i := int32(20000); i >= 1; i-- {
|
for i := int32(20000); i >= 1; i-- {
|
||||||
workEvent := event.NewEvent(OptionEventName)
|
workEvent := produce.NewEvent(OptionEventName)
|
||||||
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
|
||||||
v1 := stringx.NewStringSingle()
|
v1 := stringx.NewStringSingle()
|
||||||
key := proto.BaseKey{
|
key := proto.BaseKey{
|
||||||
|
@ -52,6 +52,7 @@ func TestSingleCache_DelToClearSize(t *testing.T) {
|
||||||
workEvent.InitWaitEvent()
|
workEvent.InitWaitEvent()
|
||||||
produce.Call(ctx, workEvent)
|
produce.Call(ctx, workEvent)
|
||||||
workEvent.StartWaitEvent(2 * time.Second)
|
workEvent.StartWaitEvent(2 * time.Second)
|
||||||
|
produce.Recovery(workEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
|
@ -2,10 +2,11 @@ package lru
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitee.com/timedb/wheatCache/pkg/errorx"
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
||||||
"gitee.com/timedb/wheatCache/pkg/event"
|
"gitee.com/timedb/wheatCache/pkg/event"
|
||||||
"gitee.com/timedb/wheatCache/pkg/logx"
|
"gitee.com/timedb/wheatCache/pkg/logx"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (lru *SingleCache) lruSingleWork() {
|
func (lru *SingleCache) lruSingleWork() {
|
||||||
|
@ -27,7 +28,7 @@ func (lru *SingleCache) lruSingleWork() {
|
||||||
case CleanEventName:
|
case CleanEventName:
|
||||||
// 对当前的io数量进行判断
|
// 对当前的io数量进行判断
|
||||||
ioNum := lru.GetDriver().GetLength()
|
ioNum := lru.GetDriver().GetLength()
|
||||||
if ioNum > lru.lruMaxDiverSize*1/2 {
|
if ioNum > lru.lruMaxDiverSize/2 {
|
||||||
lru.lruCleanProduce.Call(ctx, workEvent)
|
lru.lruCleanProduce.Call(ctx, workEvent)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -46,7 +47,6 @@ func (lru *SingleCache) lruSingleWork() {
|
||||||
// 执行过期事件
|
// 执行过期事件
|
||||||
func (lru *SingleCache) lruTtlWork() {
|
func (lru *SingleCache) lruTtlWork() {
|
||||||
|
|
||||||
ttlEvent := event.NewEvent(TtlEventName)
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
work := event.EventWorkFunc(func() (interface{}, error) {
|
work := event.EventWorkFunc(func() (interface{}, error) {
|
||||||
|
|
||||||
|
@ -66,7 +66,6 @@ func (lru *SingleCache) lruTtlWork() {
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
})
|
})
|
||||||
ttlEvent.SetValue(WorkFuncEventKey, work)
|
|
||||||
|
|
||||||
cleanTTlTicker := time.NewTicker(500 * time.Millisecond)
|
cleanTTlTicker := time.NewTicker(500 * time.Millisecond)
|
||||||
defer cleanTTlTicker.Stop()
|
defer cleanTTlTicker.Stop()
|
||||||
|
@ -77,16 +76,22 @@ func (lru *SingleCache) lruTtlWork() {
|
||||||
select {
|
select {
|
||||||
// 清理事件
|
// 清理事件
|
||||||
case <-cleanTTlTicker.C:
|
case <-cleanTTlTicker.C:
|
||||||
|
ttlEvent := lru.lruCleanProduce.NewEvent(TtlEventName)
|
||||||
|
ttlEvent.SetValue(WorkFuncEventKey, work)
|
||||||
|
|
||||||
if len(lru.lruTtlManage.memoryKey) == 0 {
|
if len(lru.lruTtlManage.memoryKey) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ttlEvent.InitWaitEvent()
|
ttlEvent.InitWaitEvent()
|
||||||
lru.lruCleanProduce.Call(ctx, ttlEvent)
|
lru.lruCleanProduce.Call(ctx, ttlEvent)
|
||||||
_, err := ttlEvent.StartWaitEvent(time.Second * 2)
|
_, err := ttlEvent.StartWaitEvent(time.Second * 2)
|
||||||
|
lru.lruCleanProduce.Recovery(ttlEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.With(ctx, lru.middleProduce).Errorln(err)
|
logx.With(ctx, lru.middleProduce).Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 收集过期的 key
|
||||||
case <-gatherTTlTicker.C:
|
case <-gatherTTlTicker.C:
|
||||||
lru.lruTtlManage.ttlKeyToMemoryBySecond()
|
lru.lruTtlManage.ttlKeyToMemoryBySecond()
|
||||||
}
|
}
|
||||||
|
@ -95,24 +100,27 @@ func (lru *SingleCache) lruTtlWork() {
|
||||||
|
|
||||||
func (lru *SingleCache) cleanWork() {
|
func (lru *SingleCache) cleanWork() {
|
||||||
cxt := context.Background()
|
cxt := context.Background()
|
||||||
lruCleanEvent := event.NewEvent(CleanEventName)
|
|
||||||
work := event.EventWorkFunc(func() (interface{}, error) {
|
work := event.EventWorkFunc(func() (interface{}, error) {
|
||||||
err := lru.DelToClearSize()
|
err := lru.DelToClearSize()
|
||||||
return nil, err
|
return nil, err
|
||||||
})
|
})
|
||||||
|
|
||||||
lruCleanEvent.SetValue(WorkFuncEventKey, work)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
if lru.clearSize < lru.nowSize {
|
if lru.clearSize < lru.nowSize {
|
||||||
|
lruCleanEvent := lru.lruCleanProduce.NewEvent(CleanEventName)
|
||||||
|
lruCleanEvent.SetValue(WorkFuncEventKey, work)
|
||||||
|
|
||||||
lruCleanEvent.InitWaitEvent()
|
lruCleanEvent.InitWaitEvent()
|
||||||
lru.lruCleanProduce.Call(cxt, lruCleanEvent)
|
lru.lruCleanProduce.Call(cxt, lruCleanEvent)
|
||||||
_, err := lruCleanEvent.StartWaitEvent(defaultWaitTime)
|
_, err := lruCleanEvent.StartWaitEvent(defaultWaitTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.With(cxt, lru.middleProduce).Errorln(err)
|
logx.With(cxt, lru.middleProduce).Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 归还
|
||||||
|
lru.lruCleanProduce.Recovery(lruCleanEvent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ func SendMiddleMsg(
|
||||||
eventName = PulginsInfosName
|
eventName = PulginsInfosName
|
||||||
}
|
}
|
||||||
|
|
||||||
msgEvent := event.NewEvent(eventName)
|
msgEvent := middleProduce.NewEvent(eventName)
|
||||||
msgEvent.SetValue(MiddleMsgKey, val)
|
msgEvent.SetValue(MiddleMsgKey, val)
|
||||||
middleProduce.Call(ctx, msgEvent)
|
middleProduce.Call(ctx, msgEvent)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -16,6 +16,8 @@ func (m *MiddleWare) startWork() {
|
||||||
workEvent := m.eventConsumer.Receive(ctx)
|
workEvent := m.eventConsumer.Receive(ctx)
|
||||||
plugs := m.plugins[workEvent.GetEventName()]
|
plugs := m.plugins[workEvent.GetEventName()]
|
||||||
msg, ok := workEvent.GetValue(middleMsg.MiddleMsgKey)
|
msg, ok := workEvent.GetValue(middleMsg.MiddleMsgKey)
|
||||||
|
m.eventConsumer.Recovery(workEvent)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
logx.With(ctx, m.eventProduce).Error("get event value err,not key:%s", middleMsg.MiddleMsgKey)
|
logx.With(ctx, m.eventProduce).Error("get event value err,not key:%s", middleMsg.MiddleMsgKey)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -16,11 +16,12 @@ func (s *serverSingle) LIndex(
|
||||||
return s.dao.LINdex(req.Key, req.Index)
|
return s.dao.LINdex(req.Key, req.Index)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -38,11 +39,12 @@ func (s *serverSingle) LLen(
|
||||||
return s.dao.LLen(req.Key)
|
return s.dao.LLen(req.Key)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -60,11 +62,12 @@ func (s *serverSingle) LPop(
|
||||||
return s.dao.LPop(request.Key, request.Count)
|
return s.dao.LPop(request.Key, request.Count)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -82,11 +85,13 @@ func (s *serverSingle) LPush(
|
||||||
return nil, s.dao.LPush(req.Key, req.Values...)
|
return nil, s.dao.LPush(req.Key, req.Values...)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -102,11 +107,13 @@ func (s *serverSingle) LPushX(
|
||||||
return nil, s.dao.LPush(req.Key, req.Values...)
|
return nil, s.dao.LPush(req.Key, req.Values...)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -122,11 +129,13 @@ func (s *serverSingle) LRange(
|
||||||
return s.dao.LRange(req.Key, req.Start, req.End)
|
return s.dao.LRange(req.Key, req.Start, req.End)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -144,11 +153,13 @@ func (s *serverSingle) LRem(
|
||||||
return s.dao.LRemove(req.Key, req.Count, req.Value)
|
return s.dao.LRemove(req.Key, req.Count, req.Value)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -166,11 +177,13 @@ func (s *serverSingle) LSet(
|
||||||
return nil, s.dao.LSet(req.Key, req.Index, req.Value)
|
return nil, s.dao.LSet(req.Key, req.Index, req.Value)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -186,11 +199,13 @@ func (s *serverSingle) RPop(
|
||||||
return s.dao.RPop(req.Key, req.Count)
|
return s.dao.RPop(req.Key, req.Count)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -208,11 +223,13 @@ func (s *serverSingle) LTrim(
|
||||||
return nil, s.dao.LTrim(req.Key, req.Start, req.End)
|
return nil, s.dao.LTrim(req.Key, req.Start, req.End)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -228,11 +245,13 @@ func (s *serverSingle) RPush(
|
||||||
return nil, s.dao.RPush(req.Key, req.Values...)
|
return nil, s.dao.RPush(req.Key, req.Values...)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -248,11 +267,13 @@ func (s *serverSingle) RPushX(
|
||||||
return nil, s.dao.RPushX(req.Key, req.Values...)
|
return nil, s.dao.RPushX(req.Key, req.Values...)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,11 +17,13 @@ func (s *serverSingle) Set(
|
||||||
return s.dao.Set(req.Key, req.Val)
|
return s.dao.Set(req.Key, req.Val)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -39,11 +41,13 @@ func (s *serverSingle) Get(
|
||||||
return s.dao.Get(req.Key)
|
return s.dao.Get(req.Key)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(cxt, lruEvent)
|
s.lruProduce.Call(cxt, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -60,11 +64,13 @@ func (s serverSingle) Add(
|
||||||
return s.dao.Add(req.Key, req.Renewal)
|
return s.dao.Add(req.Key, req.Renewal)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(cxt, lruEvent)
|
s.lruProduce.Call(cxt, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -81,11 +87,13 @@ func (s *serverSingle) Reduce(
|
||||||
return s.dao.Add(req.Key, req.Renewal)
|
return s.dao.Add(req.Key, req.Renewal)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(cxt, lruEvent)
|
s.lruProduce.Call(cxt, lruEvent)
|
||||||
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
resp, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -102,11 +110,13 @@ func (s *serverSingle) SetBit(
|
||||||
return nil, s.dao.Setbit(req.Key, req.Val, req.Offer)
|
return nil, s.dao.Setbit(req.Key, req.Val, req.Offer)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(cxt, lruEvent)
|
s.lruProduce.Call(cxt, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -121,11 +131,13 @@ func (s *serverSingle) GetBit(
|
||||||
return s.dao.GetBit(req.Key, req.Offer)
|
return s.dao.GetBit(req.Key, req.Offer)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(cxt, lruEvent)
|
s.lruProduce.Call(cxt, lruEvent)
|
||||||
flag, err := lruEvent.StartWaitEvent(s.timeOut)
|
flag, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -142,11 +154,13 @@ func (s *serverSingle) GetRange(
|
||||||
return s.dao.Getrange(req.Key, req.Start, req.End)
|
return s.dao.Getrange(req.Key, req.Start, req.End)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
flag, err := lruEvent.StartWaitEvent(s.timeOut)
|
flag, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -163,11 +177,13 @@ func (s *serverSingle) GetSet(
|
||||||
return s.dao.Getset(req.Key, req.Val)
|
return s.dao.Getset(req.Key, req.Val)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
result, err := lruEvent.StartWaitEvent(s.timeOut)
|
result, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -184,11 +200,13 @@ func (s *serverSingle) StrLen(
|
||||||
return s.dao.Strlen(req.Key)
|
return s.dao.Strlen(req.Key)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
flag, err := lruEvent.StartWaitEvent(s.timeOut)
|
flag, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -205,11 +223,13 @@ func (s *serverSingle) Setnx(
|
||||||
return nil, s.dao.Setnx(req.Key, req.Val)
|
return nil, s.dao.Setnx(req.Key, req.Val)
|
||||||
})
|
})
|
||||||
|
|
||||||
lruEvent := event.NewEvent(lru.OptionEventName)
|
lruEvent := s.lruProduce.NewEvent(lru.OptionEventName)
|
||||||
lruEvent.InitWaitEvent()
|
lruEvent.InitWaitEvent()
|
||||||
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
lruEvent.SetValue(lru.WorkFuncEventKey, work)
|
||||||
s.lruProduce.Call(ctx, lruEvent)
|
s.lruProduce.Call(ctx, lruEvent)
|
||||||
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
_, err := lruEvent.StartWaitEvent(s.timeOut)
|
||||||
|
s.lruProduce.Recovery(lruEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue