@@ -21,19 +21,19 @@ import Foundation
21
21
/// The `Redis` class represents a handle for issueing commands to a Redis server.
22
22
/// It provides a set of type safe functions for issueing those commands.
23
23
public class Redis {
24
-
24
+
25
25
/// Redis Serialization Protocol handle
26
26
public var respHandle : RedisResp ?
27
-
27
+
28
28
/// Whether the client is connected or not.
29
29
/// Does not reflect state changes in the event of a disconnect.
30
30
public var connected : Bool {
31
31
return respHandle? . status == . connected
32
32
}
33
-
33
+
34
34
/// Initializes a `Redis` instance
35
35
public init ( ) { }
36
-
36
+
37
37
/// Connects to a redis server
38
38
///
39
39
/// - Parameter host: the server IP address.
@@ -52,14 +52,14 @@ public class Redis {
52
52
public func disconnect( ) {
53
53
respHandle? . disconnect ( )
54
54
}
55
-
55
+
56
56
/// Authenticate against the server
57
57
///
58
58
/// - Parameter pswd: String for the password.
59
59
/// - Parameter callback: callback function that is called after authenticating,
60
60
/// NSError will be nil if successful.
61
61
public func auth( _ pswd: String , callback: ( NSError ? ) -> Void ) {
62
-
62
+
63
63
issueCommand ( " AUTH " , pswd) { ( response: RedisResponse ) in
64
64
let ( _, error) = self . redisOkResponseHandler ( response, nilOk: false )
65
65
callback ( error)
@@ -83,20 +83,20 @@ public class Redis {
83
83
/// - Parameter callback: callback function for after the database is selected,
84
84
/// NSError will be nil if successful.
85
85
public func select( _ db: Int , callback: ( NSError ? ) -> Void ) {
86
-
86
+
87
87
issueCommand ( " SELECT " , String ( db) ) { ( response: RedisResponse ) in
88
88
let ( _, error) = self . redisOkResponseHandler ( response, nilOk: false )
89
89
callback ( error)
90
90
}
91
91
}
92
-
92
+
93
93
/// Ping the server to test if a connection is still alive
94
94
///
95
95
/// - Parameter pingStr: String for the ping message.
96
96
/// - Parameter callback: callback function for after the pong is received,
97
97
/// NSError will be nil if successful.
98
98
public func ping( _ pingStr: String ? = nil , callback: ( NSError ? ) -> Void ) {
99
-
99
+
100
100
var command = [ " PING " ]
101
101
if let pingStr = pingStr {
102
102
command. append ( pingStr)
@@ -122,19 +122,19 @@ public class Redis {
122
122
}
123
123
}
124
124
}
125
-
125
+
126
126
/// Echos a message
127
127
///
128
128
/// - Parameter str: String for the message.
129
129
/// - Parameter callback: callback function with the String echoed back,
130
130
/// NSError will be nil if successful.
131
131
public func echo( _ str: String , callback: ( RedisString ? , NSError ? ) -> Void ) {
132
-
132
+
133
133
issueCommand ( " ECHO " , str) { ( response: RedisResponse ) in
134
134
self . redisStringResponseHandler ( response, callback: callback)
135
135
}
136
136
}
137
-
137
+
138
138
/// Get information and statistics about the server
139
139
///
140
140
/// - Parameter callback: callback function with the response as a collection of text
@@ -144,7 +144,7 @@ public class Redis {
144
144
self . redisStringResponseHandler ( response, callback: callback)
145
145
}
146
146
}
147
-
147
+
148
148
/// Get information and statistics about the server
149
149
///
150
150
/// - Parameter callback: callback function with the response as a struct
@@ -155,7 +155,7 @@ public class Redis {
155
155
self . redisDictionaryResponseHandler ( response, callback: callback)
156
156
}
157
157
}
158
-
158
+
159
159
/// Delete all the keys of the currently selected DB. This command never fails.
160
160
///
161
161
/// - Parameter callback: a function returning the response,
@@ -166,30 +166,30 @@ public class Redis {
166
166
callback ( ok, _: error)
167
167
}
168
168
}
169
-
170
-
169
+
170
+
171
171
//
172
172
// MARK: Transaction support
173
173
//
174
-
174
+
175
175
/// Create a `RedisMulti` object in order to perform a Redis transaction
176
176
public func multi( ) -> RedisMulti {
177
177
return RedisMulti ( redis: self )
178
178
}
179
-
180
-
179
+
180
+
181
181
//
182
182
// MARK: Base API functions
183
183
//
184
-
184
+
185
185
/// Issue a Redis command
186
186
///
187
187
/// - Parameter stringArgs: A list of Strings making up the Redis command to issue
188
188
/// - Parameter callback: a function returning the response in the form of a `RedisResponse`
189
189
public func issueCommand( _ stringArgs: String ... , callback: ( RedisResponse ) -> Void ) {
190
190
issueCommandInArray ( stringArgs, callback: callback)
191
191
}
192
-
192
+
193
193
/// Issue a Redis command
194
194
///
195
195
/// - Parameter stringArgs: An array of Strings making up the Redis command to issue
@@ -199,23 +199,23 @@ public class Redis {
199
199
callback ( RedisResponse . Error ( " Not connected to Redis server " ) )
200
200
return
201
201
}
202
-
202
+
203
203
guard stringArgs. count > 0 else {
204
204
callback ( RedisResponse . Error ( " Empty command " ) )
205
205
return
206
206
}
207
-
207
+ print ( " ISSUE " , stringArgs )
208
208
respHandle. issueCommand ( stringArgs, callback: callback)
209
209
}
210
-
210
+
211
211
/// Issue a Redis command
212
212
///
213
213
/// - Parameter stringArgs: A list of `RedisString` objects making up the Redis command to issue
214
214
/// - Parameter callback: a function returning the response in the form of a `RedisResponse`
215
215
public func issueCommand( _ stringArgs: RedisString ... , callback: ( RedisResponse ) -> Void ) {
216
216
issueCommandInArray ( stringArgs, callback: callback)
217
217
}
218
-
218
+
219
219
/// Issue a Redis command
220
220
///
221
221
/// - Parameter stringArgs: An array of `RedisString` objects making up the Redis command to issue
@@ -225,19 +225,19 @@ public class Redis {
225
225
callback ( RedisResponse . Error ( " Not connected to Redis server " ) )
226
226
return
227
227
}
228
-
228
+
229
229
guard stringArgs. count > 0 else {
230
230
callback ( RedisResponse . Error ( " Empty command " ) )
231
231
return
232
232
}
233
-
233
+
234
234
respHandle. issueCommand ( stringArgs, callback: callback)
235
235
}
236
-
236
+
237
237
//
238
238
// MARK: Helper functions
239
239
//
240
-
240
+
241
241
func redisBoolResponseHandler( _ response: RedisResponse , callback: ( Bool , NSError ? ) -> Void ) {
242
242
switch ( response) {
243
243
case . IntegerValue( let num) :
@@ -252,7 +252,7 @@ public class Redis {
252
252
callback ( false , _: createUnexpectedResponseError ( response) )
253
253
}
254
254
}
255
-
255
+
256
256
func redisIntegerResponseHandler( _ response: RedisResponse , callback: ( Int ? , NSError ? ) -> Void ) {
257
257
switch ( response) {
258
258
case . IntegerValue( let num) :
@@ -265,7 +265,7 @@ public class Redis {
265
265
callback ( nil , _: createUnexpectedResponseError ( response) )
266
266
}
267
267
}
268
-
268
+
269
269
func redisOkResponseHandler( _ response: RedisResponse , nilOk: Bool = true ) -> ( Bool , NSError ? ) {
270
270
switch ( response) {
271
271
case . Status( let str) :
@@ -282,7 +282,7 @@ public class Redis {
282
282
return ( false , createUnexpectedResponseError ( response) )
283
283
}
284
284
}
285
-
285
+
286
286
func redisSimpleStringResponseHandler( response: RedisResponse , callback: ( String ? , NSError ? ) -> Void ) {
287
287
switch ( response) {
288
288
case . Status( let str) :
@@ -295,7 +295,7 @@ public class Redis {
295
295
callback ( nil , _: createUnexpectedResponseError ( response) )
296
296
}
297
297
}
298
-
298
+
299
299
func redisStringResponseHandler( _ response: RedisResponse , callback: ( RedisString ? , NSError ? ) -> Void ) {
300
300
switch ( response) {
301
301
case . StringValue( let str) :
@@ -308,11 +308,11 @@ public class Redis {
308
308
callback ( nil , _: createUnexpectedResponseError ( response) )
309
309
}
310
310
}
311
-
311
+
312
312
func redisArrayResponseHandler( response: RedisResponse , callback: ( [ RedisResponse ? ] ? , NSError ? ) -> Void ) {
313
313
var error : NSError ? = nil
314
314
var result : [ RedisResponse ? ] ?
315
-
315
+
316
316
switch ( response) {
317
317
case . Array( let responses) :
318
318
result = responses
@@ -325,7 +325,7 @@ public class Redis {
325
325
}
326
326
callback ( error == nil ? result : nil , _: error)
327
327
}
328
-
328
+
329
329
func getCoordinates( from responses: [ RedisResponse ? ] ? , callback: ( [ ( Double , Double ) ? ] ? , NSError ? ) -> Void ) {
330
330
guard let responses = responses else {
331
331
callback ( nil , createError ( " Could not get coordinates from nil response. " , code: 1 ) )
@@ -347,11 +347,11 @@ public class Redis {
347
347
}
348
348
callback ( result, nil )
349
349
}
350
-
350
+
351
351
func redisStringArrayResponseHandler( _ response: RedisResponse , callback: ( [ RedisString ? ] ? , NSError ? ) -> Void ) {
352
352
var error : NSError ? = nil
353
353
var result : [ RedisString ? ] ?
354
-
354
+
355
355
switch ( response) {
356
356
case . Array( let responses) :
357
357
var strings = [ RedisString? ] ( )
@@ -375,11 +375,11 @@ public class Redis {
375
375
}
376
376
callback ( error == nil ? result : nil , _: error)
377
377
}
378
-
378
+
379
379
func redisStringArrayOrIntegerResponseHandler( _ response: RedisResponse , callback: ( [ RedisString ? ] ? , NSError ? ) -> Void ) {
380
380
var error : NSError ? = nil
381
381
var result : [ RedisString ? ] ?
382
-
382
+
383
383
switch ( response) {
384
384
case . Array( let responses) :
385
385
var strings = [ RedisString? ] ( )
@@ -405,12 +405,12 @@ public class Redis {
405
405
}
406
406
callback ( error == nil ? result : nil , _: error)
407
407
}
408
-
408
+
409
409
func redisScanResponseHandler( _ response: RedisResponse , callback: ( RedisString ? , [ RedisString ? ] ? , NSError ? ) -> Void ) {
410
410
var error : NSError ? = nil
411
411
var cursor : RedisString ?
412
412
var result : [ RedisString ? ] ?
413
-
413
+
414
414
switch ( response) {
415
415
case . Array( let responses) :
416
416
var strings = [ RedisString? ] ( )
@@ -441,14 +441,14 @@ public class Redis {
441
441
default :
442
442
error = self . createUnexpectedResponseError ( response)
443
443
}
444
-
444
+
445
445
if ( error == nil ) {
446
446
callback ( cursor, result, nil )
447
447
} else {
448
448
callback ( nil , nil , error)
449
449
}
450
450
}
451
-
451
+
452
452
func redisDictionaryResponseHandler( _ response: RedisResponse , callback: ( RedisInfo ? , NSError ? ) -> Void ) {
453
453
switch ( response) {
454
454
case . StringValue( let str) :
@@ -461,11 +461,11 @@ public class Redis {
461
461
callback ( nil , _: createUnexpectedResponseError ( response) )
462
462
}
463
463
}
464
-
464
+
465
465
func createUnexpectedResponseError( _ response: RedisResponse ) -> NSError {
466
466
return createError ( " Unexpected result received from Redis \( response) " , code: 2 )
467
467
}
468
-
468
+
469
469
func createError( _ errorMessage: String , code: Int ) -> NSError {
470
470
#if os(Linux)
471
471
let userInfo : [ String : Any ]
@@ -475,7 +475,7 @@ public class Redis {
475
475
userInfo = [ NSLocalizedDescriptionKey: errorMessage]
476
476
return NSError ( domain: " RedisDomain " , code: code, userInfo: userInfo)
477
477
}
478
-
478
+
479
479
func createRedisError( _ redisError: String ) -> NSError {
480
480
return createError ( redisError, code: 1 )
481
481
}
0 commit comments