/* This file implements keyspace events notification via Pub/Sub and * described at https://redis.io/topics/notifications. */ 这个文件通过Pub/Sub实现键空消息通知事件,具体的描述可以参见地址https://redis.io/topics/notifications. /* Turn a string representing notification classes into an integer * representing notification classes flags xored. 将字符串表示的通知类型转化为用整数表示的通知类型 标志的异或 * The function returns -1 if the input contains characters not mapping to * any class. */ 函数返回-1,如果输入的字符串中包含找不到映射到任何类型 int keyspaceEventsStringToFlags(char *classes) { char *p = classes; int c, flags = 0; while((c = *p++) != '\0') { switch(c) { case 'A': flags |= NOTIFY_ALL; break; 全部 case 'g': flags |= NOTIFY_GENERIC; break; 一般情况 case '$': flags |= NOTIFY_STRING; break; 字符串 case 'l': flags |= NOTIFY_LIST; break; 链表 case 's': flags |= NOTIFY_SET; break; 集合 case 'h': flags |= NOTIFY_HASH; break; 哈希 case 'z': flags |= NOTIFY_ZSET; break; 有序集 case 'x': flags |= NOTIFY_EXPIRED; break; 过期 case 'e': flags |= NOTIFY_EVICTED; break; 淘汰 case 'K': flags |= NOTIFY_KEYSPACE; break; 操作键相关事件 case 'E': flags |= NOTIFY_KEYEVENT; break; 键相关事件 case 't': flags |= NOTIFY_STREAM; break; 流 case 'm': flags |= NOTIFY_KEY_MISS; break; 不命中 default: return -1; 不能识别的情况 } } return flags; 返回对应的整数异或表示标志 } /* This function does exactly the revese of the function above: it gets * as input an integer with the xored flags and returns a string representing * the selected classes. The string returned is an sds string that needs to * be released with sdsfree(). */ 这个函数与上面的函数恰好相反:它获取一个带有异或标志的整数作为输入, 并返回一个用用来表示所选类型的字符串。返回的字符串是sds字符串,需要用函数sdsfree释放 sds keyspaceEventsFlagsToString(int flags) { sds res; res = sdsempty(); 创建新sds字符串 if ((flags & NOTIFY_ALL) == NOTIFY_ALL) { 全部事件 res = sdscatlen(res,"A",1); } else { if (flags & NOTIFY_GENERIC) res = sdscatlen(res,"g",1); 一般 if (flags & NOTIFY_STRING) res = sdscatlen(res,"$",1); 字符串 if (flags & NOTIFY_LIST) res = sdscatlen(res,"l",1); 链表 if (flags & NOTIFY_SET) res = sdscatlen(res,"s",1); 集合 if (flags & NOTIFY_HASH) res = sdscatlen(res,"h",1); 哈希 if (flags & NOTIFY_ZSET) res = sdscatlen(res,"z",1); 有序集 if (flags & NOTIFY_EXPIRED) res = sdscatlen(res,"x",1); 过期 if (flags & NOTIFY_EVICTED) res = sdscatlen(res,"e",1); 淘汰 if (flags & NOTIFY_STREAM) res = sdscatlen(res,"t",1); 流 } if (flags & NOTIFY_KEYSPACE) res = sdscatlen(res,"K",1); 操作键事件 if (flags & NOTIFY_KEYEVENT) res = sdscatlen(res,"E",1); 键本身事件 if (flags & NOTIFY_KEY_MISS) res = sdscatlen(res,"m",1); 键不命中 return res; } /* The API provided to the rest of the Redis core is a simple function: 提供给redis数据库核心的其余部分是一个简单的函数: * notifyKeyspaceEvent(char *event, robj *key, int dbid); * * 'event' is a C string representing the event name. event是一个C类型的字符串表示事件名 * 'key' is a Redis object representing the key name. key 是redis对象表示的键名 * 'dbid' is the database ID where the key lives. */ dbid是键所在的数据库ID void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid) { sds chan; robj *chanobj, *eventobj; int len = -1; char buf[24]; /* If any modules are interested in events, notify the module system now. * This bypasses the notifications configuration, but the module engine * will only call event subscribers if the event type matches the types * they are interested in. */ 如果任何模块对事件感兴趣,立刻通知模块系统。 这个绕过了通知事件的配置,但是模块的引擎只调用事件订阅者,当事件类型匹配上了他们(模块)感兴趣的类型 moduleNotifyKeyspaceEvent(type, event, key, dbid); 通知订阅的模块 /* If notifications for this class of events are off, return ASAP. */ 如果为这类时间的通知已经关闭,尽快返回 if (!(server.notify_keyspace_events & type)) return; eventobj = createStringObject(event,strlen(event)); 为事件创建字符串对象 /* __keyspace@<db>__:<key> <event> notifications. */ 格式 if (server.notify_keyspace_events & NOTIFY_KEYSPACE) { 操作键事件 chan = sdsnewlen("__keyspace@",11); len = ll2string(buf,sizeof(buf),dbid); chan = sdscatlen(chan, buf, len); chan = sdscatlen(chan, "__:", 3); 按照上述格式拼接 chan = sdscatsds(chan, key->ptr); 到<key>位置的字符串 chanobj = createObject(OBJ_STRING, chan); pubsubPublishMessage(chanobj, eventobj); 向订阅的客户端传递消息 decrRefCount(chanobj); } /* __keyevent@<db>__:<event> <key> notifications. */ 格式 if (server.notify_keyspace_events & NOTIFY_KEYEVENT) { 键本身事件 chan = sdsnewlen("__keyevent@",11); if (len == -1) len = ll2string(buf,sizeof(buf),dbid); chan = sdscatlen(chan, buf, len); chan = sdscatlen(chan, "__:", 3); chan = sdscatsds(chan, eventobj->ptr); chanobj = createObject(OBJ_STRING, chan); pubsubPublishMessage(chanobj, key); 向订阅的客户端传递消息 decrRefCount(chanobj); } decrRefCount(eventobj); }