Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
bc1ef48e
Commit
bc1ef48e
authored
Oct 23, 2019
by
antirez
Browse files
Modules hooks: FLUSHDB event example.
parent
ed833c9f
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/module.c
View file @
bc1ef48e
...
@@ -5870,6 +5870,8 @@ void moduleFireServerEvent(uint64_t eid, int subid, void *data) {
...
@@ -5870,6 +5870,8 @@ void moduleFireServerEvent(uint64_t eid, int subid, void *data) {
modulePopulateClientInfoStructure
(
&
civ1
,
data
,
modulePopulateClientInfoStructure
(
&
civ1
,
data
,
el
->
event
.
dataver
);
el
->
event
.
dataver
);
moduledata
=
&
civ1
;
moduledata
=
&
civ1
;
}
else
if
(
eid
==
REDISMODULE_EVENT_FLUSHDB
)
{
moduledata
=
data
;
}
}
el
->
callback
(
&
ctx
,
el
->
event
,
subid
,
moduledata
);
el
->
callback
(
&
ctx
,
el
->
event
,
subid
,
moduledata
);
moduleFreeContext
(
&
ctx
);
moduleFreeContext
(
&
ctx
);
...
...
src/modules/hellohook.c
View file @
bc1ef48e
...
@@ -50,6 +50,31 @@ void clientChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub,
...
@@ -50,6 +50,31 @@ void clientChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub,
ci
->
id
,
ci
->
addr
,
ci
->
port
);
ci
->
id
,
ci
->
addr
,
ci
->
port
);
}
}
void
flushdbCallback
(
RedisModuleCtx
*
ctx
,
RedisModuleEvent
e
,
uint64_t
sub
,
void
*
data
)
{
REDISMODULE_NOT_USED
(
ctx
);
REDISMODULE_NOT_USED
(
e
);
RedisModuleFlushInfo
*
fi
=
data
;
if
(
sub
==
REDISMODULE_SUBEVENT_FLUSHDB_START
)
{
if
(
fi
->
dbnum
!=
-
1
)
{
RedisModuleCallReply
*
reply
;
reply
=
RedisModule_Call
(
ctx
,
"DBSIZE"
,
""
);
long
long
numkeys
=
RedisModule_CallReplyInteger
(
reply
);
printf
(
"FLUSHDB event of database %d started (%lld keys in DB)
\n
"
,
fi
->
dbnum
,
numkeys
);
}
else
{
printf
(
"FLUSHALL event started
\n
"
);
}
}
else
{
if
(
fi
->
dbnum
!=
-
1
)
{
printf
(
"FLUSHDB event of database %d ended
\n
"
,
fi
->
dbnum
);
}
else
{
printf
(
"FLUSHALL event ended
\n
"
);
}
}
}
/* This function must be present on each Redis module. It is used in order to
/* This function must be present on each Redis module. It is used in order to
* register the commands into the Redis server. */
* register the commands into the Redis server. */
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
...
@@ -61,5 +86,7 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
...
@@ -61,5 +86,7 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
RedisModule_SubscribeToServerEvent
(
ctx
,
RedisModule_SubscribeToServerEvent
(
ctx
,
RedisModuleEvent_ClientChange
,
clientChangeCallback
);
RedisModuleEvent_ClientChange
,
clientChangeCallback
);
RedisModule_SubscribeToServerEvent
(
ctx
,
RedisModuleEvent_FlushDB
,
flushdbCallback
);
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment