redis/example/del-keys-without-ttl/main.go

156 lines
2.7 KiB
Go
Raw Normal View History

package main
import (
"context"
"fmt"
"sync"
2022-12-07 11:38:28 +03:00
"time"
2022-12-07 11:38:28 +03:00
"go.uber.org/zap"
"github.com/redis/go-redis/v9"
)
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()
2022-12-07 11:38:28 +03:00
checker := NewKeyChecker(rdb, 100)
start := time.Now()
checker.Start(ctx)
iter := rdb.Scan(ctx, 0, "", 0).Iterator()
for iter.Next(ctx) {
2022-12-07 11:38:28 +03:00
checker.Add(iter.Val())
}
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))
}
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
}
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
}
2022-12-07 11:38:28 +03:00
func (c *KeyChecker) Start(ctx context.Context) {
c.wg.Add(1)
go func() {
2022-12-07 11:38:28 +03:00
defer c.wg.Done()
if err := c.del(ctx); err != nil {
panic(err)
}
}()
2022-12-07 11:38:28 +03:00
c.wg.Add(1)
go func() {
defer c.wg.Done()
defer close(c.delCh)
2022-12-07 11:38:28 +03:00
keys := make([]string, 0, c.batchSize)
2022-12-07 11:38:28 +03:00
for key := range c.ch {
keys = append(keys, key)
if len(keys) < cap(keys) {
continue
}
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]
}
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
}
2022-12-07 11:38:28 +03:00
}()
}
2022-12-07 11:38:28 +03:00
func (c *KeyChecker) Stop() int {
close(c.ch)
c.wg.Wait()
return c.deleted
}
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 {
for _, key := range keys {
pipe.TTL(ctx, key)
}
return nil
})
if err != nil {
2022-12-07 11:38:28 +03:00
return err
}
2022-12-07 11:38:28 +03:00
for i, cmd := range cmds {
d, err := cmd.(*redis.DurationCmd).Result()
if err != nil {
2022-12-07 11:38:28 +03:00
return err
}
2022-12-07 11:38:28 +03:00
if d == -1 {
c.delCh <- keys[i]
}
}
2022-12-07 11:38:28 +03:00
return nil
}
2022-12-07 11:38:28 +03:00
func (c *KeyChecker) del(ctx context.Context) error {
pipe := c.rdb.Pipeline()
2022-12-07 11:38:28 +03:00
for key := range c.delCh {
fmt.Printf("deleting %s...\n", key)
pipe.Del(ctx, key)
2022-12-07 11:38:28 +03:00
c.deleted++
2022-12-07 11:38:28 +03:00
if pipe.Len() < c.batchSize {
continue
}
if _, err := pipe.Exec(ctx); err != nil {
return err
}
}
if _, err := pipe.Exec(ctx); err != nil {
return err
}
return nil
}