Skip to content

Commit 46d1a5f

Browse files
committed
Add cacheCleanup.go
1 parent e001011 commit 46d1a5f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

share/scripts/cacheCleanup.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
// Remove invalid cache entries.
4+
// Cache entry is invalid, if it contains a special substring.
5+
6+
import (
7+
"context"
8+
"log"
9+
"strings"
10+
11+
"github.com/go-redis/redis"
12+
)
13+
14+
var invalidEntrySubstr = "Unknown cheat sheet"
15+
16+
func removeInvalidEntries() error {
17+
rdb := redis.NewClient(&redis.Options{
18+
Addr: "localhost:6379",
19+
Password: "",
20+
DB: 0,
21+
})
22+
23+
ctx := context.Background()
24+
allKeys, err := rdb.Keys(ctx, "*").Result()
25+
if err != nil {
26+
return err
27+
}
28+
29+
var counter int
30+
for _, key := range allKeys {
31+
val, err := rdb.Get(ctx, key).Result()
32+
if err != nil {
33+
return err
34+
}
35+
if strings.Contains(val, invalidEntrySubstr) {
36+
err = rdb.Del(ctx, key).Err()
37+
if err != nil {
38+
return err
39+
}
40+
counter++
41+
}
42+
}
43+
log.Println("invalid entries removed:", counter)
44+
return nil
45+
}
46+
47+
func main() {
48+
err := removeInvalidEntries()
49+
if err != nil {
50+
log.Println(err)
51+
}
52+
}

0 commit comments

Comments
 (0)