关键词搜索

源码搜索 ×
×

漫话Redis源码之四十

发布2021-12-26浏览1033次

详情内容

在该文件中,或逻辑用得很巧妙。在我们实际开发中,也经常用到。有点屏蔽位的感觉。

  1. /*
  2. * Copyright (c) 2013, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "server.h"
  30. /* This file implements keyspace events notification via Pub/Sub and
  31. * described at https://redis.io/topics/notifications. */
  32. /* Turn a string representing notification classes into an integer
  33. * representing notification classes flags xored.
  34. *
  35. * The function returns -1 if the input contains characters not mapping to
  36. * any class. */
  37. int keyspaceEventsStringToFlags(char *classes) {
  38. char *p = classes;
  39. int c, flags = 0;
  40. while((c = *p++) != '\0') {
  41. switch(c) {
  42. case 'A': flags |= NOTIFY_ALL; break;
  43. case 'g': flags |= NOTIFY_GENERIC; break;
  44. case '$': flags |= NOTIFY_STRING; break;
  45. case 'l': flags |= NOTIFY_LIST; break;
  46. case 's': flags |= NOTIFY_SET; break;
  47. case 'h': flags |= NOTIFY_HASH; break;
  48. case 'z': flags |= NOTIFY_ZSET; break;
  49. case 'x': flags |= NOTIFY_EXPIRED; break;
  50. case 'e': flags |= NOTIFY_EVICTED; break;
  51. case 'K': flags |= NOTIFY_KEYSPACE; break;
  52. case 'E': flags |= NOTIFY_KEYEVENT; break;
  53. case 't': flags |= NOTIFY_STREAM; break;
  54. case 'm': flags |= NOTIFY_KEY_MISS; break;
  55. case 'd': flags |= NOTIFY_MODULE; break;
  56. default: return -1;
  57. }
  58. }
  59. return flags;
  60. }
  61. /* This function does exactly the reverse of the function above: it gets
  62. * as input an integer with the xored flags and returns a string representing
  63. * the selected classes. The string returned is an sds string that needs to
  64. * be released with sdsfree(). */
  65. sds keyspaceEventsFlagsToString(int flags) {
  66. sds res;
  67. res = sdsempty();
  68. if ((flags & NOTIFY_ALL) == NOTIFY_ALL) {
  69. res = sdscatlen(res,"A",1);
  70. } else {
  71. if (flags & NOTIFY_GENERIC) res = sdscatlen(res,"g",1);
  72. if (flags & NOTIFY_STRING) res = sdscatlen(res,"$",1);
  73. if (flags & NOTIFY_LIST) res = sdscatlen(res,"l",1);
  74. if (flags & NOTIFY_SET) res = sdscatlen(res,"s",1);
  75. if (flags & NOTIFY_HASH) res = sdscatlen(res,"h",1);
  76. if (flags & NOTIFY_ZSET) res = sdscatlen(res,"z",1);
  77. if (flags & NOTIFY_EXPIRED) res = sdscatlen(res,"x",1);
  78. if (flags & NOTIFY_EVICTED) res = sdscatlen(res,"e",1);
  79. if (flags & NOTIFY_STREAM) res = sdscatlen(res,"t",1);
  80. if (flags & NOTIFY_MODULE) res = sdscatlen(res,"d",1);
  81. }
  82. if (flags & NOTIFY_KEYSPACE) res = sdscatlen(res,"K",1);
  83. if (flags & NOTIFY_KEYEVENT) res = sdscatlen(res,"E",1);
  84. if (flags & NOTIFY_KEY_MISS) res = sdscatlen(res,"m",1);
  85. return res;
  86. }
  87. /* The API provided to the rest of the Redis core is a simple function:
  88. *
  89. * notifyKeyspaceEvent(char *event, robj *key, int dbid);
  90. *
  91. * 'event' is a C string representing the event name.
  92. * 'key' is a Redis object representing the key name.
  93. * 'dbid' is the database ID where the key lives. */
  94. void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid) {
  95. sds chan;
  96. robj *chanobj, *eventobj;
  97. int len = -1;
  98. char buf[24];
  99. /* If any modules are interested in events, notify the module system now.
  100. * This bypasses the notifications configuration, but the module engine
  101. * will only call event subscribers if the event type matches the types
  102. * they are interested in. */
  103. moduleNotifyKeyspaceEvent(type, event, key, dbid);
  104. /* If notifications for this class of events are off, return ASAP. */
  105. if (!(server.notify_keyspace_events & type)) return;
  106. eventobj = createStringObject(event,strlen(event));
  107. /* __keyspace@<db>__:<key> <event> notifications. */
  108. if (server.notify_keyspace_events & NOTIFY_KEYSPACE) {
  109. chan = sdsnewlen("__keyspace@",11);
  110. len = ll2string(buf,sizeof(buf),dbid);
  111. chan = sdscatlen(chan, buf, len);
  112. chan = sdscatlen(chan, "__:", 3);
  113. chan = sdscatsds(chan, key->ptr);
  114. chanobj = createObject(OBJ_STRING, chan);
  115. pubsubPublishMessage(chanobj, eventobj);
  116. decrRefCount(chanobj);
  117. }
  118. /* __keyevent@<db>__:<event> <key> notifications. */
  119. if (server.notify_keyspace_events & NOTIFY_KEYEVENT) {
  120. chan = sdsnewlen("__keyevent@",11);
  121. if (len == -1) len = ll2string(buf,sizeof(buf),dbid);
  122. chan = sdscatlen(chan, buf, len);
  123. chan = sdscatlen(chan, "__:", 3);
  124. chan = sdscatsds(chan, eventobj->ptr);
  125. chanobj = createObject(OBJ_STRING, chan);
  126. pubsubPublishMessage(chanobj, key);
  127. decrRefCount(chanobj);
  128. }
  129. decrRefCount(eventobj);
  130. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载