2021-11-19 18:03:42 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2022-12-07 11:38:28 +03:00
|
|
|
"time"
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2022-12-07 11:38:28 +03:00
|
|
|
"go.uber.org/zap"
|
2021-11-19 18:03:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
|
|
Addr: ":6379",
|
|
|
|
})
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
_ = rdb.Set(ctx, "key_with_ttl", "bar", time.Minute).Err()
|
|
|
|
_ = rdb.Set(ctx, "key_without_ttl_1", "", 0).Err()
|
|
|
|
_ = rdb.Set(ctx, "key_without_ttl_2", "", 0).Err()
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
checker := NewKeyChecker(rdb, 100)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
checker.Start(ctx)
|
2021-11-19 18:03:42 +03:00
|
|
|
|
|
|
|
iter := rdb.Scan(ctx, 0, "", 0).Iterator()
|
|
|
|
for iter.Next(ctx) {
|
2022-12-07 11:38:28 +03:00
|
|
|
checker.Add(iter.Val())
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
if err := iter.Err(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
deleted := checker.Stop()
|
|
|
|
fmt.Println("deleted", deleted, "keys", "in", time.Since(start))
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
type KeyChecker struct {
|
|
|
|
rdb *redis.Client
|
|
|
|
batchSize int
|
|
|
|
ch chan string
|
|
|
|
delCh chan string
|
|
|
|
wg sync.WaitGroup
|
|
|
|
deleted int
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
func NewKeyChecker(rdb *redis.Client, batchSize int) *KeyChecker {
|
|
|
|
return &KeyChecker{
|
|
|
|
rdb: rdb,
|
|
|
|
batchSize: batchSize,
|
|
|
|
ch: make(chan string, batchSize),
|
|
|
|
delCh: make(chan string, batchSize),
|
|
|
|
logger: zap.L(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KeyChecker) Add(key string) {
|
|
|
|
c.ch <- key
|
|
|
|
}
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
func (c *KeyChecker) Start(ctx context.Context) {
|
|
|
|
c.wg.Add(1)
|
2021-11-19 18:03:42 +03:00
|
|
|
go func() {
|
2022-12-07 11:38:28 +03:00
|
|
|
defer c.wg.Done()
|
|
|
|
if err := c.del(ctx); err != nil {
|
2021-11-19 18:03:42 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
defer close(c.delCh)
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
keys := make([]string, 0, c.batchSize)
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
for key := range c.ch {
|
|
|
|
keys = append(keys, key)
|
|
|
|
if len(keys) < cap(keys) {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
if err := c.checkKeys(ctx, keys); err != nil {
|
|
|
|
c.logger.Error("checkKeys failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
keys = keys[:0]
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
if len(keys) > 0 {
|
|
|
|
if err := c.checkKeys(ctx, keys); err != nil {
|
|
|
|
c.logger.Error("checkKeys failed", zap.Error(err))
|
|
|
|
}
|
|
|
|
keys = nil
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
2022-12-07 11:38:28 +03:00
|
|
|
}()
|
|
|
|
}
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
func (c *KeyChecker) Stop() int {
|
|
|
|
close(c.ch)
|
|
|
|
c.wg.Wait()
|
|
|
|
return c.deleted
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
func (c *KeyChecker) checkKeys(ctx context.Context, keys []string) error {
|
|
|
|
cmds, err := c.rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
2021-11-19 18:03:42 +03:00
|
|
|
for _, key := range keys {
|
|
|
|
pipe.TTL(ctx, key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-12-07 11:38:28 +03:00
|
|
|
return err
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
for i, cmd := range cmds {
|
|
|
|
d, err := cmd.(*redis.DurationCmd).Result()
|
2021-11-19 18:03:42 +03:00
|
|
|
if err != nil {
|
2022-12-07 11:38:28 +03:00
|
|
|
return err
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
2022-12-07 11:38:28 +03:00
|
|
|
if d == -1 {
|
|
|
|
c.delCh <- keys[i]
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
return nil
|
2021-11-19 18:03:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
func (c *KeyChecker) del(ctx context.Context) error {
|
|
|
|
pipe := c.rdb.Pipeline()
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
for key := range c.delCh {
|
|
|
|
fmt.Printf("deleting %s...\n", key)
|
2021-11-19 18:03:42 +03:00
|
|
|
pipe.Del(ctx, key)
|
2022-12-07 11:38:28 +03:00
|
|
|
c.deleted++
|
2021-11-19 18:03:42 +03:00
|
|
|
|
2022-12-07 11:38:28 +03:00
|
|
|
if pipe.Len() < c.batchSize {
|
2021-11-19 18:03:42 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := pipe.Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := pipe.Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|