go使用go-redis操作redis(三)连接类型,pipline, 发布订阅

本文介绍了如何使用Go的go-redis库进行Redis操作,包括创建普通模式、主从模式和哨兵模式的客户端,重点讲解了连接(Conn)、Pipeline和发布订阅(PubSub)的使用方法,并提供了相关示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内容:

一 . 客户端Client(普通模式,主从模式,哨兵模式)
二. conn连接(连接, pipline, 发布订阅等)
三. 示例程序(连接, pipline, 发布订阅等)

客户端
  1. Client 普通模式的客户端

    go redis依据用途提供了多种客户端创建的函数, 如下:

    func NewClient(opt *Options) *Client
    func NewFailoverClient(failoverOpt *FailoverOptions) *Client
    func (c *Client) Context() context.Context
    func (c *Client) Do(args ...interface{}) *Cmd
    func (c *Client) DoContext(ctx context.Context, args ...interface{}) *Cmd
    func (c *Client) Options() *Options
    func (c *Client) PSubscribe(channels ...string) *PubSub
    func (c *Client) Pipeline() Pipeliner
    func (c *Client) Pipelined(fn func(Pipeliner) error) ([]Cmder, error)
    func (c *Client) PoolStats() *PoolStats
    func (c *Client) Process(cmd Cmder) error
    func (c *Client) ProcessContext(ctx context.Context, cmd Cmder) error
    func (c *Client) SetLimiter(l Limiter) *Client
    func (c *Client) Subscribe(channels ...string) *PubSub
    func (c *Client) TxPipeline() Pipeliner
    func (c *Client) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error)
    func (c *Client) Watch(fn func(*Tx) error, keys ...string) error
    func (c *Client) WithContext(ctx context.Context) *Client
    

    NewClient 创建一个普通连接

    NewFailoverClient 具有故障检测以及故障转移的client

    PSubscribe / Subscribe 发布订阅模式的client

    Pipeline 启用pipline管道模式的client

    PoolStats 连接池状态

    Close 关闭连接

  2. 集群模式的ClusterClient
    func NewClusterClient(opt *ClusterOptions) *ClusterClient
    func (c *ClusterClient) Close() error
    func (c *ClusterClient) Context() context.Context
    func (c *ClusterClient) DBSize() *IntCmd
    func (c *ClusterClient) Do(args ...interface{}) *Cmd
    func (c *ClusterClient) DoContext(ctx context.Context, args ...interface{}) *Cmd
    func (c *ClusterClient) ForEachMaster(fn func(client *Client) error) error
    func (c *ClusterClient) ForEachNode(fn func(client *Client) error) error
    func (c *ClusterClient) ForEachSlave(fn func(client *Client) error) error
    func (c *ClusterClient) Options() *ClusterOptions
    func (c *ClusterClient) PSubscribe(channels ...string) *PubSub
    func (c *ClusterClient) Pipeline() Pipeliner
    func (c *ClusterClient) Pipelined(fn func(Pipeliner) error) ([]Cmder, error)
    func (c *ClusterClient) PoolStats() *PoolStats
    func (c *ClusterClient) Process(cmd Cmder) error
    func (c *ClusterClient) ProcessContext(ctx context.Context, cmd Cmder) error
    func (c *ClusterClient) ReloadState() error
    func (c *ClusterClient) Subscribe(channels ...string) *PubSub
    func (c *ClusterClient) TxPipeline() Pipeliner
    func (c *ClusterClient) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error)
    func (c *ClusterClient) Watch(fn func(*Tx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值