Redis.php 8.71 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
namespace App;
/**
 * Very simple Redis implementation, all commands passed in cli format
 * Add commands via cmd ( $command [, $variable1 [, $variable2 ] ] ) method
 * Fire commands via get () o set () methods ( first one will return output, usefull for get operations )
 *
 * Usage:
 * $redis = new redis_cli ( '127.0.0.1', 6379 );
 * $redis -> cmd ( 'SET', 'foo', 'bar' ) -> set ();
 * $foo = $redis -> cmd ( 'GET', 'foo' ) -> get ();
 *
 * $redis -> cmd ( 'HSET', 'hash', 'foo', 'bar' ) -> cmd ( 'HSET', 'hash', 'abc', 'def' ) -> set ();
 * $vals = $redis -> cmd ( 'HVALS', 'hash' ) -> get ();
 *
 * $redis -> cmd ( 'KEYS', 'online*' );
 * $total_online = $redis -> get_len ();
 *
 * Based on http://redis.io/topics/protocol
 */
class Redis
{
    const INTEGER = ':';
    const INLINE = '+';
    const BULK = '$';
    const MULTIBULK = '*';
    const ERROR = '-';
    const NL = "\r\n";

    private $handle = false;
    private $host;
    private $port;
    private $silent_fail;

    private $commands = array ();

    //Timeout for stream, 30 seconds
    private $timeout = 30;

    //Timeout for socket connection
    private $connect_timeout = 3;

    //Use this with extreme caution
    private $force_reconnect = false;

    //Error handling, debug info
    private $last_used_command = '';

    //Error handling function, use set_error_function method ()
    private $error_function = null;

    public function __construct ( $host = false, $port = false, $silent_fail = false, $timeout = 60 )
    {
        if ( $host && $port )
        {
            $this -> connect ( $host, $port, $silent_fail, $timeout );
        }
    }

    //Main method to establish connection
    public function connect ( $host = '127.0.0.1', $port = 6379, $silent_fail = false, $timeout = 60 )
    {
        $this -> host = $host;
        $this -> port = $port;
        $this -> silent_fail = $silent_fail;
        $this -> timeout = $timeout;

        if ( $silent_fail )
        {
            $this -> handle = @fsockopen ( $host, $port, $errno, $errstr, $this -> connect_timeout );

            if ( !$this ->  handle )
            {
                $this -> handle = false;
            }
        }
        else
        {
            $this -> handle = fsockopen ( $host, $port, $errno, $errstr, $this -> connect_timeout );
        }

        if ( is_resource ( $this -> handle ) )
        {
            stream_set_timeout ( $this -> handle, $this -> timeout );
        }
    }

    public function reconnect (  )
    {
        $this -> __destruct ();
        $this -> connect ( $this -> host, $this -> port, $this -> silent_fail );
    }

    public function __destruct ()
    {
        if ( is_resource ( $this -> handle ) )
        {
            fclose ( $this -> handle );
        }
    }

    //Returns all commands array
    public function commands ()
    {
        return $this -> commands;
    }

    //Used to push single command to queue
    public function cmd ()
    {
        if ( !$this -> handle )
        {
            return $this;
        }

        $args = func_get_args ();
        $rlen = count ( $args );

        $output = '*'. $rlen . self::NL;

        foreach ( $args as $arg )
        {
            $output .= '$'. strlen ( $arg ) . self::NL . $arg . self::NL;
        }

        $this -> commands [] = $output;

        return $this;
    }

    //Used to push many commands at once, almost always for setting something
    public function set ()
    {
        if ( !$this -> handle )
        {
            return false;
        }

        //Total size of commands
        $size = $this -> exec ();
        $response = array ();

        for ( $i=0; $i<$size; $i++ )
        {
            $response [] = $this -> get_response ();
        }

        if ( $this -> force_reconnect )
        {
            $this -> reconnect ();
        }

        return $response;
    }

    //Used to get command response
    public function get ( $line = false )
    {
        if ( !$this -> handle )
        {
            return false;
        }

        $return = false;

        if ( $this -> exec () )
        {
            $return = $this -> get_response ();

            if ( $this -> force_reconnect )
            {
                $this -> reconnect ();
            }

        }

        return $return;
    }

    //Used to get length of the returned array. Most useful with `Keys` command
    public function get_len ()
    {
        if ( !$this -> handle )
        {
            return false;
        }

        $return = null;

        if ( $this -> exec () )
        {
            $char = fgetc ( $this -> handle );

            if ( $char == self::BULK )
            {
                $return = sizeof ( $this -> bulk_response () );
            }
            elseif ( $char == self::MULTIBULK )
            {
                $return = sizeof ( $this -> multibulk_response () );
            }

            if ( $this -> force_reconnect )
            {
                $this -> reconnect ();
            }
        }

        return $return;
    }

    //Forces to reconnect after every get() or set(). Use this with extreme caution
    public function set_force_reconnect ( $flag )
    {
        $this -> force_reconnect = $flag;
        return $this;
    }

    //Used to parse single command single response
    private function get_response ()
    {
        $return = false;

        $char = fgetc ( $this -> handle );

        switch ( $char )
        {
            case self::INLINE:
                $return = $this -> inline_response ();
                break;
            case self::INTEGER:
                $return = $this -> integer_response ();
                break;
            case self::BULK:
                $return = $this -> bulk_response ();
                break;
            case self::MULTIBULK:
                $return = $this -> multibulk_response ();
                break;
            case self::ERROR:
                $return = $this -> error_response ();
                break;
        }

        return $return;
    }

    //For inline responses only
    private function inline_response ()
    {
        return trim ( fgets ( $this -> handle ) );
    }

    //For integer responses only
    private function integer_response ()
    {
        return ( int ) trim ( fgets ( $this -> handle ) );
    }

    //For error responses only
    private function error_response ()
    {
        $error = fgets ( $this -> handle );

        if ( $this -> error_function )
        {
            call_user_func ( $this -> error_function, $error .'('. $this -> last_used_command .')' );
        }

        return false;
    }

    //For bulk responses only
    private function bulk_response ()
    {
        $return = trim ( fgets ( $this -> handle ) );

        if ( $return === '-1' )
        {
            $return = null;
        }
        else
        {
            $return = $this -> read_bulk_response ( $return );
        }

        return $return;
    }

    //For multibulk responses only
    private function multibulk_response ()
    {
        $size = trim ( fgets ( $this -> handle ) );
        $return = false;

        if ( $size === '-1' )
        {
            $return = null;
        }
        else
        {
            $return = array ();

            for ( $i = 0; $i < $size; $i++ )
            {
                $tmp = trim ( fgets ( $this -> handle ) );

                if ( $tmp === '-1' )
                {
                    $return [] = null;
                }
                else
                {
                    $return [] = $this -> read_bulk_response ( $tmp );
                }
            }
        }

        return $return;
    }

    //Sends command to the redis
    private function exec ()
    {
        $size = sizeof ( $this -> commands );

        if ( $size < 1 )
        {
            return null;
        }

        if ( $this -> error_function )
        {
            $this -> last_used_command = str_replace ( self::NL, '\\r\\n', implode ( ';', $this -> commands ) );
        }

        $command = implode ( self::NL, $this -> commands ) . self::NL;
        fwrite ( $this -> handle, $command );

        $this -> commands = array ();
        return $size;
    }

    //Bulk response reader
    private function read_bulk_response ( $tmp )
    {
        $response = null;

        $read = 0;
        $size = ( ( strlen ( $tmp ) > 1 && substr ( $tmp, 0, 1 ) === self::BULK ) ? substr ( $tmp, 1 ) : $tmp );

        while ( $read < $size )
        {
            $diff = $size - $read;

            $block_size = $diff > 8192 ? 8192 : $diff;

            $response .= fread ( $this -> handle, $block_size );
            $read += $block_size;
        }

        fgets ( $this -> handle );

        return $response;
    }

    public function set_error_function ( $func )
    {
        $this -> error_function = $func;
    }
}