package global import ( "errors" "github.com/garyburd/redigo/redis" ) // RedisClient Redis连接对象 type RedisClient struct { Client redis.Conn } // GetData 获取数据 func (r *RedisClient) GetData(key string) (string, error) { return redis.String(r.Client.Do("get", key)) } // GetKeys 获取key列表 func (r *RedisClient) GetKeys(key string) ([]string, error) { return redis.Strings(r.Client.Do("keys", key)) } // ExistKey 判断是否存在key func (r *RedisClient) ExistKey(key string) bool { keys, err := redis.Strings(r.Client.Do("keys", key)) if err != nil { return false } return len(keys) > 0 } // Set 保存数据 func (r *RedisClient) Set(key string, value string) error { _, err := r.Client.Do("set", key, value) if err != nil { return errors.New("Redis保存数据失败") } return nil } // SetWithTimeout 保存带过期时间的数据(单位:秒) func (r *RedisClient) SetWithTimeout(key string, value string, timeout string) error { _, err := r.Client.Do("set", key, value, "EX", timeout) if err != nil { return errors.New("Redis保存数据失败") } return nil } // Del 根据key删除Redis数据 func (r *RedisClient) Del(key string) error { _, err := r.Client.Do("DEL", key) if err != nil { return err } return nil }