1 | <?php |
||
40 | class Redis |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * @var Adapter\Socket |
||
45 | */ |
||
46 | protected $socket; |
||
47 | |||
48 | /** |
||
49 | * The database number the connection selects on load. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $database; |
||
54 | |||
55 | /** |
||
56 | * The connection to the Redis database. |
||
57 | * |
||
58 | * @var resource |
||
59 | */ |
||
60 | protected $connection; |
||
61 | |||
62 | /** |
||
63 | * The active Redis database instances. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected static $databases = array(); |
||
68 | |||
69 | /** |
||
70 | * Create a new Redis connection instance. |
||
71 | * |
||
72 | * @param Adapter\Socket $socket |
||
73 | * @param int $database |
||
74 | */ |
||
75 | public function __construct(Adapter\Socket $socket, $database = 0) |
||
80 | |||
81 | /** |
||
82 | * Get a Redis database connection instance. |
||
83 | * |
||
84 | * The given name should correspond to a Redis database in the configuration file. |
||
85 | * |
||
86 | * @param string $name |
||
87 | * |
||
88 | * @return Redis |
||
89 | * @throws \RuntimeException |
||
90 | */ |
||
91 | public static function database($name = 'default') |
||
109 | |||
110 | /** |
||
111 | * Execute a command against the Redis database. |
||
112 | * |
||
113 | * @param string $method |
||
114 | * @param array $parameters |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function run($method, $parameters) |
||
126 | |||
127 | /** |
||
128 | * Parse and return the response from the Redis database. |
||
129 | * |
||
130 | * @param string $response |
||
131 | * |
||
132 | * @return array|string |
||
133 | * @throws \RuntimeException |
||
134 | */ |
||
135 | protected function parse($response) |
||
155 | |||
156 | /** |
||
157 | * Establish the connection to the Redis database. |
||
158 | * |
||
159 | * @return resource |
||
160 | */ |
||
161 | public function connect() |
||
173 | |||
174 | /** |
||
175 | * Build the Redis command based from a given method and parameters. |
||
176 | * |
||
177 | * Redis protocol states that a command should conform to the following format: |
||
178 | * |
||
179 | * *<number of arguments> CR LF |
||
180 | * $<number of bytes of argument 1> CR LF |
||
181 | * <argument data> CR LF |
||
182 | * ... |
||
183 | * $<number of bytes of argument N> CR LF |
||
184 | * <argument data> CR LF |
||
185 | * |
||
186 | * More information regarding the Redis protocol: http://redis.io/topics/protocol |
||
187 | * |
||
188 | * @param string $method |
||
189 | * @param $parameters |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function command($method, $parameters) |
||
205 | |||
206 | /** |
||
207 | * Parse and handle an inline response from the Redis database. |
||
208 | * |
||
209 | * @param string $response |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | protected function inline($response) |
||
217 | |||
218 | /** |
||
219 | * Parse and handle a bulk response from the Redis database. |
||
220 | * |
||
221 | * @param string $head |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | protected function bulk($head) |
||
249 | |||
250 | /** |
||
251 | * Parse and handle a multi-bulk reply from the Redis database. |
||
252 | * |
||
253 | * @param string $head |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | protected function multibulk($head) |
||
272 | |||
273 | /** |
||
274 | * Dynamically make calls to the Redis database. |
||
275 | * |
||
276 | * @param string $method |
||
277 | * @param array $parameters |
||
278 | * |
||
279 | * @return mixed |
||
280 | */ |
||
281 | public function __call($method, $parameters) |
||
285 | |||
286 | /** |
||
287 | * Dynamically pass static method calls to the Redis instance. |
||
288 | * |
||
289 | * @param $method |
||
290 | * @param $parameters |
||
291 | * |
||
292 | * @return mixed |
||
293 | */ |
||
294 | public static function __callStatic($method, $parameters) |
||
298 | |||
299 | /** |
||
300 | * Close the connection to the Redis database. |
||
301 | * |
||
302 | * @return void |
||
303 | */ |
||
304 | public function __destruct() |
||
310 | } |
||
311 |