TreeFrogFramework  2.8
tredis.h
Go to the documentation of this file.
1 #pragma once
2 #include <QByteArray>
3 #include <QStringList>
4 #include <QVariant>
5 #include <TGlobal>
6 #include <TKvsDatabase>
7 #include <TfNamespace>
8 
9 class TRedisDriver;
10 
11 
13 public:
14  TRedis();
15  virtual ~TRedis() { }
16 
17  bool isOpen() const;
18  bool exists(const QByteArray &key);
19 
20  // binary
21  QByteArray get(const QByteArray &key);
22  bool set(const QByteArray &key, const QByteArray &value);
23  bool setEx(const QByteArray &key, const QByteArray &value, int seconds);
24  bool setNx(const QByteArray &key, const QByteArray &value);
25  QByteArray getSet(const QByteArray &key, const QByteArray &value);
26 
27  // string
28  QString gets(const QByteArray &key);
29  bool sets(const QByteArray &key, const QString &value);
30  bool setsEx(const QByteArray &key, const QString &value, int seconds);
31  bool setsNx(const QByteArray &key, const QString &value);
32  QString getsSets(const QByteArray &key, const QString &value);
33 
34  bool del(const QByteArray &key);
35  int del(const QByteArrayList &keys);
36 
37  // binary list
38  int rpush(const QByteArray &key, const QByteArrayList &values);
39  int lpush(const QByteArray &key, const QByteArrayList &values);
40  QByteArrayList lrange(const QByteArray &key, int start, int end);
41  QByteArray lindex(const QByteArray &key, int index);
42 
43  // string list
44  int rpushs(const QByteArray &key, const QStringList &values);
45  int lpushs(const QByteArray &key, const QStringList &values);
46  QStringList lranges(const QByteArray &key, int start, int end);
47  QString lindexs(const QByteArray &key, int index);
48  int llen(const QByteArray &key);
49 
50  // hash
51  bool hset(const QByteArray &key, const QByteArray &field, const QByteArray &value);
52  bool hsetNx(const QByteArray &key, const QByteArray &field, const QByteArray &value);
53  bool hsets(const QByteArray &key, const QByteArray &field, const QString &value);
54  bool hsetsNx(const QByteArray &key, const QByteArray &field, const QString &value);
55  QByteArray hget(const QByteArray &key, const QByteArray &field);
56  QString hgets(const QByteArray &key, const QByteArray &field);
57  bool hexists(const QByteArray &key, const QByteArray &field);
58  bool hdel(const QByteArray &key, const QByteArray &field);
59  int hdel(const QByteArray &key, const QByteArrayList &fields);
60  int hlen(const QByteArray &key);
61  QList<QPair<QByteArray, QByteArray>> hgetAll(const QByteArray &key);
62 
63  void flushDb();
64 
65 private:
66  TRedis(Tf::KvsEngine engine);
67  TRedisDriver *driver();
68  const TRedisDriver *driver() const;
69 
70  static QByteArrayList toByteArrayList(const QStringList &values);
71  static QStringList toStringList(const QByteArrayList &values);
72 
73  TKvsDatabase _database;
74 
75  friend class TCacheRedisStore;
78 };
79 
80 
81 inline QString TRedis::gets(const QByteArray &key)
82 {
83  return QString::fromUtf8(get(key));
84 }
85 
86 inline bool TRedis::sets(const QByteArray &key, const QString &value)
87 {
88  return set(key, value.toUtf8());
89 }
90 
91 inline bool TRedis::setsEx(const QByteArray &key, const QString &value, int seconds)
92 {
93  return setEx(key, value.toUtf8(), seconds);
94 }
95 
96 inline bool TRedis::setsNx(const QByteArray &key, const QString &value)
97 {
98  return setNx(key, value.toUtf8());
99 }
100 
101 inline QString TRedis::getsSets(const QByteArray &key, const QString &value)
102 {
103  return QString::fromUtf8(getSet(key, value.toUtf8()));
104 }
105 
106 inline int TRedis::rpushs(const QByteArray &key, const QStringList &values)
107 {
108  return rpush(key, toByteArrayList(values));
109 }
110 
111 inline int TRedis::lpushs(const QByteArray &key, const QStringList &values)
112 {
113  return lpush(key, toByteArrayList(values));
114 }
115 
116 inline QStringList TRedis::lranges(const QByteArray &key, int start, int end = -1)
117 {
118  return toStringList(lrange(key, start, end));
119 }
120 
121 inline QString TRedis::lindexs(const QByteArray &key, int index)
122 {
123  return QString::fromUtf8(lindex(key, index));
124 }
125 
126 inline bool TRedis::hsets(const QByteArray &key, const QByteArray &field, const QString &value)
127 {
128  return hset(key, field, value.toUtf8());
129 }
130 
131 inline bool TRedis::hsetsNx(const QByteArray &key, const QByteArray &field, const QString &value)
132 {
133  return hsetNx(key, field, value.toUtf8());
134 }
135 
136 inline QString TRedis::hgets(const QByteArray &key, const QByteArray &field)
137 {
138  return QString::fromUtf8(hget(key, field));
139 }
140 
The TRedis class provides a means of operating a Redis system.
Definition: tredis.h:12
QString hgets(const QByteArray &key, const QByteArray &field)
Definition: tredis.h:136
QString gets(const QByteArray &key)
Returns the string associated with the key; otherwise returns a null string.
Definition: tredis.h:81
bool setNx(const QByteArray &key, const QByteArray &value)
Set the key to hold the value if key does not exist.
Definition: tredis.cpp:161
QByteArray getSet(const QByteArray &key, const QByteArray &value)
Atomically sets the key to the value and returns the old value stored at the key.
Definition: tredis.cpp:177
QString lindexs(const QByteArray &key, int index)
Returns the string at the index in the list stored at the key.
Definition: tredis.h:121
bool setsNx(const QByteArray &key, const QString &value)
Definition: tredis.h:96
int lpush(const QByteArray &key, const QByteArrayList &values)
Inserts all the values at the tail of the list stored at the key.
Definition: tredis.cpp:238
virtual ~TRedis()
Definition: tredis.h:15
bool hsets(const QByteArray &key, const QByteArray &field, const QString &value)
Definition: tredis.h:126
int lpushs(const QByteArray &key, const QStringList &values)
Inserts all the string values at the tail of the list stored at the key.
Definition: tredis.h:111
bool set(const QByteArray &key, const QByteArray &value)
Sets the key to hold the value.
Definition: tredis.cpp:130
QByteArray get(const QByteArray &key)
Returns the value associated with the key; otherwise returns an empty byte array.
Definition: tredis.cpp:114
QString getsSets(const QByteArray &key, const QString &value)
Atomically sets the key to the string value and returns the old string value stored at the key.
Definition: tredis.h:101
QStringList lranges(const QByteArray &key, int start, int end)
Returns the specified elements of the list stored at the key.
Definition: tredis.h:116
bool setEx(const QByteArray &key, const QByteArray &value, int seconds)
Sets the key to hold the value and set the key to timeout after a given number of seconds.
Definition: tredis.cpp:145
bool sets(const QByteArray &key, const QString &value)
Sets the key to hold the string value.
Definition: tredis.h:86
int rpushs(const QByteArray &key, const QStringList &values)
Inserts all the string values at the tail of the list stored at the key.
Definition: tredis.h:106
bool setsEx(const QByteArray &key, const QString &value, int seconds)
Sets the key to hold the string value and set the key to timeout after a given number of seconds.
Definition: tredis.h:91
bool hsetNx(const QByteArray &key, const QByteArray &field, const QByteArray &value)
Definition: tredis.cpp:336
int rpush(const QByteArray &key, const QByteArrayList &values)
Inserts all the values at the tail of the list stored at the key.
Definition: tredis.cpp:221
QByteArrayList lrange(const QByteArray &key, int start, int end)
Returns the specified elements of the list stored at the key.
Definition: tredis.cpp:254
bool hsetsNx(const QByteArray &key, const QByteArray &field, const QString &value)
Definition: tredis.h:131
QByteArray hget(const QByteArray &key, const QByteArray &field)
Definition: tredis.cpp:349
QByteArray lindex(const QByteArray &key, int index)
Returns the element at the index in the list stored at the key.
Definition: tredis.cpp:275
bool hset(const QByteArray &key, const QByteArray &field, const QByteArray &value)
Definition: tredis.cpp:323
KvsEngine
Definition: tfnamespace.h:239
#define T_CORE_EXPORT
Definition: tdeclexport.h:28
#define T_DISABLE_COPY(Class)
Definition: tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition: tdeclexport.h:41