|
@@ -0,0 +1,48 @@
|
|
|
+package spiderutil
|
|
|
+
|
|
|
+import (
|
|
|
+ "log"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/go-redis/redis"
|
|
|
+ "golang.org/x/net/context"
|
|
|
+)
|
|
|
+
|
|
|
+var ctx = context.Background()
|
|
|
+var RedisCluster *redis.ClusterClient
|
|
|
+
|
|
|
+func InitRedisCluster(addrs []string) {
|
|
|
+ opts := &redis.ClusterOptions{
|
|
|
+ Addrs: addrs,
|
|
|
+ DialTimeout: 120 * time.Second,
|
|
|
+ ReadTimeout: 60 * time.Second,
|
|
|
+ WriteTimeout: 60 * time.Second,
|
|
|
+ }
|
|
|
+ RedisCluster = redis.NewClusterClient(opts)
|
|
|
+}
|
|
|
+
|
|
|
+//set
|
|
|
+func RedisClusterSet(key string, val interface{}, timeout int) bool {
|
|
|
+ resullt, err := RedisCluster.Set(ctx, key, val, time.Duration(timeout)*time.Second).Result()
|
|
|
+ if err != nil || resullt != "OK" {
|
|
|
+ log.Println("Redis Cluster Set Error:", resullt, err)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+//get
|
|
|
+func RedisClusterGet(key string) string {
|
|
|
+ resullt, _ := RedisCluster.Get(ctx, key).Result()
|
|
|
+ return resullt
|
|
|
+}
|
|
|
+
|
|
|
+//exists
|
|
|
+func RedisClusterExists(key string) bool {
|
|
|
+ resullt, err := RedisCluster.Exists(ctx, key).Result()
|
|
|
+ if err != nil || resullt != 1 {
|
|
|
+ log.Println("Redis Cluster Exists Error:", resullt, err)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|