/
redis.go
65 lines (52 loc) · 1.12 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
package storage
import (
"encoding/json"
"strings"
"github.com/Agzdjy/proxy-pool/model"
"github.com/go-redis/redis"
)
type Redis struct {
client *redis.Client
}
var _ Storage = &Redis{}
func genKey(protocol string) string {
return "ip_proxy:" + strings.ToLower(protocol)
}
func decodeValue(ipStr string) (ip *model.IP, err error) {
ip = &model.IP{}
err = json.Unmarshal([]byte(ipStr), ip)
if err != nil {
return nil, err
}
return ip, nil
}
func encodeValue(ip *model.IP) string {
b, _ := json.Marshal(ip)
return string(b)
}
func (r *Redis) Save(ip *model.IP) error {
key := genKey(ip.Protocol)
err := r.client.SAdd(key, encodeValue(ip)).Err()
return err
}
func (r *Redis) Del(ip *model.IP) bool {
key := genKey(ip.Protocol)
value := encodeValue(ip)
err := r.client.SRem(key, value).Err()
if err != nil {
return false
}
return true
}
func (r *Redis) RangeOne(protocol string) (ip *model.IP, err error) {
key := genKey(protocol)
val, err := r.client.SRandMember(key).Result()
if err != nil {
return nil, err
}
ip, err = decodeValue(val)
return
}
func (r *Redis) Close() error {
return r.client.Close()
}