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
f2324293
Commit
f2324293
authored
Nov 10, 2009
by
antirez
Browse files
Implemented a much better lazy expiring algorithm for EXPIRE
parent
060f6be6
Changes
1
Hide whitespace changes
Inline
Side-by-side
redis.c
View file @
f2324293
...
@@ -940,14 +940,21 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD
...
@@ -940,14 +940,21 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD
}
}
}
}
/* Try to expire a few timed out keys */
/* Try to expire a few timed out keys. The algorithm used is adaptive and
* will use few CPU cycles if there are few expiring keys, otherwise
* it will get more aggressive to avoid that too much memory is used by
* keys that can be removed from the keyspace. */
for
(
j
=
0
;
j
<
server
.
dbnum
;
j
++
)
{
for
(
j
=
0
;
j
<
server
.
dbnum
;
j
++
)
{
int
expired
;
redisDb
*
db
=
server
.
db
+
j
;
redisDb
*
db
=
server
.
db
+
j
;
int
num
=
dictSize
(
db
->
expires
);
if
(
num
)
{
/* Continue to expire if at the end of the cycle more than 25%
* of the keys were expired. */
do
{
int
num
=
dictSize
(
db
->
expires
);
time_t
now
=
time
(
NULL
);
time_t
now
=
time
(
NULL
);
expired
=
0
;
if
(
num
>
REDIS_EXPIRELOOKUPS_PER_CRON
)
if
(
num
>
REDIS_EXPIRELOOKUPS_PER_CRON
)
num
=
REDIS_EXPIRELOOKUPS_PER_CRON
;
num
=
REDIS_EXPIRELOOKUPS_PER_CRON
;
while
(
num
--
)
{
while
(
num
--
)
{
...
@@ -958,9 +965,10 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD
...
@@ -958,9 +965,10 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD
t
=
(
time_t
)
dictGetEntryVal
(
de
);
t
=
(
time_t
)
dictGetEntryVal
(
de
);
if
(
now
>
t
)
{
if
(
now
>
t
)
{
deleteKey
(
db
,
dictGetEntryKey
(
de
));
deleteKey
(
db
,
dictGetEntryKey
(
de
));
expired
++
;
}
}
}
}
}
}
while
(
expired
>
REDIS_EXPIRELOOKUPS_PER_CRON
/
4
);
}
}
/* Check if we should connect to a MASTER */
/* Check if we should connect to a MASTER */
...
...
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