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
94754ccc
Commit
94754ccc
authored
Apr 01, 2009
by
antirez
Browse files
non-lazy expired keys purging implemented
parent
98c541c5
Changes
1
Hide whitespace changes
Inline
Side-by-side
redis.c
View file @
94754ccc
...
@@ -71,6 +71,7 @@
...
@@ -71,6 +71,7 @@
#define REDIS_CONFIGLINE_MAX 1024
#define REDIS_CONFIGLINE_MAX 1024
#define REDIS_OBJFREELIST_MAX 1000000
/* Max number of objects to cache */
#define REDIS_OBJFREELIST_MAX 1000000
/* Max number of objects to cache */
#define REDIS_MAX_SYNC_TIME 60
/* Slave can't take more to sync */
#define REDIS_MAX_SYNC_TIME 60
/* Slave can't take more to sync */
#define REDIS_EXPIRELOOKUPS_PER_CRON 100
/* try to expire 100 keys/second */
/* Hash table parameters */
/* Hash table parameters */
#define REDIS_HT_MINFILL 10
/* Minimal hash table fill 10% */
#define REDIS_HT_MINFILL 10
/* Minimal hash table fill 10% */
...
@@ -285,6 +286,7 @@ static robj *tryObjectSharing(robj *o);
...
@@ -285,6 +286,7 @@ static robj *tryObjectSharing(robj *o);
static
int
removeExpire
(
redisDb
*
db
,
robj
*
key
);
static
int
removeExpire
(
redisDb
*
db
,
robj
*
key
);
static
int
expireIfNeeded
(
redisDb
*
db
,
robj
*
key
);
static
int
expireIfNeeded
(
redisDb
*
db
,
robj
*
key
);
static
int
deleteIfVolatile
(
redisDb
*
db
,
robj
*
key
);
static
int
deleteIfVolatile
(
redisDb
*
db
,
robj
*
key
);
static
int
deleteKey
(
redisDb
*
db
,
robj
*
key
);
static
void
authCommand
(
redisClient
*
c
);
static
void
authCommand
(
redisClient
*
c
);
static
void
pingCommand
(
redisClient
*
c
);
static
void
pingCommand
(
redisClient
*
c
);
...
@@ -628,7 +630,7 @@ void closeTimedoutClients(void) {
...
@@ -628,7 +630,7 @@ void closeTimedoutClients(void) {
}
}
int
serverCron
(
struct
aeEventLoop
*
eventLoop
,
long
long
id
,
void
*
clientData
)
{
int
serverCron
(
struct
aeEventLoop
*
eventLoop
,
long
long
id
,
void
*
clientData
)
{
int
j
,
size
,
used
,
loops
=
server
.
cronloops
++
;
int
j
,
loops
=
server
.
cronloops
++
;
REDIS_NOTUSED
(
eventLoop
);
REDIS_NOTUSED
(
eventLoop
);
REDIS_NOTUSED
(
id
);
REDIS_NOTUSED
(
id
);
REDIS_NOTUSED
(
clientData
);
REDIS_NOTUSED
(
clientData
);
...
@@ -639,10 +641,13 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
...
@@ -639,10 +641,13 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
/* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
/* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
* we resize the hash table to save memory */
* we resize the hash table to save memory */
for
(
j
=
0
;
j
<
server
.
dbnum
;
j
++
)
{
for
(
j
=
0
;
j
<
server
.
dbnum
;
j
++
)
{
int
size
,
used
,
vkeys
;
size
=
dictSlots
(
server
.
db
[
j
].
dict
);
size
=
dictSlots
(
server
.
db
[
j
].
dict
);
used
=
dictSize
(
server
.
db
[
j
].
dict
);
used
=
dictSize
(
server
.
db
[
j
].
dict
);
vkeys
=
dictSize
(
server
.
db
[
j
].
expires
);
if
(
!
(
loops
%
5
)
&&
used
>
0
)
{
if
(
!
(
loops
%
5
)
&&
used
>
0
)
{
redisLog
(
REDIS_DEBUG
,
"DB %d: %d keys in %d slots HT."
,
j
,
used
,
size
);
redisLog
(
REDIS_DEBUG
,
"DB %d: %d keys
(%d volatile)
in %d slots HT."
,
j
,
used
,
vkeys
,
size
);
/* dictPrintStats(server.dict); */
/* dictPrintStats(server.dict); */
}
}
if
(
size
&&
used
&&
size
>
REDIS_HT_MINSLOTS
&&
if
(
size
&&
used
&&
size
>
REDIS_HT_MINSLOTS
&&
...
@@ -698,6 +703,30 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
...
@@ -698,6 +703,30 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
}
}
}
}
}
}
/* Try to expire a few timed out keys */
for
(
j
=
0
;
j
<
server
.
dbnum
;
j
++
)
{
redisDb
*
db
=
server
.
db
+
j
;
int
num
=
dictSize
(
db
->
expires
);
if
(
num
)
{
time_t
now
=
time
(
NULL
);
if
(
num
>
REDIS_EXPIRELOOKUPS_PER_CRON
)
num
=
REDIS_EXPIRELOOKUPS_PER_CRON
;
while
(
num
--
)
{
dictEntry
*
de
;
time_t
t
;
if
((
de
=
dictGetRandomKey
(
db
->
expires
))
==
NULL
)
break
;
t
=
(
time_t
)
dictGetEntryVal
(
de
);
if
(
now
>
t
)
{
deleteKey
(
db
,
dictGetEntryKey
(
de
));
}
}
}
}
/* Check if we should connect to a MASTER */
/* Check if we should connect to a MASTER */
if
(
server
.
replstate
==
REDIS_REPL_CONNECT
)
{
if
(
server
.
replstate
==
REDIS_REPL_CONNECT
)
{
redisLog
(
REDIS_NOTICE
,
"Connecting to MASTER..."
);
redisLog
(
REDIS_NOTICE
,
"Connecting to MASTER..."
);
...
@@ -1446,10 +1475,19 @@ static void freeHashObject(robj *o) {
...
@@ -1446,10 +1475,19 @@ static void freeHashObject(robj *o) {
static
void
incrRefCount
(
robj
*
o
)
{
static
void
incrRefCount
(
robj
*
o
)
{
o
->
refcount
++
;
o
->
refcount
++
;
#ifdef DEBUG_REFCOUNT
if
(
o
->
type
==
REDIS_STRING
)
printf
(
"Increment '%s'(%p), now is: %d
\n
"
,
o
->
ptr
,
o
,
o
->
refcount
);
#endif
}
}
static
void
decrRefCount
(
void
*
obj
)
{
static
void
decrRefCount
(
void
*
obj
)
{
robj
*
o
=
obj
;
robj
*
o
=
obj
;
#ifdef DEBUG_REFCOUNT
if
(
o
->
type
==
REDIS_STRING
)
printf
(
"Decrement '%s'(%p), now is: %d
\n
"
,
o
->
ptr
,
o
,
o
->
refcount
-
1
);
#endif
if
(
--
(
o
->
refcount
)
==
0
)
{
if
(
--
(
o
->
refcount
)
==
0
)
{
switch
(
o
->
type
)
{
switch
(
o
->
type
)
{
case
REDIS_STRING
:
freeStringObject
(
o
);
break
;
case
REDIS_STRING
:
freeStringObject
(
o
);
break
;
...
@@ -1525,8 +1563,18 @@ static robj *lookupKeyWrite(redisDb *db, robj *key) {
...
@@ -1525,8 +1563,18 @@ static robj *lookupKeyWrite(redisDb *db, robj *key) {
}
}
static
int
deleteKey
(
redisDb
*
db
,
robj
*
key
)
{
static
int
deleteKey
(
redisDb
*
db
,
robj
*
key
)
{
int
retval
;
/* We need to protect key from destruction: after the first dictDelete()
* it may happen that 'key' is no longer valid if we don't increment
* it's count. This may happen when we get the object reference directly
* from the hash table with dictRandomKey() or dict iterators */
incrRefCount
(
key
);
if
(
dictSize
(
db
->
expires
))
dictDelete
(
db
->
expires
,
key
);
if
(
dictSize
(
db
->
expires
))
dictDelete
(
db
->
expires
,
key
);
return
dictDelete
(
db
->
dict
,
key
)
==
DICT_OK
;
retval
=
dictDelete
(
db
->
dict
,
key
);
decrRefCount
(
key
);
return
retval
==
DICT_OK
;
}
}
/*============================ DB saving/loading ============================ */
/*============================ DB saving/loading ============================ */
...
...
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