package export import ( "context" "fmt" "github.com/go-redis/redis/v8" "time" ) type RedisPart struct { ctx context.Context client *redis.Client redisKey string } func NewRedisPart(ctx context.Context, client *redis.Client, redisKey string) *RedisPart { return &RedisPart{ ctx: ctx, client: client, redisKey: redisKey, } } func (s *RedisPart) FinishPart(spn int, fn ...PartDone) TaskOption { return UseFinishPart(spn, func(spn, nfp float64) { s.client.HSet(s.ctx, s.redisKey, "schedule", fmt.Sprintf("%.1f", nfp/spn)) s.client.Expire(s.ctx, s.redisKey, 2*time.Minute) for _, f := range fn { f(spn, nfp) } }) } func (s *RedisPart) Schedule() string { return s.client.HGet(s.ctx, s.redisKey, "schedule").Val() } func (s *RedisPart) Close() error { return s.client.Del(s.ctx, s.redisKey).Err() }