-
Notifications
You must be signed in to change notification settings - Fork 0
/
psubscribe.c
132 lines (107 loc) · 2.6 KB
/
psubscribe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// https://github.com/redis/hiredis/issues/55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "hiredis/hiredis.h"
#include "hiredis/async.h"
#include "hiredis/adapters/libevent.h"
struct event_base *g_base;
unsigned int g_rdb = 0;
int g_keyshift;
static void sig_handler(int signum)
{
switch(signum) {
case SIGPIPE:
fprintf(stderr, "redis went away!\n");
case SIGHUP:
case SIGINT:
case SIGTERM:
case SIGQUIT:
event_base_loopexit(g_base, NULL);
break;
}
}
#ifdef PRINTALL
static void redisEventPrint(redisAsyncContext *rac, void *reply, void *user_data)
{
size_t i;
redisReply *r;
if(reply == NULL) { return; }
r = reply;
if(r->type == REDIS_REPLY_ARRAY) {
for (i = 0; i < r->elements; i++) {
printf("%lu) %s\n", i, r->element[i]->str);
}
}
}
#else
static void keySet(char *key)
{
printf("set %s\n", key);
}
static void keyDel(char *key)
{
printf("del %s\n", key);
}
static void keyExpired(char *key)
{
printf("exp %s\n", key);
}
static void redisEventHandle(redisAsyncContext *rac, void *reply, void *user_data)
{
char *cmd, *key;
redisReply *r;
if(reply == NULL) { return; }
r = reply;
if(r->type == REDIS_REPLY_ARRAY) {
if(r->elements == 4) {
cmd = r->element[3]->str;
key = r->element[2]->str;
if(strcmp(cmd, "set") == 0) { keySet(key+g_keyshift); }
if(strcmp(cmd, "del") == 0) { keyDel(key+g_keyshift); }
if(strcmp(cmd, "expired") == 0) { keyExpired(key+g_keyshift); }
}
}
}
#endif
static void bail(char *s)
{
fprintf(stderr, "%s: <HOST> <PORT> [DB (0-15)]\n", s);
exit(1);
}
int main(int argc, char *argv[])
{
char subscription[64];
redisAsyncContext *rac;
if(argc == 4) {
g_rdb = atoi(argv[3]);
} else if(argc != 3) { bail(argv[0]); }
if(g_rdb > 15) { bail(argv[0]); }
g_keyshift = 15;
if(g_rdb > 9) { g_keyshift++; }
rac = redisAsyncConnect(argv[1], atoi(argv[2]));
if(rac->err) {
// This doesnt seem to work ...
fprintf(stderr, "error: %s\n", rac->errstr);
return 1;
}
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGHUP, sig_handler);
//signal(SIGPIPE, SIG_IGN);
signal(SIGPIPE, sig_handler);
g_base = event_base_new();
redisLibeventAttach(rac, g_base);
redisAsyncCommand(rac, NULL, NULL, "config set notify-keyspace-events KEA");
snprintf(subscription, sizeof(subscription), "PSUBSCRIBE __keyspace@%u__:*", g_rdb);
#ifdef PRINTALL
redisAsyncCommand(rac, redisEventPrint, NULL, subscription);
#else
redisAsyncCommand(rac, redisEventHandle, NULL, subscription);
#endif
event_base_dispatch(g_base);
return 0;
}