-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |