Skip to content

Commit

Permalink
Add cacheCleanup.go
Browse files Browse the repository at this point in the history
  • Loading branch information
chubin committed Jan 1, 2022
1 parent e001011 commit 46d1a5f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions share/scripts/cacheCleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

// Remove invalid cache entries.
// Cache entry is invalid, if it contains a special substring.

import (
"context"
"log"
"strings"

"github.com/go-redis/redis"
)

var invalidEntrySubstr = "Unknown cheat sheet"

func removeInvalidEntries() error {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})

ctx := context.Background()
allKeys, err := rdb.Keys(ctx, "*").Result()
if err != nil {
return err
}

var counter int
for _, key := range allKeys {
val, err := rdb.Get(ctx, key).Result()
if err != nil {
return err
}
if strings.Contains(val, invalidEntrySubstr) {
err = rdb.Del(ctx, key).Err()
if err != nil {
return err
}
counter++
}
}
log.Println("invalid entries removed:", counter)
return nil
}

func main() {
err := removeInvalidEntries()
if err != nil {
log.Println(err)
}
}

0 comments on commit 46d1a5f

Please sign in to comment.