@@ -20,12 +20,12 @@ class RedisSource extends DataSource {
2020 * @var array
2121 */
2222 protected $ _baseConfig = array (
23- 'server ' => '127.0.0.1 ' ,
23+ 'host ' => '127.0.0.1 ' ,
2424 'port ' => 6379 ,
2525 'password ' => '' ,
2626 'database ' => 0 ,
2727 'timeout ' => 0 ,
28- 'persistent ' => true ,
28+ 'persistent ' => false ,
2929 'unix_socket ' => '' ,
3030 'prefix ' => '' ,
3131 );
@@ -45,37 +45,32 @@ class RedisSource extends DataSource {
4545 protected $ _connection = null ;
4646
4747/**
48- * Constructor .
48+ * Whether or not we are connected to the DataSource .
4949 *
50- * Opens the connection to the server (if needed).
51- *
52- * @param array $config Array of configuration information for the Datasource
53- * @param bool $autoConnect Whether or not the datasource should automatically connect
54- * @return bool If autoconnect is true, true if the database could be connected, else false, otherwise, (always) true
55- * @throws MissingConnectionException when a connection cannot be made
50+ * @var bool
5651 */
57- public function __construct ($ config = null , $ autoConnect = true ) {
52+ public $ connected = false ;
53+
54+ /**
55+ * Constructor.
56+ *
57+ * @param array $config Array of configuration information for the Datasource
58+ * @return bool True if connecting to the DataSource succeeds, else false
59+ */
60+ public function __construct ($ config = array ()) {
5861 parent ::__construct ($ config );
5962
6063 if (!$ this ->enabled ()) {
61- throw new MissingConnectionException (array (
62- 'class ' => get_class ($ this ),
63- 'message ' => __d ('cake_dev ' , 'Selected driver is not enabled ' ),
64- 'enabled ' => false
65- ));
64+ return false ;
6665 }
6766
68- if ($ autoConnect ) {
69- return $ this ->connect ();
70- }
71-
72- return true ;
67+ return $ this ->connect ();
7368 }
7469
7570/**
7671 * Destructor.
7772 *
78- * Closes the connection to the server (if needed).
73+ * Closes the connection to the host (if needed).
7974 *
8075 * @return void
8176 */
@@ -86,7 +81,18 @@ public function __destruct() {
8681 }
8782
8883/**
89- * Check that the redis extension is installed/loaded.
84+ * Passes (non-existing) method calls to `Redis`.
85+ *
86+ * @param string $name The name of the method being called
87+ * @param array $arguments An enumerated array containing the parameters passed to the method
88+ * @return mixed Method return value
89+ */
90+ public function __call ($ name , $ arguments ) {
91+ return call_user_func_array (array ($ this ->_connection , $ name ), $ arguments );
92+ }
93+
94+ /**
95+ * Check that the redis extension is loaded.
9096 *
9197 * @return bool Whether or not the extension is loaded
9298 */
@@ -97,21 +103,87 @@ public function enabled() {
97103/**
98104 * Connects to the database using options in the given configuration array.
99105 *
100- * @return bool True if the database could be connected, else false
101- * @throws MissingConnectionException
106+ * "Connects mean:
107+ * - connect
108+ * - authenticate
109+ * - select
110+ * - setPrefix
111+ *
112+ * @return bool
102113 */
103114 public function connect () {
104- $ this ->connected = false ;
115+ $ this ->connected = $ this ->_connect ();
116+ $ this ->connected = $ this ->connected && $ this ->_authenticate ();
117+ $ this ->connected = $ this ->connected && $ this ->_select ();
118+ $ this ->connected = $ this ->connected && !$ this ->_setPrefix ();
119+
120+ return $ this ->connected ;
121+ }
105122
123+ /**
124+ * Connects to the database using options in the given configuration array.
125+ *
126+ * @return bool True if connecting to the DataSource succeeds, else false
127+ */
128+ protected function _connect () {
106129 try {
107130 $ this ->_connection = new Redis ();
108- $ this ->connected = true ;
109131
110- } catch (Exception $ e ) {
111- throw new MissingConnectionException (array ('class ' => get_class ($ this ), 'message ' => $ e ->getMessage ()));
132+ if ($ this ->config ['unix_socket ' ]) {
133+ return $ this ->_connection ->connect ($ this ->config ['unix_socket ' ]);
134+ } elseif (!$ this ->config ['persistent ' ]) {
135+ return $ this ->_connection ->connect (
136+ $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['timeout ' ]
137+ );
138+ } else {
139+ $ persistentId = crc32 (serialize ($ this ->config ));
140+
141+ return $ this ->_connection ->pconnect (
142+ $ this ->config ['host ' ], $ this ->config ['port ' ], $ this ->config ['timeout ' ], $ persistentId
143+ );
144+ }
145+ } catch (RedisException $ e ) {
146+ return false ;
112147 }
148+ }
113149
114- return $ this ->connected ;
150+ /**
151+ * Authenticates to the database (if needed) using options in the given configuration array.
152+ *
153+ * @return bool True if the authentication succeeded or no password was specified, else false
154+ */
155+ protected function _authenticate () {
156+ if ($ this ->config ['password ' ]) {
157+ return $ this ->_connection ->auth ($ this ->config ['password ' ]);
158+ }
159+
160+ return true ;
161+ }
162+
163+ /**
164+ * Selects a database (if needed) using options in the given configuration array.
165+ *
166+ * @return bool True if the select succeeded or no database was specified, else false
167+ */
168+ protected function _select () {
169+ if ($ this ->config ['database ' ]) {
170+ return $ this ->_connection ->select ($ this ->config ['database ' ]);
171+ }
172+
173+ return true ;
174+ }
175+
176+ /**
177+ * Sets a prefix for all keys (if needed) using options in the given configuration array.
178+ *
179+ * @return bool True if setting the prefix succeeded or no prefix was specified, else false
180+ */
181+ protected function _setPrefix () {
182+ if ($ this ->config ['prefix ' ]) {
183+ return $ this ->_connection ->setOption (Redis::OPT_PREFIX , $ this ->config ['prefix ' ]);
184+ }
185+
186+ return true ;
115187 }
116188
117189/**
@@ -122,10 +194,10 @@ public function connect() {
122194 public function close () {
123195 if ($ this ->isConnected ()) {
124196 $ this ->_connection ->close ();
125- unset($ this ->_connection );
126197 }
127198
128199 $ this ->connected = false ;
200+ $ this ->_connection = null ;
129201
130202 return true ;
131203 }
0 commit comments