@@ -26,6 +26,11 @@ class MemCache
2626 */
2727 protected $ servers = [];
2828
29+ /**
30+ * @var array
31+ */
32+ protected $ options = [];
33+
2934 /**
3035 * @link http://php.net/manual/en/memcached.add.php
3136 * @param string $key
@@ -318,9 +323,6 @@ public function getMemcached()
318323 {
319324 if ($ this ->memcached === null ) {
320325 $ this ->setMemcached (new Memcached ());
321-
322- $ servers = array_values ($ this ->getServers ());
323- $ this ->memcached ->addServers ($ servers );
324326 }
325327
326328 return $ this ->memcached ;
@@ -335,6 +337,16 @@ public function setMemcached(Memcached $cache)
335337 {
336338 $ this ->memcached = $ cache ;
337339
340+ $ servers = array_values ($ this ->getServers ());
341+ if ($ servers ) {
342+ $ this ->memcached ->addServers ($ servers );
343+ }
344+
345+ $ options = $ this ->getOptions ();
346+ if ($ options ) {
347+ $ this ->memcached ->setOptions ($ options );
348+ }
349+
338350 return $ this ;
339351 }
340352
@@ -386,7 +398,7 @@ public function setServers( array $servers)
386398
387399 $ key = implode ('' , $ server );
388400 if (!array_key_exists ($ key , $ this ->servers )) {
389- $ nServers [] = $ servers ;
401+ $ nServers [] = $ server ;
390402 $ this ->servers [$ key ] = $ server ;
391403 }
392404 }
@@ -398,4 +410,29 @@ public function setServers( array $servers)
398410
399411 return $ this ;
400412 }
413+
414+ /**
415+ * @return array
416+ */
417+ public function getOptions ()
418+ {
419+ return $ this ->options ;
420+ }
421+
422+ /**
423+ * @link http://php.net/manual/en/memcached.setoptions.php
424+ * @param array $options
425+ *
426+ * @return $this
427+ */
428+ public function setOptions (array $ options )
429+ {
430+ $ this ->options = array_merge ($ this ->options , $ options );
431+
432+ if ($ this ->memcached !== null && !empty ($ options )) {
433+ $ this ->memcached ->setOptions ($ options );
434+ }
435+
436+ return $ this ;
437+ }
401438}
0 commit comments