github.com/Anderson-Lu/gobox@v0.0.0-20191127065433-3e6c4c2da420/database/redis/redis_helper.go (about)

     1  package redis
     2  
     3  import (
     4  	"time"
     5  
     6  	redis "gopkg.in/redis.v4"
     7  )
     8  
     9  type RedisClient struct {
    10  	client *redis.Client
    11  }
    12  
    13  func NewRedisClient(addr, pwd string) *RedisClient {
    14  	option := redis.Options{
    15  		Addr: addr,
    16  	}
    17  	if pwd != "" {
    18  		option.Password = pwd
    19  	}
    20  	client := redis.NewClient(&option)
    21  	return &RedisClient{
    22  		client: client,
    23  	}
    24  }
    25  
    26  func (r *RedisClient) Get(key string) (string, error) {
    27  	strCmd := r.client.Get(key)
    28  	return strCmd.Val(), strCmd.Err()
    29  }
    30  
    31  func (r *RedisClient) Set(key string, value string, duration time.Duration) {
    32  	r.client.Set(key, value, duration)
    33  }