-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_worker.go
55 lines (45 loc) · 1.26 KB
/
redis_worker.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
54
55
package main
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
)
type RedisWorker struct {
client *redis.Client
}
func InitRedisWorker() RedisWorker {
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", environment.RedisHost, "6379"),
Password: environment.RedisPass,
DB: 0,
})
return RedisWorker{client: client}
}
func (r RedisWorker) Ping() (response string, error error) {
r.client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", environment.RedisHost, "6379"),
Password: environment.RedisPass,
DB: 0,
})
pong, err := r.client.Ping().Result()
return pong, err
}
func (r RedisWorker) StoreArticles(key string, coll []string) error {
pages, _ := json.Marshal(coll)
return r.client.LPush(key, pages).Err()
}
func (r RedisWorker) StoreArticlesSet(key string, articles []string) {
for i := len(articles) - 1; i >= 0; i-- {
r.client.LPush(key, articles[i]).Err()
}
}
func (r RedisWorker) GetAllArticles(key string) []string {
return r.client.LRange(key, 0, -1).Val()
}
func (r RedisWorker) GetArticleByIndex(key string, index int64) string {
return r.client.LIndex(key, index).Val()
}
func (r RedisWorker) GetArticlesLen(key string) int {
len64 := r.client.LLen(key).Val()
return int(len64)
}