RedisClient.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include "hiredis/adapters/qt.h"
  3. #include "hiredis/async.h"
  4. #include "hiredis/hiredis.h"
  5. #include <QObject>
  6. #include <QDebug>
  7. #include <QJsonObject>
  8. #include <QJsonDocument>
  9. #include "RedisClient_global.h"
  10. class REDISCLIENT_EXPORT RedisClient : public QObject
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit RedisClient(QObject *parent = 0);
  15. ~RedisClient();
  16. public slots:
  17. void start();
  18. void conn();
  19. bool hset(QString m, QString k, QString v);
  20. QString hget(QString m, QString k);
  21. bool set(QString k, QString v);
  22. bool setb(QString k, QByteArray &v);
  23. QString get(QString k);
  24. QByteArray getb(QString k);
  25. bool rpush(QString lData, QString js); //向队列尾部加入字符串数据
  26. bool rpushb(QString lData, QByteArray ba); //向队列尾部加入二进制数据
  27. QString lpop(QString lData);
  28. QString blpop(QString lData, quint32 timeout); //同步阻塞一定时间返回数据
  29. bool blpop(QString lData, redisCallbackFn *fn); //阻塞从队列头部获取最早数据
  30. bool publish(const QString& ch, const QString& js);
  31. bool publishb(const QString& ch, const QByteArray& ba);
  32. bool expire(QString k,int sec);
  33. void subscribe(QString ch, redisCallbackFn *fn, void* data = nullptr); // 订阅
  34. void psubscribe(QString ch, redisCallbackFn *fn, void* priData = nullptr); // 订阅:模式匹配
  35. private:
  36. //QString ip = "192.168.9.56";
  37. QString ip = "127.0.0.1";
  38. quint16 port = 16379;
  39. QString auth = "<LanPeng&&CeKong>";
  40. bool redisOk = false; //redis全局状态,操作出错时置为false, redis线程内会自动重连
  41. redisContext *rc; //同步连接
  42. redisAsyncContext *rac = nullptr; //异步连接
  43. RedisQtAdapter adapter; //异步事件驱动适配器
  44. public:
  45. void Setup(std::string addr, uint port, std::string password);
  46. };