Commit e047b6f7 authored by vicotor's avatar vicotor

add subscribe for task update

parent cb54fe8c
...@@ -51,5 +51,6 @@ func NewCacheData(ctx context.Context, options RedisConnParam, mysqlConfig model ...@@ -51,5 +51,6 @@ func NewCacheData(ctx context.Context, options RedisConnParam, mysqlConfig model
for i := 0; i < cache.maxRoutine; i++ { for i := 0; i < cache.maxRoutine; i++ {
go cache.queryRoutine() go cache.queryRoutine()
} }
go cache.SubscribeTaskUpdate()
return cache return cache
} }
package cachedata package cachedata
import ( import (
"context"
"encoding/json" "encoding/json"
"github.com/odysseus/payment/model" "github.com/odysseus/payment/model"
"strconv" "strconv"
...@@ -108,3 +109,21 @@ func (c *CacheData) SetTaskDataToRedis(task *model.TaskType) error { ...@@ -108,3 +109,21 @@ func (c *CacheData) SetTaskDataToRedis(task *model.TaskType) error {
return err return err
} }
} }
const (
TASKUPDATECHANNEL = "task-update"
)
func (c *CacheData) SubscribeTaskUpdate() {
ch := TASKUPDATECHANNEL
sub := c.rdb.Subscribe(context.Background(), ch)
defer sub.Close()
for msg := range sub.Channel() {
taskPath := msg.Payload
c.pathLru.Remove(taskPath)
}
}
func (c *CacheData) PublichTaskUpdate(path string) error {
return c.rdb.Publish(c.ctx, TASKUPDATECHANNEL, path).Err()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment