-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
150 lines (125 loc) · 2.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/redis/go-redis/v9"
)
type RedisConnectionParams struct {
Addr string
Password string
Base int
Domain string
Timeout time.Duration
}
type RedisQueue struct {
Domain string
Key string
Timeout time.Duration
Client *redis.Client
lock sync.Mutex
InProgress int64
}
func (q *RedisQueue) Size(ctx context.Context) int64 {
ctx, cancel := context.WithTimeout(ctx, q.Timeout)
defer cancel()
if val, err := q.Client.LLen(ctx, q.Key).Result(); err == nil {
return val + q.InProgress
}
return q.InProgress
}
func (q *RedisQueue) Put(ctx context.Context, t *Task) {
ctx, cancel := context.WithTimeout(ctx, q.Timeout)
defer cancel()
b, err := json.Marshal(t)
if err != nil {
fmt.Println(err)
return
}
if err := q.Client.RPush(ctx, q.Key, b).Err(); err != nil {
panic(err)
}
}
func (q *RedisQueue) Take(ctx context.Context) (*Task, error) {
ctx, cancel := context.WithTimeout(ctx, q.Timeout)
defer cancel()
q.lock.Lock()
defer q.lock.Unlock()
if val, err := q.Client.LPop(ctx, q.Key).Result(); err != nil {
return nil, err
} else {
q.InProgress++
var task *Task
_ = json.Unmarshal([]byte(val), &task)
return task, nil
}
}
func (q *RedisQueue) TaskDone(ctx context.Context) {
q.lock.Lock()
q.InProgress--
q.lock.Unlock()
}
func NewRedisQueue(p RedisConnectionParams) *RedisQueue {
r := &RedisQueue{
Domain: p.Domain,
Key: fmt.Sprintf("queue:%s", p.Domain),
Timeout: p.Timeout,
Client: redis.NewClient(&redis.Options{
Addr: p.Addr,
Password: p.Password,
DB: p.Base,
WriteTimeout: time.Second * 5,
ReadTimeout: time.Second * 5,
DialTimeout: time.Second * 5,
PoolTimeout: time.Second * 1,
PoolSize: 1000,
}),
}
return r
}
type RedisCache struct {
Domain string
Key string
Timeout time.Duration
TTL time.Duration
Client *redis.Client
}
func (c *RedisCache) Add(ctx context.Context, uri string) {
ctx, cancel := context.WithTimeout(ctx, c.Timeout)
defer cancel()
key := fmt.Sprintf("%s:%s", c.Key, uri)
Log.Info("set", "key", key)
if _, err := c.Client.Set(ctx, key, uri, c.TTL).Result(); err != nil {
panic(err)
}
}
func (c *RedisCache) Exists(ctx context.Context, uri string) bool {
ctx, cancel := context.WithTimeout(ctx, c.Timeout)
defer cancel()
key := fmt.Sprintf("%s:%s", c.Key, uri)
if _, err := c.Client.Get(ctx, key).Result(); err != nil {
return false
}
return true
}
func NewRedisCache(p RedisConnectionParams) *RedisCache {
r := &RedisCache{
Domain: p.Domain,
Key: fmt.Sprintf("cache:%s", p.Domain),
Timeout: p.Timeout,
Client: redis.NewClient(&redis.Options{
Addr: p.Addr,
Password: p.Password,
DB: p.Base,
WriteTimeout: time.Second * 5,
ReadTimeout: time.Second * 5,
DialTimeout: time.Second * 5,
PoolTimeout: time.Second * 1,
PoolSize: 1000,
}),
TTL: 24 * 7 * time.Hour,
}
return r
}