int CodisClient::incrby(string key, int incr, int tt) { Reply rep = RedisCommand(Command("INCRBY")(key)(int2string(incr)), tt); return (rep.error()) ? 0 : rep.integer(); }
/* * Class: com_redis_sentinel_client_RedisClient * Method: cRedisCommand * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_redis_sentinel_client_RedisClient_cRedisCommand__Ljava_lang_String_2 (JNIEnv *env, jclass, jstring jCommand) { const char* ccommand = env->GetStringUTFChars(jCommand, NULL); std::string comm(ccommand); env->ReleaseStringUTFChars(jCommand, ccommand); std::vector<std::string> command; split(comm, std::string(" "), command); Reply ret = client->RedisCommand(command); stringstream stream; stream << ret.type() << ";" << ret.str() << ";" << ret.error() << ";" << ret.integer() << ";"; std::vector<Reply> elements = ret.elements(); for (size_t i=0; i<elements.size(); i++) { stream << elements[i].type() << ";" << elements[i].str() << ";" << elements[i].error() << ";" << elements[i].integer() << ";"; } jstring reply = env->NewStringUTF(stream.str().c_str()); return reply; }
vector<string> CodisClient::mget(vector<string>& keys, int tt) { vector<string> values; Command comm("MGET"); for (size_t i=0; i<keys.size(); i++) { comm(keys[i]); } Reply rep = RedisCommand(comm, tt); if (rep.error()) { return values; } else { for (size_t i=0; i<rep.elements().size(); i++) { values.push_back(rep.elements()[i].str()); } } return values; }
string CodisClient::hget(string key, string field, int tt) { Reply rep = RedisCommand(Command("HGET")(key)(field), tt); if (rep.error()) { return ""; } else { return rep.str(); } }
string CodisClient::srandmember(string key, int tt) { Reply rep = RedisCommand(Command("SRANDMEMBER")(key), tt); if (rep.error()) { return ""; } else { return rep.str(); } }
string CodisClient::spop(string key, int tt) { Reply rep = RedisCommand(Command("SPOP")(key), tt); if (rep.error()) { return ""; } else { return rep.str(); } }
bool CodisClient::hgetall(string key, vector<string>& fields, vector<string>& values, int tt) { Reply rep = RedisCommand(Command("HGETALL")(key), tt); if (rep.error()) return false; for (size_t i=1; i<rep.elements().size(); i+=2) { fields.push_back(rep.elements()[i-1].str()); values.push_back(rep.elements()[i].str()); } return true; }
string CodisClient::getset(string key, string value, int tt) { if (value.length()>1048576) { throw myex; } Reply rep = RedisCommand(Command("GETSET")(key)(value), tt); if (rep.error()) { return ""; } else { return rep.str(); } }
void ReplyToString(Reply& reply, std::string &rep) { stringstream stream; stream << reply.type() << ";" << reply.str() << ";" << reply.error() << ";" << reply.integer() << ";"; std::vector<Reply> elements = reply.elements(); for (size_t i=0; i<elements.size(); i++) { stream << elements[i].type() << ";" << elements[i].str() << ";" << elements[i].error() << ";" << elements[i].integer() << ";"; } rep = stream.str(); }
bool CodisClient::mset(map<string, string>& keyvalues, int tt) { Command comm("MSET"); map<string, string>::iterator it = keyvalues.begin(); for (; it!=keyvalues.end(); it++) { if (it->second.length()>1048576) { throw myex; } comm(it->first)(it->second); } Reply rep = RedisCommand(comm, tt); if (rep.error()) { return false; } else { return true; } }
int CodisClient::decr(string key, int tt) { Reply rep = RedisCommand(Command("DECR")(key), tt); return (rep.error()) ? 0 : rep.integer(); }