Redis.h 1.8 KB

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