-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
50 lines (39 loc) · 1.17 KB
/
redis.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
package main
import (
"log"
"gopkg.in/redis.v4"
)
const redisXMLListName = "NEWS_XML"
const redisKeySetName = "NEWS_KEYS"
var rd = initRedis()
// initRedis connects to our redis instance and return a connection handler
func initRedis() *redis.Client {
redisHost := "localhost"
redisPort := "6379"
client := redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort,
Password: "",
DB: 0,
})
if _, err := client.Ping().Result(); err != nil {
log.Fatal(err)
}
return client
}
// storeNewsPost stores the string representation of the XML news item in the
// list NEWS_XML in redis. We use RPUSH to add to the end of the list
func storeNewsPost(post string) {
rd.RPush(redisXMLListName, post)
}
// storeNewsKey adds the unixEpoch from the file name to the key SET in redis
func storeNewsKey(key string) {
rd.SAdd(redisKeySetName, key)
}
// isKeyPresent checks to see if the unixEpoch from the archive file name is
// available in the SET redisKeySetName in redis. Returns true of the key is
// there, false if not
func isKeyPresent(key string) bool {
scan := rd.SScan(redisKeySetName, 0, key, 0)
keys, _, _ := scan.ScanCmd.Result()
return len(keys) > 0
}