Skip to content

Instantly share code, notes, and snippets.

@ecto
Created September 10, 2012 15:21
Show Gist options
  • Select an option

  • Save ecto/3691484 to your computer and use it in GitHub Desktop.

Select an option

Save ecto/3691484 to your computer and use it in GitHub Desktop.
diff --git a/src/db.c b/src/db.c
index 6447838..ad28b1c 100644
--- a/src/db.c
+++ b/src/db.c
@@ -280,6 +280,31 @@ void keysCommand(redisClient *c) {
setDeferredMultiBulkLength(c,replylen,numkeys);
}
+void countCommand(redisClient *c) {
+ dictIterator *di;
+ dictEntry *de;
+ sds pattern = c->argv[1]->ptr;
+ int plen = sdslen(pattern), allkeys;
+ unsigned long numkeys = 0;
+
+ di = dictGetSafeIterator(c->db->dict);
+ allkeys = (pattern[0] == '*' && pattern[1] == '\0');
+ while((de = dictNext(di)) != NULL) {
+ sds key = dictGetKey(de);
+ robj *keyobj;
+
+ if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) {
+ keyobj = createStringObject(key,sdslen(key));
+ if (expireIfNeeded(c->db,keyobj) == 0) {
+ numkeys++;
+ }
+ decrRefCount(keyobj);
+ }
+ }
+ dictReleaseIterator(di);
+ addReplyLongLong(c,numkeys);
+}
+
void dbsizeCommand(redisClient *c) {
addReplyLongLong(c,dictSize(c->db->dict));
}
diff --git a/src/redis.c b/src/redis.c
index 1f5725b..4802cb2 100644
--- a/src/redis.c
+++ b/src/redis.c
@@ -205,6 +205,7 @@ struct redisCommand redisCommandTable[] = {
{"pexpire",pexpireCommand,3,"w",0,NULL,1,1,1,0,0},
{"pexpireat",pexpireatCommand,3,"w",0,NULL,1,1,1,0,0},
{"keys",keysCommand,2,"rS",0,NULL,0,0,0,0,0},
+ {"count",countCommand,2,"r",0,NULL,0,0,0,0,0},
{"dbsize",dbsizeCommand,1,"r",0,NULL,0,0,0,0,0},
{"auth",authCommand,2,"rs",0,NULL,0,0,0,0,0},
{"ping",pingCommand,1,"r",0,NULL,0,0,0,0,0},
diff --git a/src/redis.h b/src/redis.h
index ec2bde2..b9a7ab4 100644
--- a/src/redis.h
+++ b/src/redis.h
@@ -1160,6 +1160,7 @@ void incrbyfloatCommand(redisClient *c);
void selectCommand(redisClient *c);
void randomkeyCommand(redisClient *c);
void keysCommand(redisClient *c);
+void countCommand(redisClient *c);
void dbsizeCommand(redisClient *c);
void lastsaveCommand(redisClient *c);
void saveCommand(redisClient *c);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment