【笔记】《Redis设计与实现》chapter18 发布与订阅

chapter18 发布与订阅

客户端订阅频道。

客户端向频道发送消息, 消息被传递至各个订阅者。

匹配模式

客户端订阅模式。

客户端向频道发送消息, 消息被传递给正在订阅匹配模式的订阅者。

另一个模式被匹配的例子。

18.1 频道的订阅与退订

struct redisServer{
      /* Pubsub */
    // 字典,键为频道,值为链表
    // 链表中保存了所有订阅某个频道的客户端
    // 新客户端总是被添加到链表的表尾
    dict *pubsub_channels;  /* Map channels to list of subscribed clients */
};

订阅频道

每当客户端执行SUBSCRIBE命令订阅某个或某些频道的时候,服务器都会将客户端与被订阅的频道再pubsub_cannels字典进行关联

退订频道

18.2 模式的订阅与退订

struct redisServer{
    // 链表,包含多个 pubsubPattern 结构
    // 记录了所有订阅频道的客户端的信息
    // 新 pubsubPattern 结构总是被添加到表尾
    list *pubsub_patterns;  /* patterns a client is interested in (SUBSCRIBE) */
};

/*
 * 记录订阅模式的结构
 */
typedef struct pubsubPattern {

    // 订阅模式的客户端
    redisClient *client;

    // 被订阅的模式
    robj *pattern;

} pubsubPattern;

订阅模式

退订模式

18.3 发送消息

18.4 查看订阅信息

PUBSB CHANNELS

返回服务器当前被订阅的频道

PUBSUB NUMSUB

PUBSUB NUMSUB [channel-1 channel-2...channel-n] 子命令接受任意多个频道作为输入参数,并返回这些频道的订阅者数量

PUBSUB NUMPAT

PUBSUB NUMPAT 子命令用于返回服务器当前被订阅模式的数量

18.5 重点回顾

image-20201016095118157

chapter19 事务

Redis通过MULTI、EXEC、WATCH等命令来实现事务功能

19.1 事务的实现

事务开始

redis> MULTI
ok

通过切换客户端状态的flag属性的REDIS_MULTI标识来完成

命令入队

事务队列

struct redisServer{
      // 事务状态
    multiState mstate;      /* MULTI/EXEC state */  
};

/*
 * 事务命令
 */
typedef struct multiCmd {

    // 参数
    robj **argv;

    // 参数数量
    int argc;

    // 命令指针
    struct redisCommand *cmd;

} multiCmd;

/*
 * 事务状态
 */
typedef struct multiState {

    // 事务队列,FIFO 顺序
    multiCmd *commands;     /* Array of MULTI commands */

    // 已入队命令计数
    int count;              /* Total number of MULTI commands */
    int minreplicas;        /* MINREPLICAS for synchronous replication */
    time_t minreplicas_timeout; /* MINREPLICAS timeout as unixtime. */
} multiState;

执行事务

image-20201016114628683

posted @ 2021-04-20 11:42  汉森伯逸  阅读(56)  评论(0)    收藏  举报