|
@@ -156,7 +156,7 @@ void RedisClient::start()
|
|
|
redisTimer->start(1000);
|
|
|
}
|
|
|
|
|
|
-bool RedisClient::hset(QString m, QString k, QString v)
|
|
|
+bool RedisClient::hset(const QString& m, const QString& k, const QString& v)
|
|
|
{
|
|
|
//如果 field 是哈希表中的一个新建域,并且值设置成功,reply->integer为1
|
|
|
//如果哈希表中域 field 已经存在且旧值已被新值覆盖,reply->integer为0
|
|
@@ -176,7 +176,7 @@ bool RedisClient::hset(QString m, QString k, QString v)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-QString RedisClient::hget(QString m, QString k)
|
|
|
+QString RedisClient::hget(const QString& m, const QString& k)
|
|
|
{
|
|
|
QString ret;
|
|
|
redisReply * reply = (redisReply*)redisCommand(rc, QString("hget " + m + " " + k).toStdString().c_str());
|
|
@@ -197,8 +197,74 @@ QString RedisClient::hget(QString m, QString k)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
+QStringList RedisClient::hkeys(const QString& k)
|
|
|
+{
|
|
|
+ QStringList ret;
|
|
|
+ redisReply *reply = (redisReply *)redisCommand(rc, QString("hkeys " + k).toStdString().c_str());
|
|
|
+ if (rc->err)
|
|
|
+ {
|
|
|
+ redisOk = false;
|
|
|
+ qDebug() << rc->errstr;
|
|
|
+ }
|
|
|
+ else if (reply->type == REDIS_REPLY_ARRAY)
|
|
|
+ {
|
|
|
+ // type:4表示不存在,type:1表示返回的为字符串
|
|
|
+ // qDebug() <<"type:" << reply->type << reply->str << reply->len; //type:1
|
|
|
+ for (size_t i = 0; i < reply->elements; i++)
|
|
|
+ {
|
|
|
+ // qDebug() << "key: " << i << reply->element[i]->str;
|
|
|
+ ret.append(QString(reply->element[i]->str));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+QStringList RedisClient::hvals(const QString& k)
|
|
|
+{
|
|
|
+ QStringList ret;
|
|
|
+ redisReply *reply = (redisReply *)redisCommand(rc, QString("hvals " + k).toStdString().c_str());
|
|
|
+ if (rc->err)
|
|
|
+ {
|
|
|
+ redisOk = false;
|
|
|
+ qDebug() << rc->errstr;
|
|
|
+ }
|
|
|
+ else if (reply->type == REDIS_REPLY_ARRAY)
|
|
|
+ {
|
|
|
+ // type:4表示不存在,type:1表示返回的为字符串
|
|
|
+ // qDebug() <<"type:" << reply->type << reply->str << reply->len; //type:1
|
|
|
+ for (size_t i = 0; i < reply->elements; i++)
|
|
|
+ {
|
|
|
+ // qDebug() << "key: " << i << reply->element[i]->str;
|
|
|
+ ret.append(QString(reply->element[i]->str));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+QHash<QString, QString> RedisClient::hgetall(const QString& k)
|
|
|
+{
|
|
|
+ QHash<QString, QString> ret;
|
|
|
+ redisReply *reply = (redisReply *)redisCommand(rc, QString("hgetall " + k).toStdString().c_str());
|
|
|
+ if (rc->err)
|
|
|
+ {
|
|
|
+ redisOk = false;
|
|
|
+ qDebug() << rc->errstr;
|
|
|
+ }
|
|
|
+ else if (reply->type == REDIS_REPLY_ARRAY)
|
|
|
+ {
|
|
|
+ // type:4表示不存在,type:1表示返回的为字符串
|
|
|
+ // qDebug() <<"type:" << reply->type << reply->str << reply->len; //type:1
|
|
|
+ for (size_t i = 0; i < reply->elements; i += 2)
|
|
|
+ {
|
|
|
+ // qDebug() << "key: " << i << reply->element[i]->str;
|
|
|
+ ret.insert(QString(reply->element[i]->str), QString(reply->element[i + 1]->str));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
//向队列尾(右)部加入字符串数据
|
|
|
-bool RedisClient::rpush(QString lData, QString js)
|
|
|
+bool RedisClient::rpush(const QString& lData, const QString& js)
|
|
|
{
|
|
|
bool ret = false;
|
|
|
redisReply * reply = (redisReply*)redisCommand(rc, QString("rpush " + lData + " " + js).toStdString().c_str());
|
|
@@ -220,7 +286,7 @@ bool RedisClient::rpush(QString lData, QString js)
|
|
|
}
|
|
|
|
|
|
//向队列尾(右)部加入二进制数据
|
|
|
-bool RedisClient::rpushb(QString lData, QByteArray ba)
|
|
|
+bool RedisClient::rpushb(const QString& lData, const QByteArray& ba)
|
|
|
{
|
|
|
bool ret = false;
|
|
|
const char *arg[3]; //3个参数(cmd, k, v)
|
|
@@ -253,7 +319,7 @@ bool RedisClient::rpushb(QString lData, QByteArray ba)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-QString RedisClient::lpop(QString lData)
|
|
|
+QString RedisClient::lpop(const QString& lData)
|
|
|
{
|
|
|
QString ret;
|
|
|
redisReply * reply = (redisReply*)redisCommand(rc, QString("lpop " + lData).toStdString().c_str());
|
|
@@ -488,7 +554,7 @@ bool RedisClient::publishb(const QString& ch,const QByteArray& ba)
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-void RedisClient::subscribe(QString ch, redisCallbackFn *fn, void* data)
|
|
|
+void RedisClient::subscribe(const QString& ch, redisCallbackFn *fn, void* data)
|
|
|
{
|
|
|
if (rac == nullptr)
|
|
|
{
|
|
@@ -517,7 +583,7 @@ void RedisClient::subscribe(QString ch, redisCallbackFn *fn, void* data)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void RedisClient::psubscribe(QString ch, redisCallbackFn *fn, void* data)
|
|
|
+void RedisClient::psubscribe(const QString& ch, redisCallbackFn *fn, void* data)
|
|
|
{
|
|
|
if (rac == nullptr)
|
|
|
{
|
|
@@ -547,11 +613,11 @@ void RedisClient::psubscribe(QString ch, redisCallbackFn *fn, void* data)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void RedisClient::Setup(std::string addr, uint _port, std::string password)
|
|
|
+void RedisClient::Setup(const QString& addr, uint _port,const QString&password)
|
|
|
{
|
|
|
- ip = addr.c_str();
|
|
|
+ ip = addr;
|
|
|
port = _port;
|
|
|
- auth = password.c_str();
|
|
|
+ auth = password;
|
|
|
|
|
|
redisOk = false;
|
|
|
}
|