12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package spider_com
- import (
- "errors"
- "fmt"
- "strings"
- "time"
- qu "app.yhyue.com/moapp/jybase/common"
- "github.com/go-redis/redis"
- )
- var redisClient *redis.Client
- var redisClientMap map[string]*redis.Client
- func InitRedisClient(addrs string) {
- //list=192.168.3.207:1679@jytopnet123,href=192.168.3.207:1679
- redisClientMap = map[string]*redis.Client{}
- addrArr := strings.Split(addrs, ",")
- for _, addr := range addrArr {
- saddr := strings.Split(addr, "=")
- if len(saddr) == 2 {
- code := saddr[0]
- url_pass := strings.Split(saddr[1], "@")
- password := ""
- url := url_pass[0]
- if len(url_pass) == 2 {
- password = url_pass[1]
- }
- client := redis.NewClient(&redis.Options{
- Addr: url,
- Password: password,
- DB: 0,
- })
- _, err := client.Ping().Result()
- if err != nil {
- fmt.Println("Redis Init Err:", addr, err)
- continue
- }
- redisClientMap[code] = client
- } else {
- fmt.Println("Redis Init Err,Url Is Wrong")
- }
- }
- }
- func RedisSet(code, key string, val interface{}, timeout int) bool {
- defer qu.Catch()
- client := redisClientMap[code]
- if client == nil {
- fmt.Println("Redis Not Init Set")
- return false
- }
- //err := client.Set(context.Background(), key, val, time.Duration(timeout)*time.Second).Err()
- result, err := client.Set(key, val, time.Duration(timeout)*time.Second).Result()
- if result == "OK" {
- return true
- }
- if err != nil {
- fmt.Println("Set Redis Error:", err)
- }
- return false
- }
- func RedisExist(code, key string) bool {
- defer qu.Catch()
- client := redisClientMap[code]
- if client == nil {
- fmt.Println("Redis Not Init Exist")
- return false
- }
- result, err := client.Exists(key).Result()
- if err != nil {
- fmt.Println("Exists Redis Error:", err, result)
- return false
- }
- return result == 1
- }
- func RedisGet(code, key string) (string, error) {
- defer qu.Catch()
- client := redisClientMap[code]
- if client == nil {
- fmt.Println("Redis Not Init Get")
- return "", errors.New("Redis Not Init")
- }
- result, err := client.Get(key).Result()
- if err != nil {
- //fmt.Println("Get Redis Error:", err, result)
- return "", err
- }
- return result, err
- }
|