-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttl.go
53 lines (46 loc) · 1.36 KB
/
ttl.go
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
47
48
49
50
51
52
53
package sider
import (
"fmt"
"time"
)
var ttlKeys = make(map[string]int64)
var ttlLists = make(map[string]int64)
func CheckTTL() {
for range time.Tick(1 * time.Second) {
for keyName, timestamp := range ttlKeys {
if time.Now().Unix() > timestamp {
fmt.Printf("Key '%v' expired", keyName)
delete(keys, keyName)
delete(ttlKeys, keyName)
}
}
for listName, timestamp := range ttlLists {
if time.Now().Unix() > timestamp {
fmt.Printf("List '%v' expired", listName)
delete(lists, listName)
delete(ttlLists, listName)
}
}
}
}
// TODO: Refactor "ExpireKey" and "ExpireList" to use only 1 function for expire
func ExpireKey(keyName string, expirationTimestamp int64) (bool, error) {
if !isKey(keyName) {
return false, fmt.Errorf("'%v' is not a valid key", keyName)
}
if expirationTimestamp <= time.Now().Unix() {
return false, fmt.Errorf("timestamp '%v' must be greater than now", expirationTimestamp)
}
ttlKeys[keyName] = expirationTimestamp
return true, nil
}
func ExpireList(listName string, expirationTimestamp int64) (bool, error) {
if !isList(listName) {
return false, fmt.Errorf("'%v' is not a valid list", listName)
}
if expirationTimestamp <= time.Now().Unix() {
return false, fmt.Errorf("timestamp '%v' must be greater than now", expirationTimestamp)
}
ttlLists[listName] = expirationTimestamp
return true, nil
}