From 1beafa934ca69eea0ab87b26c6d165cde3d2c9ff Mon Sep 17 00:00:00 2001 From: Yun Date: Wed, 27 Dec 2023 17:11:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=A8=E5=B1=80=E5=8D=95?= =?UTF-8?q?=E6=AC=A1=E5=AE=9A=E6=97=B6=E5=99=A8=E7=9A=84=E5=86=B2=E7=AA=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- once.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/once.go b/once.go index c0e1245..f498b94 100644 --- a/once.go +++ b/once.go @@ -40,7 +40,7 @@ type Callback interface { // uniqueKey: 任务唯一标识 // jobType: 任务类型,用于区分任务 // data: 任务数据 - Worker(uniqueKey string, jobType string, data interface{}) (WorkerCode, time.Duration) + Worker(jobType string, uniqueKey string, data interface{}) (WorkerCode, time.Duration) } var wo *worker = nil @@ -52,15 +52,15 @@ type extendData struct { } // 初始化 -func InitOnce(ctx context.Context, re *redis.Client, w Callback) *worker { +func InitOnce(ctx context.Context, re *redis.Client, jobGlobalName string, jobCallback Callback) *worker { once.Do(func() { wo = &worker{ ctx: ctx, - zsetKey: "timer:once_zsetkey", - listKey: "timer:once_listkey", + zsetKey: "timer:once_zsetkey" + jobGlobalName, + listKey: "timer:once_listkey" + jobGlobalName, redis: re, - worker: w, + worker: jobCallback, } go wo.getTask() go wo.watch() @@ -71,7 +71,7 @@ func InitOnce(ctx context.Context, re *redis.Client, w Callback) *worker { // 添加任务 // 重复插入就代表覆盖 -func (w *worker) Add(uniqueKey string, jobType string, delayTime time.Duration, data interface{}) error { +func (w *worker) Add(jobType string, uniqueKey string, delayTime time.Duration, data interface{}) error { if delayTime.Abs() != delayTime { return fmt.Errorf("时间间隔不能为负数") } @@ -79,7 +79,7 @@ func (w *worker) Add(uniqueKey string, jobType string, delayTime time.Duration, return fmt.Errorf("时间间隔不能为0") } - redisKey := fmt.Sprintf("%s[:]%s", uniqueKey, jobType) + redisKey := fmt.Sprintf("%s[:]%s", jobType, uniqueKey) ed := extendData{ Delay: delayTime, @@ -101,8 +101,8 @@ func (w *worker) Add(uniqueKey string, jobType string, delayTime time.Duration, } // 删除任务 -func (w *worker) Del(uniqueKey string, jobType string) error { - redisKey := fmt.Sprintf("%s[:]%s", uniqueKey, jobType) +func (w *worker) Del(jobType string, uniqueKey string) error { + redisKey := fmt.Sprintf("%s[:]%s", jobType, uniqueKey) w.redis.Del(w.ctx, redisKey).Result()