-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcrontab.go
More file actions
46 lines (39 loc) · 1.19 KB
/
Copy pathcrontab.go
File metadata and controls
46 lines (39 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
// iterating keys with pattern `itachi:taglist:*` in redis, and check if the
// key exists, if not, then delete it from the corresponding tag, and finally,
// delete the key itself.
import (
"log"
"time"
"github.com/garyburd/redigo/redis"
)
func expirationWorker() {
conn := redisPool.Get()
defer conn.Close()
for {
// iterating keys
tagListKeys, err := redis.Strings(conn.Do("KEYS", "obito:taglist:*"))
if err != nil {
log.Panicf("failed to iterate with error: %s", err)
}
for _, tagList := range tagListKeys {
uuid := tagList[14:] // "obito:taglist:"
exists, _ := redis.Bool(conn.Do("EXISTS", "obito:uuid:"+uuid))
if !exists {
log.Printf("key(%s) not exist", tagList)
// delete from tag and finally delete itself
joinedTagList, _ := redis.Strings(conn.Do("ZRANGE", tagList, 0, -1))
for _, joinedTag := range joinedTagList {
log.Printf("gonna remove %s in tag %s", uuid, joinedTag)
conn.Do("ZREM", "obito:tag:"+joinedTag, uuid)
}
log.Printf("gonna remove key %s", tagList)
conn.Do("DEL", tagList)
} else {
log.Printf("key(%s) exist", tagList)
}
}
// sleep a while
time.Sleep(time.Minute * time.Duration(10))
}
}