github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/queue/redis/redis.go (about)

     1  package redis
     2  
     3  import (
     4  	rds "github.com/go-redis/redis"
     5  	"github.com/qxnw/lib4go/queue"
     6  	"github.com/qxnw/lib4go/redis"
     7  )
     8  
     9  // redisClient memcache配置文件
    10  type redisClient struct {
    11  	servers []string
    12  	client  *redis.Client
    13  }
    14  
    15  // New 根据配置文件创建一个redis连接
    16  func New(addrs []string, conf string) (m *redisClient, err error) {
    17  	m = &redisClient{servers: addrs}
    18  	m.client, err = redis.NewClientByJSON(conf)
    19  	if err != nil {
    20  		return
    21  	}
    22  	return
    23  }
    24  
    25  // Push 向存于 key 的列表的尾部插入所有指定的值
    26  func (c *redisClient) Push(key string, value string) error {
    27  	_, err := c.client.RPush(key, value).Result()
    28  	return err
    29  }
    30  
    31  // Pop 移除并且返回 key 对应的 list 的第一个元素。
    32  func (c *redisClient) Pop(key string) (string, error) {
    33  	r, err := c.client.LPop(key).Result()
    34  	if err != nil && err == rds.Nil {
    35  		return "", queue.Nil
    36  	}
    37  	return r, err
    38  }
    39  
    40  // Pop 移除并且返回 key 对应的 list 的第一个元素。
    41  func (c *redisClient) Count(key string) (int64, error) {
    42  	return c.client.LLen(key).Result()
    43  }
    44  
    45  // Close 释放资源
    46  func (c *redisClient) Close() error {
    47  	return c.client.Close()
    48  }
    49  
    50  type redisResolver struct {
    51  }
    52  
    53  func (s *redisResolver) Resolve(address []string, conf string) (queue.IQueue, error) {
    54  	return New(address, conf)
    55  }
    56  func init() {
    57  	queue.Register("redis", &redisResolver{})
    58  }