Redis4Qt  1.0.0
Redis API for Qt framework
lht_redis.h
1 #ifndef LHT_REDIS_H
2 #define LHT_REDIS_H
3 
4 #include <QList>
5 #include <QMutex>
6 #include <QThread>
7 #include <lht_errors.h>
8 #include "lht_redis_i.h"
9 #include "lht_redis_core.h"
10 
11 class QTimer;
12 class LHNodeInfo;
13 class ctkPluginContext;
14 class LHLoggerInterface;
15 class LHSettingsInterface;
16 
18 {
19  QMutex* lock;
20  QThread* thread;
21  LHRedisCore* core;
22 };
23 
24 class LHRedis : public LHRedisInterface
25 {
26  Q_OBJECT
27  Q_INTERFACES(LHRedisInterface)
28 public:
29  LHRedis();
30  ~LHRedis();
31 
32  virtual int Init(const LHParameters &Parameters);
33  virtual int Uninit();
34  virtual int CreateInstance(QVariant &varInstance, const LHParameters &Parameters);
35  virtual int DestroyInstance(const QVariant &varInstance);
36  virtual void Upgrade()
37  {
38 
39  }
40 
41 public:
48  int key_del(QString key, bool *error = NULL);
49 
56  QString key_dump(QString key, bool *error = NULL);
57 
64  bool key_exists(QString key, bool *error = NULL);
65 
73  bool key_expire(QString key,int seconds, bool *error = NULL);
74 
82  bool key_expireat(QString key, qint64 timestamp, bool *error = NULL);
83 
90  QStringList keys(QString pattern, bool *error = NULL);
91 
98  int key_pttl(QString key, bool *error = NULL);
99 
106  int key_ttl(QString key, bool *error = NULL);
114  bool key_rename(QString key, QString newkey, bool *error = NULL);
115 
122  QString key_type(QString key, bool *error = NULL);
123 
124 
132  bool string_set(QString key, QString value, bool *error = NULL);
133 
140  QString string_get(QString key, bool *error = NULL);
141 
150  QString string_getrange(QString key, int start, int stop, bool *error = NULL);
151 
158  QStringList string_mget(QStringList keys, bool *error = NULL);
159 
166  bool string_mset(QMap<QString, QVariant> map, bool *error = NULL);
167 
174  int string_incr(QString key, bool *error = NULL);
175 
183  int string_incrby(QString key,int interval, bool *error = NULL);
184 
191  int string_decr(QString key, bool *error = NULL);
192 
200  int string_decrby(QString key,int interval, bool *error = NULL);
201 
209  int string_append(QString key, QString value, bool *error = NULL);
210 
211 
219  QStringList list_blpop(QString key, int timeout, bool *error = NULL);
220 
228  QStringList list_brpop(QString key, int timeout, bool *error = NULL);
229 
238  QStringList list_brpoplpush(QString source, QString destination, int timeout, bool *error = NULL);
239 
247  QString list_index(QString key, int index, bool *error = NULL);
248 
259  int list_insert(QString key, QString position, QString pivot, QString value, bool *error = NULL);
260 
267  int list_len(QString key, bool *error = NULL);
268 
275  QString list_lpop(QString key, bool *error = NULL);
276 
284  int list_lpush(QString key, QString value, bool *error = NULL);
285 
293  int list_lpush(QString key, QStringList values, bool *error = NULL);
294 
302  int list_lpushx(QString key, QStringList values, bool *error = NULL);
303 
313  QStringList list_range(QString key, int start, int end, bool *error = NULL);
314 
327  int list_rem(QString key, int count, QString value, bool *error = NULL);
328 
338  int list_set(QString key, int index, QString value, bool *error = NULL);
339 
350  bool list_trim(QString key, int start, int stop, bool *error = NULL);
351 
358  QString list_rpop(QString key, bool *error = NULL);
359 
367  QString list_rpoplpush(QString source, QString destination, bool *error = NULL);
368 
376  int list_rpush(QString key, QString value, bool *error = NULL);
377 
385  int list_rpush(QString key, QStringList values, bool *error = NULL);
386 
394  int list_rpushx(QString key, QStringList values, bool *error = NULL);
395 
396 
406  int set_add(QString key, QString member, bool *error = NULL);
407 
417  int set_add(QString key, QStringList members, bool *error = NULL);
418 
425  int set_card(QString key, bool *error = NULL);
426 
433  QStringList set_diff(QStringList keys, bool *error = NULL);
434 
442  int set_diffstore(QString destination, QStringList keys, bool *error = NULL);
443 
450  QStringList set_inter(QStringList keys, bool *error = NULL);
451 
459  int set_interstore(QString destination, QStringList keys, bool *error = NULL);
460 
468  bool set_ismember(QString key, QString member, bool *error = NULL);
469 
476  QStringList set_members(QString key, bool *error = NULL);
477 
490  bool set_move(QString source, QString destination, QString member, bool *error = NULL);
491 
498  QString set_pop(QString key, bool *error = NULL);
499 
507  virtual QStringList set_randmember(QString key, bool *error = NULL) = 0;
508 
519  QStringList set_randmember(QString key, bool *error = NULL);
520 
528  int set_rem(QString key, QString member, bool *error = NULL);
529 
537  int set_rem(QString key, QStringList members, bool *error = NULL);
538 
545  QStringList set_union(QStringList keys, bool *error = NULL);
546 
554  int set_unionstore(QString destination, QStringList keys, bool *error = NULL);
555 
556 
564  int hash_del(QString key, QString field, bool *error = NULL);
565 
573  int hash_del(QString key, QStringList fields, bool *error = NULL);
574 
582  bool hash_exists(QString key, QString field, bool *error = NULL);
583 
591  QString hash_get(QString key, QString field, bool *error = NULL);
592 
599  QVariantMap hash_getall(QString key, bool *error = NULL);
600 
614  int hash_incrby(QString key, QString field, int increment, bool *error = NULL);
615 
624  float hash_incrbyfloat(QString key, QString field, float increment, bool *error = NULL);
625 
632  QStringList hash_keys(QString key, bool *error = NULL);
633 
640  int hash_len(QString key, bool *error = NULL);
641 
649  QStringList hash_mget(QString key, QStringList fields, bool *error = NULL);
650 
658  bool hash_mset(QString key, QMap<QString,QVariant> map, bool *error = NULL);
659 
668  bool hash_set(QString key, QString field, QString value, bool *error = NULL);
669 
681  bool hash_setnx(QString key, QString field, QString value, bool *error = NULL);
682 
689  QStringList hash_values(QString key, bool *error = NULL);
690 
697  void publish(QString channel, QString message, bool *error = NULL);
698 
704  void subscribe(QString channel, bool *error = NULL);
705 
711  void unsubscribe(QString channel, bool *error = NULL);
712 
719  void psubscribe(QString channel, bool *error = NULL);
720 
726  void punsubscribe(QString channel, bool *error = NULL);
727 
728 public:
733  int getLastError();
734 public:
735  enum FuncType
736  {
737  //<1> key操作
738  KEY_DEL = 100,
739  KEY_DUMP,
740  KEY_EXISTS,
741  KEY_EXPIRE,
742  KEY_EXPIRE_AT,
743  KEYS,
744  KEY_PTTL,
745  KEY_TTL,
746  KEY_RENAME,
747  KEY_TYPE,
748 
749  //<2> string操作
750  STRING_SET = 200,
751  STRING_GET,
752  STRING_GET_RANGE,
753  STRING_MGET,
754  STRING_MSET,
755  STRING_INCR,
756  STRING_INCRBY,
757  STRING_DECR,
758  STRING_DECRBY,
759  STRING_APPEND,
760 
761  //<3> list操作
762  LIST_BLPOP = 300,
763  LIST_BRPOP,
764  LIST_BRPOPLPUSH,
765  LIST_INDEX,
766  LIST_INSERT,
767  LIST_LEN,
768  LIST_LPOP,
769  LIST_LPUSH,
770  LIST_LPUSHX,
771  LIST_RANGE,
772  LIST_REMOVE,
773  LIST_SET,
774  LIST_TRIM,
775  LIST_RPOP,
776  LIST_RPOPLPUSH,
777  LIST_RPUSH,
778  LIST_RPUSHX,
779 
780  //<4> set操作
781  SET_ADD = 400,
782  SET_CARD,
783  SET_DIFF,
784  SET_DIFFSTORE,
785  SET_INTER,
786  SET_INTERSTORE,
787  SET_ISMEMBER,
788  SET_MEMBERS,
789  SET_MOVE,
790  SET_POP,
791  SET_RANDMEMBER,
792  SET_REMOVE,
793  SET_UNION,
794  SET_UNIONSTORE,
795 
796  //<5> hash操作
797  HASH_DEL = 500,
798  HASH_EXISTS,
799  HASH_GET,
800  HASH_GETALL,
801  HASH_INCRBY,
802  HASH_INCRBYFLOAT,
803  HASH_KEYS,
804  HASH_LEN,
805  HASH_MGET,
806  HASH_MSET,
807  HASH_SET,
808  HASH_SETNX,
809  HASH_VALUES,
810 
811  //<6> 订阅发布
812  PUBLISH = 600,
813  SUBSCRIBE,
814  UNSUBSCRIBE,
815  PSUBSCRIBE,
816  PUNSUBSCRIBE
817  };
818 
819 
820 private:
821  LHRedisCore *getRedisInstance();
822  bool validKey(QString &key, QString funcName, bool *error);
823  bool validKeys(QStringList &key, QString funcName, bool *error);
824  void setLastError(int error);
825  QVariant dealwithCmd(FuncType type, QVariantList &data, bool *error);
826  QVariant excuteCmd(FuncType type, LHRedisCore* redis, QVariantList &data);
827 
828 private:
829 
830  bool m_bInit;
831  int m_nInstance;
832  int m_nSettingsInstance;
833 
834  QString m_redisHost;
835  qint16 m_redisPort;
836  int m_redisCmdTimeout;
837 
838  QMap<void *, ThreadRedis> m_redis;
839 
840  LHSettingsInterface *m_Settings;
841  LHLoggerInterface *m_Log4qt;
842  ctkPluginContext *m_PluginContext;
843 
844  QMutex m_Lock;
845 };
846 
847 #endif // LHT_REDIS_H
Definition: lht_redis.h:24
Definition: lht_redis.h:17