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
7ab9cba5
Commit
7ab9cba5
authored
Oct 19, 2018
by
zhaozhao.zz
Browse files
do not delete expired keys in KEYS command
parent
3f7bc5a5
Changes
1
Show whitespace changes
Inline
Side-by-side
src/db.c
View file @
7ab9cba5
...
@@ -38,6 +38,8 @@
...
@@ -38,6 +38,8 @@
* C-level DB API
* C-level DB API
*----------------------------------------------------------------------------*/
*----------------------------------------------------------------------------*/
int
keyIsExpired
(
redisDb
*
db
,
robj
*
key
);
/* Update LFU when an object is accessed.
/* Update LFU when an object is accessed.
* Firstly, decrement the counter if the decrement time is reached.
* Firstly, decrement the counter if the decrement time is reached.
* Then logarithmically increment the counter, and update the access time. */
* Then logarithmically increment the counter, and update the access time. */
...
@@ -543,7 +545,7 @@ void keysCommand(client *c) {
...
@@ -543,7 +545,7 @@ void keysCommand(client *c) {
if
(
allkeys
||
stringmatchlen
(
pattern
,
plen
,
key
,
sdslen
(
key
),
0
))
{
if
(
allkeys
||
stringmatchlen
(
pattern
,
plen
,
key
,
sdslen
(
key
),
0
))
{
keyobj
=
createStringObject
(
key
,
sdslen
(
key
));
keyobj
=
createStringObject
(
key
,
sdslen
(
key
));
if
(
expireIfNeed
ed
(
c
->
db
,
keyobj
)
==
0
)
{
if
(
!
keyIsExpir
ed
(
c
->
db
,
keyobj
))
{
addReplyBulk
(
c
,
keyobj
);
addReplyBulk
(
c
,
keyobj
);
numkeys
++
;
numkeys
++
;
}
}
...
@@ -1120,6 +1122,25 @@ void propagateExpire(redisDb *db, robj *key, int lazy) {
...
@@ -1120,6 +1122,25 @@ void propagateExpire(redisDb *db, robj *key, int lazy) {
decrRefCount
(
argv
[
1
]);
decrRefCount
(
argv
[
1
]);
}
}
/* Check if the key is expired. */
int
keyIsExpired
(
redisDb
*
db
,
robj
*
key
)
{
mstime_t
when
=
getExpire
(
db
,
key
);
if
(
when
<
0
)
return
0
;
/* No expire for this key */
/* Don't expire anything while loading. It will be done later. */
if
(
server
.
loading
)
return
0
;
/* If we are in the context of a Lua script, we pretend that time is
* blocked to when the Lua script started. This way a key can expire
* only the first time it is accessed and not in the middle of the
* script execution, making propagation to slaves / AOF consistent.
* See issue #1525 on Github for more information. */
mstime_t
now
=
server
.
lua_caller
?
server
.
lua_time_start
:
mstime
();
return
now
>
when
;
}
/* This function is called when we are going to perform some operation
/* This function is called when we are going to perform some operation
* in a given key, but such key may be already logically expired even if
* in a given key, but such key may be already logically expired even if
* it still exists in the database. The main way this function is called
* it still exists in the database. The main way this function is called
...
@@ -1140,21 +1161,9 @@ void propagateExpire(redisDb *db, robj *key, int lazy) {
...
@@ -1140,21 +1161,9 @@ void propagateExpire(redisDb *db, robj *key, int lazy) {
* The return value of the function is 0 if the key is still valid,
* The return value of the function is 0 if the key is still valid,
* otherwise the function returns 1 if the key is expired. */
* otherwise the function returns 1 if the key is expired. */
int
expireIfNeeded
(
redisDb
*
db
,
robj
*
key
)
{
int
expireIfNeeded
(
redisDb
*
db
,
robj
*
key
)
{
mstime_t
when
=
getExpire
(
db
,
key
);
if
(
!
keyIsExpired
(
db
,
key
))
{
mstime_t
now
;
return
0
;
}
else
if
(
server
.
masterhost
!=
NULL
)
{
if
(
when
<
0
)
return
0
;
/* No expire for this key */
/* Don't expire anything while loading. It will be done later. */
if
(
server
.
loading
)
return
0
;
/* If we are in the context of a Lua script, we pretend that time is
* blocked to when the Lua script started. This way a key can expire
* only the first time it is accessed and not in the middle of the
* script execution, making propagation to slaves / AOF consistent.
* See issue #1525 on Github for more information. */
now
=
server
.
lua_caller
?
server
.
lua_time_start
:
mstime
();
/* If we are running in the context of a slave, return ASAP:
/* If we are running in the context of a slave, return ASAP:
* the slave key expiration is controlled by the master that will
* the slave key expiration is controlled by the master that will
* send us synthesized DEL operations for expired keys.
* send us synthesized DEL operations for expired keys.
...
@@ -1162,10 +1171,8 @@ int expireIfNeeded(redisDb *db, robj *key) {
...
@@ -1162,10 +1171,8 @@ int expireIfNeeded(redisDb *db, robj *key) {
* Still we try to return the right information to the caller,
* Still we try to return the right information to the caller,
* that is, 0 if we think the key should be still valid, 1 if
* that is, 0 if we think the key should be still valid, 1 if
* we think the key is expired at this time. */
* we think the key is expired at this time. */
if
(
server
.
masterhost
!=
NULL
)
return
now
>
when
;
return
1
;
}
/* Return when this key has not expired */
if
(
now
<=
when
)
return
0
;
/* Delete the key */
/* Delete the key */
server
.
stat_expiredkeys
++
;
server
.
stat_expiredkeys
++
;
...
...
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