@@ -106,39 +106,35 @@ the reply is usually reported as an integer.
106
106
// Add some values to a set.
107
107
redisReply *reply = redisCommand(c, " SADD items bread milk peas" );
108
108
109
- if (reply != NULL ) {
110
- // This gives an integer reply.
111
- if (reply->type == REDIS_REPLY_INTEGER) {
112
- // Report status.
113
- printf ("Integer reply\n");
114
- printf("Number added: %lld\n", reply->integer);
115
- // >>> Number added: 3
116
- }
117
-
118
- freeReplyObject (reply);
119
- reply = NULL;
109
+ if (reply->type == REDIS_REPLY_INTEGER) {
110
+ // Report status.
111
+ printf ("Integer reply\n");
112
+ printf("Number added: %lld\n", reply->integer);
113
+ // >>> Number added: 3
120
114
}
121
115
116
+ freeReplyObject (reply);
117
+ reply = NULL;
118
+
119
+
122
120
reply = redisCommand(c, "SISMEMBER items bread");
123
121
124
- if (reply != NULL ) {
125
- // This also gives an integer reply but you should interpret
126
- // it as a boolean value.
127
- if (reply->type == REDIS_REPLY_INTEGER) {
128
- // Respond to boolean integer value.
129
- printf ("Integer reply\n");
130
-
131
- if (reply->integer == 0) {
132
- printf("Items set has no member 'bread'\n");
133
- } else {
134
- printf("'Bread' is a member of items set\n");
135
- }
136
- // >>> 'Bread' is a member of items set
122
+ // This also gives an integer reply but you should interpret
123
+ // it as a boolean value.
124
+ if (reply->type == REDIS_REPLY_INTEGER) {
125
+ // Respond to boolean integer value.
126
+ printf("Integer reply\n");
127
+
128
+ if (reply->integer == 0) {
129
+ printf("Items set has no member 'bread'\n");
130
+ } else {
131
+ printf("'Bread' is a member of items set\n");
137
132
}
138
-
139
- freeReplyObject (reply);
140
- reply = NULL;
133
+ // >>> 'Bread' is a member of items set
141
134
}
135
+
136
+ freeReplyObject(reply);
137
+ reply = NULL;
142
138
```
143
139
144
140
### Strings
@@ -154,81 +150,75 @@ returned in `reply->str` and the length of the string is in
154
150
// Set a numeric value in a string.
155
151
reply = redisCommand(c, "SET number 1.5");
156
152
157
- if (reply != NULL ) {
158
- // This gives a status reply.
159
- if (reply->type == REDIS_REPLY_STATUS) {
160
- // Report status.
161
- printf ("Status reply\n");
162
- printf("Reply: %s\n", reply->str); // >>> Reply: OK
163
- }
164
-
165
- freeReplyObject (reply);
166
- reply = NULL;
153
+ // This gives a status reply.
154
+ if (reply->type == REDIS_REPLY_STATUS) {
155
+ // Report status.
156
+ printf("Status reply\n");
157
+ printf("Reply: %s\n", reply->str); // >>> Reply: OK
167
158
}
168
159
160
+ freeReplyObject(reply);
161
+ reply = NULL;
162
+
163
+
169
164
// Attempt to interpret the key as a hash.
170
165
reply = redisCommand(c, "HGET number field1");
171
166
172
- if (reply != NULL ) {
173
- // This gives an error reply.
174
- if (reply->type == REDIS_REPLY_ERROR) {
175
- // Report the error.
176
- printf ("Error reply\n");
177
- printf("Reply: %s\n", reply->str);
178
- // >>> Reply: WRONGTYPE Operation against a key holding the wrong kind of value
179
- }
180
-
181
- freeReplyObject (reply);
182
- reply = NULL;
167
+ // This gives an error reply.
168
+ if (reply->type == REDIS_REPLY_ERROR) {
169
+ // Report the error.
170
+ printf("Error reply\n");
171
+ printf("Reply: %s\n", reply->str);
172
+ // >>> Reply: WRONGTYPE Operation against a key holding the wrong kind of value
183
173
}
184
174
185
- reply = redisCommand(c, " GET number" );
175
+ freeReplyObject(reply);
176
+ reply = NULL;
186
177
187
- if (reply != NULL ) {
188
- // This gives a simple string reply.
189
- if (reply->type == REDIS_REPLY_STRING) {
190
- // Display the string.
191
- printf ("Simple string reply\n");
192
- printf("Reply: %s\n", reply->str); // >>> Reply: 1.5
193
- }
194
178
195
- freeReplyObject (reply);
196
- reply = NULL;
179
+ reply = redisCommand(c, "GET number");
180
+
181
+ // This gives a simple string reply.
182
+ if (reply->type == REDIS_REPLY_STRING) {
183
+ // Display the string.
184
+ printf("Simple string reply\n");
185
+ printf("Reply: %s\n", reply->str); // >>> Reply: 1.5
197
186
}
198
187
199
- reply = redisCommand(c, " ZADD prices 1.75 bread 5.99 beer" );
188
+ freeReplyObject(reply);
189
+ reply = NULL;
200
190
201
- if (reply != NULL ) {
202
- // This gives an integer reply.
203
- if (reply->type == REDIS_REPLY_INTEGER) {
204
- // Display the integer.
205
- printf ("Integer reply\n");
206
- printf("Number added: %lld\n", reply->integer);
207
- // >>> Number added: 2
208
- }
209
191
210
- freeReplyObject (reply);
211
- reply = NULL;
192
+ reply = redisCommand(c, "ZADD prices 1.75 bread 5.99 beer");
193
+
194
+ // This gives an integer reply.
195
+ if (reply->type == REDIS_REPLY_INTEGER) {
196
+ // Display the integer.
197
+ printf("Integer reply\n");
198
+ printf("Number added: %lld\n", reply->integer);
199
+ // >>> Number added: 2
212
200
}
213
201
214
- reply = redisCommand(c, " ZSCORE prices bread" );
202
+ freeReplyObject(reply);
203
+ reply = NULL;
215
204
216
- if (reply != NULL ) {
217
- // This gives a string reply with RESP2 and a double reply
218
- // with RESP3, but you handle it the same way in either case.
219
- if (reply->type == REDIS_REPLY_STRING) {
220
- printf ("String reply\n");
221
-
222
- char * endptr; // Not used.
223
- double price = strtod(reply->str, &endptr);
224
- double discounted = price * 0.75;
225
- printf("Discounted price: %.2f\n", discounted);
226
- // >>> Discounted price: 1.31
227
- }
228
205
229
- freeReplyObject (reply);
230
- reply = NULL;
206
+ reply = redisCommand(c, "ZSCORE prices bread");
207
+
208
+ // This gives a string reply with RESP2 and a double reply
209
+ // with RESP3, but you handle it the same way in either case.
210
+ if (reply->type == REDIS_REPLY_STRING) {
211
+ printf("String reply\n");
212
+
213
+ char *endptr; // Not used.
214
+ double price = strtod(reply->str, &endptr);
215
+ double discounted = price * 0.75;
216
+ printf("Discounted price: %.2f\n", discounted);
217
+ // >>> Discounted price: 1.31
231
218
}
219
+
220
+ freeReplyObject(reply);
221
+ reply = NULL;
232
222
```
233
223
234
224
### Arrays and maps
@@ -246,26 +236,23 @@ The example below shows how to get the items from a
246
236
``` c
247
237
reply = redisCommand(c, " RPUSH things thing0 thing1 thing2 thing3" );
248
238
249
- if (reply != NULL ) {
250
- printf ("Added %lld items\n", reply->integer);
251
- // >>> Added 4 items
252
- freeReplyObject(reply);
253
- reply = NULL;
254
- }
239
+ printf ("Added %lld items\n", reply->integer);
240
+ // >>> Added 4 items
241
+ freeReplyObject(reply);
242
+ reply = NULL;
243
+
255
244
256
245
reply = redisCommand(c, "LRANGE things 0 -1");
257
246
258
- if (reply != NULL ) {
259
- for (int i = 0; i < reply->elements; ++i) {
260
- if (reply->element[i]->type == REDIS_REPLY_STRING) {
261
- printf ("List item %d: %s\n", i, reply->element[ i] ->str);
262
- }
247
+ for (int i = 0; i < reply->elements; ++i) {
248
+ if (reply->element[ i] ->type == REDIS_REPLY_STRING) {
249
+ printf("List item %d: %s\n", i, reply->element[ i] ->str);
263
250
}
264
- // >>> List item 0: thing0
265
- // >>> List item 1: thing1
266
- // >>> List item 2: thing2
267
- // >>> List item 3: thing3
268
251
}
252
+ // >>> List item 0: thing0
253
+ // >>> List item 1: thing1
254
+ // >>> List item 2: thing2
255
+ // >>> List item 3: thing3
269
256
```
270
257
271
258
A map is essentially the same as an array but it has the extra
@@ -284,13 +271,12 @@ const char *hashCommand[] = {
284
271
285
272
reply = redisCommandArgv(c, 8, hashCommand, NULL);
286
273
287
- if (reply != NULL ) {
288
- printf ("Added %lld fields\n", reply->integer);
289
- // >>> Added 3 fields
274
+ printf("Added %lld fields\n", reply->integer);
275
+ // >>> Added 3 fields
276
+
277
+ freeReplyObject(reply);
278
+ reply = NULL;
290
279
291
- freeReplyObject (reply);
292
- reply = NULL;
293
- }
294
280
295
281
reply = redisCommand(c, "HGETALL details");
296
282
0 commit comments