2010-06-21 18:07:48 -04:00
|
|
|
#include "redis.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* C-level DB API
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
robj *lookupKey(redisDb *db, robj *key) {
|
|
|
|
dictEntry *de = dictFind(db->dict,key->ptr);
|
|
|
|
if (de) {
|
|
|
|
robj *val = dictGetEntryVal(de);
|
|
|
|
|
|
|
|
if (server.vm_enabled) {
|
|
|
|
if (val->storage == REDIS_VM_MEMORY ||
|
|
|
|
val->storage == REDIS_VM_SWAPPING)
|
|
|
|
{
|
|
|
|
/* If we were swapping the object out, cancel the operation */
|
|
|
|
if (val->storage == REDIS_VM_SWAPPING)
|
|
|
|
vmCancelThreadedIOJob(val);
|
|
|
|
/* Update the access time for the aging algorithm. */
|
|
|
|
val->lru = server.lruclock;
|
|
|
|
} else {
|
|
|
|
int notify = (val->storage == REDIS_VM_LOADING);
|
|
|
|
|
|
|
|
/* Our value was swapped on disk. Bring it at home. */
|
|
|
|
redisAssert(val->type == REDIS_VMPOINTER);
|
|
|
|
val = vmLoadObject(val);
|
|
|
|
dictGetEntryVal(de) = val;
|
|
|
|
|
|
|
|
/* Clients blocked by the VM subsystem may be waiting for
|
|
|
|
* this key... */
|
|
|
|
if (notify) handleClientsBlockedOnSwappedKey(db,key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyRead(redisDb *db, robj *key) {
|
|
|
|
expireIfNeeded(db,key);
|
|
|
|
return lookupKey(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyWrite(redisDb *db, robj *key) {
|
2010-08-02 12:13:39 -04:00
|
|
|
expireIfNeeded(db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
return lookupKey(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
|
|
|
|
robj *o = lookupKeyRead(c->db, key);
|
|
|
|
if (!o) addReply(c,reply);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
|
|
|
|
robj *o = lookupKeyWrite(c->db, key);
|
|
|
|
if (!o) addReply(c,reply);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the key to the DB. If the key already exists REDIS_ERR is returned,
|
|
|
|
* otherwise REDIS_OK is returned, and the caller should increment the
|
|
|
|
* refcount of 'val'. */
|
|
|
|
int dbAdd(redisDb *db, robj *key, robj *val) {
|
|
|
|
/* Perform a lookup before adding the key, as we need to copy the
|
|
|
|
* key value. */
|
|
|
|
if (dictFind(db->dict, key->ptr) != NULL) {
|
|
|
|
return REDIS_ERR;
|
|
|
|
} else {
|
|
|
|
sds copy = sdsdup(key->ptr);
|
|
|
|
dictAdd(db->dict, copy, val);
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the key does not exist, this is just like dbAdd(). Otherwise
|
|
|
|
* the value associated to the key is replaced with the new one.
|
|
|
|
*
|
|
|
|
* On update (key already existed) 0 is returned. Otherwise 1. */
|
|
|
|
int dbReplace(redisDb *db, robj *key, robj *val) {
|
|
|
|
if (dictFind(db->dict,key->ptr) == NULL) {
|
|
|
|
sds copy = sdsdup(key->ptr);
|
|
|
|
dictAdd(db->dict, copy, val);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
dictReplace(db->dict, key->ptr, val);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int dbExists(redisDb *db, robj *key) {
|
|
|
|
return dictFind(db->dict,key->ptr) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a random key, in form of a Redis object.
|
|
|
|
* If there are no keys, NULL is returned.
|
|
|
|
*
|
|
|
|
* The function makes sure to return keys not already expired. */
|
|
|
|
robj *dbRandomKey(redisDb *db) {
|
|
|
|
struct dictEntry *de;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
sds key;
|
|
|
|
robj *keyobj;
|
|
|
|
|
|
|
|
de = dictGetRandomKey(db->dict);
|
|
|
|
if (de == NULL) return NULL;
|
|
|
|
|
|
|
|
key = dictGetEntryKey(de);
|
|
|
|
keyobj = createStringObject(key,sdslen(key));
|
|
|
|
if (dictFind(db->expires,key)) {
|
|
|
|
if (expireIfNeeded(db,keyobj)) {
|
|
|
|
decrRefCount(keyobj);
|
|
|
|
continue; /* search for another key. This expired. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keyobj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a key, value, and associated expiration entry if any, from the DB */
|
|
|
|
int dbDelete(redisDb *db, robj *key) {
|
|
|
|
/* 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) > 0) dictDelete(db->expires,key->ptr);
|
|
|
|
return dictDelete(db->dict,key->ptr) == DICT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty the whole database */
|
|
|
|
long long emptyDb() {
|
|
|
|
int j;
|
|
|
|
long long removed = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < server.dbnum; j++) {
|
|
|
|
removed += dictSize(server.db[j].dict);
|
|
|
|
dictEmpty(server.db[j].dict);
|
|
|
|
dictEmpty(server.db[j].expires);
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
int selectDb(redisClient *c, int id) {
|
|
|
|
if (id < 0 || id >= server.dbnum)
|
|
|
|
return REDIS_ERR;
|
|
|
|
c->db = &server.db[id];
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Type agnostic commands operating on the key space
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void flushdbCommand(redisClient *c) {
|
|
|
|
server.dirty += dictSize(c->db->dict);
|
|
|
|
touchWatchedKeysOnFlush(c->db->id);
|
|
|
|
dictEmpty(c->db->dict);
|
|
|
|
dictEmpty(c->db->expires);
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flushallCommand(redisClient *c) {
|
|
|
|
touchWatchedKeysOnFlush(-1);
|
|
|
|
server.dirty += emptyDb();
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
if (server.bgsavechildpid != -1) {
|
|
|
|
kill(server.bgsavechildpid,SIGKILL);
|
|
|
|
rdbRemoveTempFile(server.bgsavechildpid);
|
|
|
|
}
|
|
|
|
rdbSave(server.dbfilename);
|
|
|
|
server.dirty++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delCommand(redisClient *c) {
|
|
|
|
int deleted = 0, j;
|
|
|
|
|
|
|
|
for (j = 1; j < c->argc; j++) {
|
|
|
|
if (dbDelete(c->db,c->argv[j])) {
|
|
|
|
touchWatchedKey(c->db,c->argv[j]);
|
|
|
|
server.dirty++;
|
|
|
|
deleted++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addReplyLongLong(c,deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void existsCommand(redisClient *c) {
|
|
|
|
expireIfNeeded(c->db,c->argv[1]);
|
|
|
|
if (dbExists(c->db,c->argv[1])) {
|
|
|
|
addReply(c, shared.cone);
|
|
|
|
} else {
|
|
|
|
addReply(c, shared.czero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectCommand(redisClient *c) {
|
|
|
|
int id = atoi(c->argv[1]->ptr);
|
|
|
|
|
|
|
|
if (selectDb(c,id) == REDIS_ERR) {
|
|
|
|
addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomkeyCommand(redisClient *c) {
|
|
|
|
robj *key;
|
|
|
|
|
|
|
|
if ((key = dbRandomKey(c->db)) == NULL) {
|
|
|
|
addReply(c,shared.nullbulk);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addReplyBulk(c,key);
|
|
|
|
decrRefCount(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void keysCommand(redisClient *c) {
|
|
|
|
dictIterator *di;
|
|
|
|
dictEntry *de;
|
|
|
|
sds pattern = c->argv[1]->ptr;
|
2010-08-30 05:51:45 -04:00
|
|
|
int plen = sdslen(pattern), allkeys;
|
2010-06-21 18:07:48 -04:00
|
|
|
unsigned long numkeys = 0;
|
|
|
|
robj *lenobj = createObject(REDIS_STRING,NULL);
|
|
|
|
|
|
|
|
di = dictGetIterator(c->db->dict);
|
|
|
|
addReply(c,lenobj);
|
|
|
|
decrRefCount(lenobj);
|
2010-08-30 05:51:45 -04:00
|
|
|
allkeys = (pattern[0] == '*' && pattern[1] == '\0');
|
2010-06-21 18:07:48 -04:00
|
|
|
while((de = dictNext(di)) != NULL) {
|
|
|
|
sds key = dictGetEntryKey(de);
|
|
|
|
robj *keyobj;
|
|
|
|
|
2010-08-30 05:51:45 -04:00
|
|
|
if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) {
|
2010-06-21 18:07:48 -04:00
|
|
|
keyobj = createStringObject(key,sdslen(key));
|
|
|
|
if (expireIfNeeded(c->db,keyobj) == 0) {
|
|
|
|
addReplyBulk(c,keyobj);
|
|
|
|
numkeys++;
|
|
|
|
}
|
|
|
|
decrRefCount(keyobj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dictReleaseIterator(di);
|
|
|
|
lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",numkeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dbsizeCommand(redisClient *c) {
|
|
|
|
addReplySds(c,
|
|
|
|
sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void lastsaveCommand(redisClient *c) {
|
|
|
|
addReplySds(c,
|
|
|
|
sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
|
|
|
|
}
|
|
|
|
|
|
|
|
void typeCommand(redisClient *c) {
|
|
|
|
robj *o;
|
|
|
|
char *type;
|
|
|
|
|
|
|
|
o = lookupKeyRead(c->db,c->argv[1]);
|
|
|
|
if (o == NULL) {
|
|
|
|
type = "+none";
|
|
|
|
} else {
|
|
|
|
switch(o->type) {
|
|
|
|
case REDIS_STRING: type = "+string"; break;
|
|
|
|
case REDIS_LIST: type = "+list"; break;
|
|
|
|
case REDIS_SET: type = "+set"; break;
|
|
|
|
case REDIS_ZSET: type = "+zset"; break;
|
|
|
|
case REDIS_HASH: type = "+hash"; break;
|
|
|
|
default: type = "+unknown"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addReplySds(c,sdsnew(type));
|
|
|
|
addReply(c,shared.crlf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveCommand(redisClient *c) {
|
|
|
|
if (server.bgsavechildpid != -1) {
|
|
|
|
addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rdbSave(server.dbfilename) == REDIS_OK) {
|
|
|
|
addReply(c,shared.ok);
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bgsaveCommand(redisClient *c) {
|
|
|
|
if (server.bgsavechildpid != -1) {
|
|
|
|
addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
|
|
|
|
char *status = "+Background saving started\r\n";
|
|
|
|
addReplySds(c,sdsnew(status));
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdownCommand(redisClient *c) {
|
|
|
|
if (prepareForShutdown() == REDIS_OK)
|
|
|
|
exit(0);
|
|
|
|
addReplySds(c, sdsnew("-ERR Errors trying to SHUTDOWN. Check logs.\r\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void renameGenericCommand(redisClient *c, int nx) {
|
|
|
|
robj *o;
|
|
|
|
|
|
|
|
/* To use the same key as src and dst is probably an error */
|
|
|
|
if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
|
|
|
|
addReply(c,shared.sameobjecterr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
incrRefCount(o);
|
|
|
|
if (dbAdd(c->db,c->argv[2],o) == REDIS_ERR) {
|
|
|
|
if (nx) {
|
|
|
|
decrRefCount(o);
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbReplace(c->db,c->argv[2],o);
|
|
|
|
}
|
|
|
|
dbDelete(c->db,c->argv[1]);
|
2010-07-12 06:01:15 -04:00
|
|
|
touchWatchedKey(c->db,c->argv[1]);
|
2010-06-21 18:07:48 -04:00
|
|
|
touchWatchedKey(c->db,c->argv[2]);
|
|
|
|
server.dirty++;
|
|
|
|
addReply(c,nx ? shared.cone : shared.ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void renameCommand(redisClient *c) {
|
|
|
|
renameGenericCommand(c,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void renamenxCommand(redisClient *c) {
|
|
|
|
renameGenericCommand(c,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void moveCommand(redisClient *c) {
|
|
|
|
robj *o;
|
|
|
|
redisDb *src, *dst;
|
|
|
|
int srcid;
|
|
|
|
|
|
|
|
/* Obtain source and target DB pointers */
|
|
|
|
src = c->db;
|
|
|
|
srcid = c->db->id;
|
|
|
|
if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
|
|
|
|
addReply(c,shared.outofrangeerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst = c->db;
|
|
|
|
selectDb(c,srcid); /* Back to the source DB */
|
|
|
|
|
|
|
|
/* If the user is moving using as target the same
|
|
|
|
* DB as the source DB it is probably an error. */
|
|
|
|
if (src == dst) {
|
|
|
|
addReply(c,shared.sameobjecterr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the element exists and get a reference */
|
|
|
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
if (!o) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to add the element to the target DB */
|
|
|
|
if (dbAdd(dst,c->argv[1],o) == REDIS_ERR) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
incrRefCount(o);
|
|
|
|
|
|
|
|
/* OK! key moved, free the entry in the source DB */
|
|
|
|
dbDelete(src,c->argv[1]);
|
|
|
|
server.dirty++;
|
|
|
|
addReply(c,shared.cone);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Expires API
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
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);
|
2010-08-03 08:19:20 -04:00
|
|
|
return dictDelete(db->expires,key->ptr) == DICT_OK;
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 06:26:30 -04:00
|
|
|
void setExpire(redisDb *db, robj *key, time_t when) {
|
2010-06-21 18:07:48 -04:00
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
/* Reuse the sds from the main dict in the expire dict */
|
2010-08-03 06:26:30 -04:00
|
|
|
de = dictFind(db->dict,key->ptr);
|
|
|
|
redisAssert(de != NULL);
|
|
|
|
dictReplace(db->expires,dictGetEntryKey(de),(void*)when);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the expire time of the specified key, or -1 if no expire
|
|
|
|
* is associated with this key (i.e. the key is non volatile) */
|
|
|
|
time_t getExpire(redisDb *db, robj *key) {
|
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
/* No expire? return ASAP */
|
|
|
|
if (dictSize(db->expires) == 0 ||
|
|
|
|
(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);
|
|
|
|
}
|
|
|
|
|
2010-08-02 12:13:39 -04:00
|
|
|
/* Propagate expires into slaves and the AOF file.
|
|
|
|
* When a key expires in the master, a DEL operation for this key is sent
|
|
|
|
* to all the slaves and the AOF file if enabled.
|
|
|
|
*
|
|
|
|
* This way the key expiry is centralized in one place, and since both
|
|
|
|
* AOF and the master->slave link guarantee operation ordering, everything
|
|
|
|
* will be consistent even if we allow write operations against expiring
|
|
|
|
* keys. */
|
|
|
|
void propagateExpire(redisDb *db, robj *key) {
|
|
|
|
struct redisCommand *cmd;
|
|
|
|
robj *argv[2];
|
|
|
|
|
|
|
|
cmd = lookupCommand("del");
|
|
|
|
argv[0] = createStringObject("DEL",3);
|
|
|
|
argv[1] = key;
|
|
|
|
incrRefCount(key);
|
|
|
|
|
|
|
|
if (server.appendonly)
|
|
|
|
feedAppendOnlyFile(cmd,db->id,argv,2);
|
|
|
|
if (listLength(server.slaves))
|
|
|
|
replicationFeedSlaves(server.slaves,db->id,argv,2);
|
|
|
|
|
2010-08-02 15:37:39 -04:00
|
|
|
decrRefCount(argv[0]);
|
|
|
|
decrRefCount(argv[1]);
|
2010-08-02 12:13:39 -04:00
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
int expireIfNeeded(redisDb *db, robj *key) {
|
|
|
|
time_t when = getExpire(db,key);
|
2010-08-02 12:13:39 -04:00
|
|
|
|
|
|
|
/* If we are running in the context of a slave, return ASAP:
|
|
|
|
* the slave key expiration is controlled by the master that will
|
|
|
|
* send us synthesized DEL operations for expired keys.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* we think the key is expired at this time. */
|
|
|
|
if (server.masterhost != NULL) {
|
|
|
|
return time(NULL) > when;
|
|
|
|
}
|
|
|
|
|
2010-06-21 18:07:48 -04:00
|
|
|
if (when < 0) return 0;
|
|
|
|
|
|
|
|
/* Return when this key has not expired */
|
|
|
|
if (time(NULL) <= when) return 0;
|
|
|
|
|
|
|
|
/* Delete the key */
|
|
|
|
server.stat_expiredkeys++;
|
|
|
|
server.dirty++;
|
2010-08-02 12:13:39 -04:00
|
|
|
propagateExpire(db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
return dbDelete(db,key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* Expires Commands
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
|
|
|
|
dictEntry *de;
|
|
|
|
time_t seconds;
|
|
|
|
|
|
|
|
if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
|
|
|
|
|
|
|
|
seconds -= offset;
|
|
|
|
|
|
|
|
de = dictFind(c->db->dict,key->ptr);
|
|
|
|
if (de == NULL) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (seconds <= 0) {
|
|
|
|
if (dbDelete(c->db,key)) server.dirty++;
|
|
|
|
addReply(c, shared.cone);
|
2010-07-05 13:38:12 -04:00
|
|
|
touchWatchedKey(c->db,key);
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
time_t when = time(NULL)+seconds;
|
2010-08-03 06:26:30 -04:00
|
|
|
setExpire(c->db,key,when);
|
|
|
|
addReply(c,shared.cone);
|
|
|
|
touchWatchedKey(c->db,key);
|
|
|
|
server.dirty++;
|
2010-06-21 18:07:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expireCommand(redisClient *c) {
|
|
|
|
expireGenericCommand(c,c->argv[1],c->argv[2],0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expireatCommand(redisClient *c) {
|
|
|
|
expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ttlCommand(redisClient *c) {
|
2010-08-23 11:06:38 -04:00
|
|
|
time_t expire, ttl = -1;
|
2010-06-21 18:07:48 -04:00
|
|
|
|
|
|
|
expire = getExpire(c->db,c->argv[1]);
|
|
|
|
if (expire != -1) {
|
2010-08-23 11:06:38 -04:00
|
|
|
ttl = (expire-time(NULL));
|
2010-06-21 18:07:48 -04:00
|
|
|
if (ttl < 0) ttl = -1;
|
|
|
|
}
|
2010-08-23 11:06:38 -04:00
|
|
|
addReplyLongLong(c,(long long)ttl);
|
2010-06-21 18:07:48 -04:00
|
|
|
}
|
2010-08-03 08:19:20 -04:00
|
|
|
|
|
|
|
void persistCommand(redisClient *c) {
|
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
de = dictFind(c->db->dict,c->argv[1]->ptr);
|
|
|
|
if (de == NULL) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
} else {
|
2010-08-03 08:25:22 -04:00
|
|
|
if (removeExpire(c->db,c->argv[1])) {
|
2010-08-03 08:19:20 -04:00
|
|
|
addReply(c,shared.cone);
|
2010-08-03 08:25:22 -04:00
|
|
|
server.dirty++;
|
|
|
|
} else {
|
2010-08-03 08:19:20 -04:00
|
|
|
addReply(c,shared.czero);
|
2010-08-03 08:25:22 -04:00
|
|
|
}
|
2010-08-03 08:19:20 -04:00
|
|
|
}
|
|
|
|
}
|