Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
f6475c72
Commit
f6475c72
authored
Jun 12, 2010
by
antirez
Browse files
Merge branch 'expire' of
git://github.com/pietern/redis
parents
ac9b8cfe
829137b9
Changes
1
Hide whitespace changes
Inline
Side-by-side
redis.c
View file @
f6475c72
...
@@ -1258,7 +1258,7 @@ static dictType keyptrDictType = {
...
@@ -1258,7 +1258,7 @@ static dictType keyptrDictType = {
NULL, /* key dup */
NULL, /* key dup */
NULL, /* val dup */
NULL, /* val dup */
dictSdsKeyCompare, /* key compare */
dictSdsKeyCompare, /* key compare */
dictSdsDestructor,
/* key destructor */
NULL,
/* key destructor */
NULL /* val destructor */
NULL /* val destructor */
};
};
...
@@ -3532,12 +3532,10 @@ static robj *dbRandomKey(redisDb *db) {
...
@@ -3532,12 +3532,10 @@ static robj *dbRandomKey(redisDb *db) {
/* Delete a key, value, and associated expiration entry if any, from the DB */
/* Delete a key, value, and associated expiration entry if any, from the DB */
static int dbDelete(redisDb *db, robj *key) {
static int dbDelete(redisDb *db, robj *key) {
int retval;
/* Deleting an entry from the expires dict will not free the sds of
* the key, because it is shared with the main dictionary. */
if (dictSize(db->expires)) dictDelete(db->expires,key->ptr);
if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);
retval = dictDelete(db->dict,key->ptr);
return dictDelete(db->dict,key->ptr) == DICT_OK;
return retval == DICT_OK;
}
}
/*============================ RDB saving/loading =========================== */
/*============================ RDB saving/loading =========================== */
...
@@ -7856,6 +7854,9 @@ static void monitorCommand(redisClient *c) {
...
@@ -7856,6 +7854,9 @@ static void monitorCommand(redisClient *c) {
/* ================================= Expire ================================= */
/* ================================= Expire ================================= */
static int removeExpire(redisDb *db, robj *key) {
static int removeExpire(redisDb *db, robj *key) {
/* An expire may only be removed if there is a corresponding entry in the
* main dict. Otherwise, the key will never be freed. */
redisAssert(dictFind(db->dict,key->ptr) != NULL);
if (dictDelete(db->expires,key->ptr) == DICT_OK) {
if (dictDelete(db->expires,key->ptr) == DICT_OK) {
return 1;
return 1;
} else {
} else {
...
@@ -7864,9 +7865,11 @@ static int removeExpire(redisDb *db, robj *key) {
...
@@ -7864,9 +7865,11 @@ static int removeExpire(redisDb *db, robj *key) {
}
}
static int setExpire(redisDb *db, robj *key, time_t when) {
static int setExpire(redisDb *db, robj *key, time_t when) {
sds copy = sdsdup(key->ptr);
dictEntry *de;
if (dictAdd(db->expires,copy,(void*)when) == DICT_ERR) {
sdsfree(copy);
/* Reuse the sds from the main dict in the expire dict */
redisAssert((de = dictFind(db->dict,key->ptr)) != NULL);
if (dictAdd(db->expires,dictGetEntryKey(de),(void*)when) == DICT_ERR) {
return 0;
return 0;
} else {
} else {
return 1;
return 1;
...
@@ -7882,39 +7885,32 @@ static time_t getExpire(redisDb *db, robj *key) {
...
@@ -7882,39 +7885,32 @@ static time_t getExpire(redisDb *db, robj *key) {
if (dictSize(db->expires) == 0 ||
if (dictSize(db->expires) == 0 ||
(de = dictFind(db->expires,key->ptr)) == NULL) return -1;
(de = dictFind(db->expires,key->ptr)) == NULL) return -1;
/* The entry was found in the expire dict, this means it should also
* be present in the main dict (safety check). */
redisAssert(dictFind(db->dict,key->ptr) != NULL);
return (time_t) dictGetEntryVal(de);
return (time_t) dictGetEntryVal(de);
}
}
static int expireIfNeeded(redisDb *db, robj *key) {
static int expireIfNeeded(redisDb *db, robj *key) {
time_t when;
time_t when = getExpire(db,key);
dictEntry *de;
if (when < 0) return 0;
/* No expire? return ASAP */
if (dictSize(db->expires) == 0 ||
(de = dictFind(db->expires,key->ptr)) == NULL) return 0;
/* Lookup the expire */
/* Return when this key has not expired */
when = (time_t) dictGetEntryVal(de);
if (time(NULL) <= when) return 0;
if (time(NULL) <= when) return 0;
/* Delete the key */
/* Delete the key */
dbDelete(db,key);
server.stat_expiredkeys++;
server.stat_expiredkeys++;
return 1;
server.dirty++;
return dbDelete(db,key);
}
}
static int deleteIfVolatile(redisDb *db, robj *key) {
static int deleteIfVolatile(redisDb *db, robj *key) {
dictEntry *de;
if (getExpire(db,key) < 0) return 0;
/* No expire? return ASAP */
if (dictSize(db->expires) == 0 ||
(de = dictFind(db->expires,key->ptr)) == NULL) return 0;
/* Delete the key */
/* Delete the key */
server.dirty++;
server.stat_expiredkeys++;
server.stat_expiredkeys++;
dictDelete(db->expires,key->ptr)
;
server.dirty++
;
return d
ict
Delete(db
->dict,key->ptr) == DICT_OK
;
return d
b
Delete(db
,key)
;
}
}
static void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
static void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
...
...
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