PageRenderTime 86ms CodeModel.GetById 50ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Zend/Queue/Adapter/Memcacheq.php

https://github.com/robeendey/ce
PHP | 408 lines | 178 code | 50 blank | 180 comment | 24 complexity | da49fa3ab01a28bb2ff7e7a50fdac4cc MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Memcacheq.php 18701 2009-10-26 13:03:47Z matthew $
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_AdapterAbstract
  24. */
  25. // require_once 'Zend/Queue/Adapter/AdapterAbstract.php';
  26. /**
  27. * Class for using connecting to a Zend_Cache-based queuing system
  28. *
  29. * @category Zend
  30. * @package Zend_Queue
  31. * @subpackage Adapter
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Queue_Adapter_Memcacheq extends Zend_Queue_Adapter_AdapterAbstract
  36. {
  37. const DEFAULT_HOST = '127.0.0.1';
  38. const DEFAULT_PORT = 22201;
  39. const EOL = "\r\n";
  40. /**
  41. * @var Memcache
  42. */
  43. protected $_cache = null;
  44. /**
  45. * @var string
  46. */
  47. protected $_host = null;
  48. /**
  49. * @var integer
  50. */
  51. protected $_port = null;
  52. /********************************************************************
  53. * Constructor / Destructor
  54. *********************************************************************/
  55. /**
  56. * Constructor
  57. *
  58. * @param array|Zend_Config $options
  59. * @param null|Zend_Queue $queue
  60. * @return void
  61. */
  62. public function __construct($options, Zend_Queue $queue = null)
  63. {
  64. if (!extension_loaded('memcache')) {
  65. // require_once 'Zend/Queue/Exception.php';
  66. throw new Zend_Queue_Exception('Memcache extension does not appear to be loaded');
  67. }
  68. parent::__construct($options, $queue);
  69. $options = &$this->_options['driverOptions'];
  70. if (!array_key_exists('host', $options)) {
  71. $options['host'] = self::DEFAULT_HOST;
  72. }
  73. if (!array_key_exists('port', $options)) {
  74. $options['port'] = self::DEFAULT_PORT;
  75. }
  76. $this->_cache = new Memcache();
  77. $result = $this->_cache->connect($options['host'], $options['port']);
  78. if ($result === false) {
  79. // require_once 'Zend/Queue/Exception.php';
  80. throw new Zend_Queue_Exception('Could not connect to MemcacheQ');
  81. }
  82. $this->_host = $options['host'];
  83. $this->_port = (int)$options['port'];
  84. }
  85. /**
  86. * Destructor
  87. *
  88. * @return void
  89. */
  90. public function __destruct()
  91. {
  92. if ($this->_cache instanceof Memcache) {
  93. $this->_cache->close();
  94. }
  95. }
  96. /********************************************************************
  97. * Queue management functions
  98. *********************************************************************/
  99. /**
  100. * Does a queue already exist?
  101. *
  102. * Throws an exception if the adapter cannot determine if a queue exists.
  103. * use isSupported('isExists') to determine if an adapter can test for
  104. * queue existance.
  105. *
  106. * @param string $name
  107. * @return boolean
  108. * @throws Zend_Queue_Exception
  109. */
  110. public function isExists($name)
  111. {
  112. return in_array($name, $this->getQueues());
  113. }
  114. /**
  115. * Create a new queue
  116. *
  117. * Visibility timeout is how long a message is left in the queue "invisible"
  118. * to other readers. If the message is acknowleged (deleted) before the
  119. * timeout, then the message is deleted. However, if the timeout expires
  120. * then the message will be made available to other queue readers.
  121. *
  122. * @param string $name queue name
  123. * @param integer $timeout default visibility timeout
  124. * @return boolean
  125. * @throws Zend_Queue_Exception
  126. */
  127. public function create($name, $timeout=null)
  128. {
  129. if ($this->isExists($name)) {
  130. return false;
  131. }
  132. if ($timeout === null) {
  133. $timeout = self::CREATE_TIMEOUT_DEFAULT;
  134. }
  135. // MemcacheQ does not have a method to "create" a queue
  136. // queues are created upon sending a packet.
  137. // We cannot use the send() and receive() functions because those
  138. // depend on the current name.
  139. $result = $this->_cache->set($name, 'creating queue', 0, 15);
  140. $result = $this->_cache->get($name);
  141. return true;
  142. }
  143. /**
  144. * Delete a queue and all of it's messages
  145. *
  146. * Returns false if the queue is not found, true if the queue exists
  147. *
  148. * @param string $name queue name
  149. * @return boolean
  150. * @throws Zend_Queue_Exception
  151. */
  152. public function delete($name)
  153. {
  154. $response = $this->_sendCommand('delete ' . $name, array('DELETED', 'NOT_FOUND'), true);
  155. if (in_array('DELETED', $response)) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. /**
  161. * Get an array of all available queues
  162. *
  163. * Not all adapters support getQueues(), use isSupported('getQueues')
  164. * to determine if the adapter supports this feature.
  165. *
  166. * @return array
  167. * @throws Zend_Queue_Exception
  168. */
  169. public function getQueues()
  170. {
  171. $response = $this->_sendCommand('stats queue', array('END'));
  172. $list = array();
  173. foreach ($response as $i => $line) {
  174. $list[] = str_replace('STAT ', '', $line);
  175. }
  176. return $list;
  177. }
  178. /**
  179. * Return the approximate number of messages in the queue
  180. *
  181. * @param Zend_Queue $queue
  182. * @return integer
  183. * @throws Zend_Queue_Exception (not supported)
  184. */
  185. public function count(Zend_Queue $queue=null)
  186. {
  187. // require_once 'Zend/Queue/Exception.php';
  188. throw new Zend_Queue_Exception('count() is not supported in this adapter');
  189. }
  190. /********************************************************************
  191. * Messsage management functions
  192. *********************************************************************/
  193. /**
  194. * Send a message to the queue
  195. *
  196. * @param string $message Message to send to the active queue
  197. * @param Zend_Queue $queue
  198. * @return Zend_Queue_Message
  199. * @throws Zend_Queue_Exception
  200. */
  201. public function send($message, Zend_Queue $queue=null)
  202. {
  203. if ($queue === null) {
  204. $queue = $this->_queue;
  205. }
  206. if (!$this->isExists($queue->getName())) {
  207. // require_once 'Zend/Queue/Exception.php';
  208. throw new Zend_Queue_Exception('Queue does not exist:' . $queue->getName());
  209. }
  210. $message = (string) $message;
  211. $data = array(
  212. 'message_id' => md5(uniqid(rand(), true)),
  213. 'handle' => null,
  214. 'body' => $message,
  215. 'md5' => md5($message),
  216. );
  217. $result = $this->_cache->set($queue->getName(), $message, 0, 0);
  218. if ($result === false) {
  219. // require_once 'Zend/Queue/Exception.php';
  220. throw new Zend_Queue_Exception('failed to insert message into queue:' . $queue->getName());
  221. }
  222. $options = array(
  223. 'queue' => $queue,
  224. 'data' => $data,
  225. );
  226. $classname = $queue->getMessageClass();
  227. if (!class_exists($classname)) {
  228. // require_once 'Zend/Loader.php';
  229. Zend_Loader::loadClass($classname);
  230. }
  231. return new $classname($options);
  232. }
  233. /**
  234. * Get messages in the queue
  235. *
  236. * @param integer $maxMessages Maximum number of messages to return
  237. * @param integer $timeout Visibility timeout for these messages
  238. * @param Zend_Queue $queue
  239. * @return Zend_Queue_Message_Iterator
  240. * @throws Zend_Queue_Exception
  241. */
  242. public function receive($maxMessages=null, $timeout=null, Zend_Queue $queue=null)
  243. {
  244. if ($maxMessages === null) {
  245. $maxMessages = 1;
  246. }
  247. if ($timeout === null) {
  248. $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
  249. }
  250. if ($queue === null) {
  251. $queue = $this->_queue;
  252. }
  253. $msgs = array();
  254. if ($maxMessages > 0 ) {
  255. for ($i = 0; $i < $maxMessages; $i++) {
  256. $data = array(
  257. 'handle' => md5(uniqid(rand(), true)),
  258. 'body' => $this->_cache->get($queue->getName()),
  259. );
  260. $msgs[] = $data;
  261. }
  262. }
  263. $options = array(
  264. 'queue' => $queue,
  265. 'data' => $msgs,
  266. 'messageClass' => $queue->getMessageClass(),
  267. );
  268. $classname = $queue->getMessageSetClass();
  269. if (!class_exists($classname)) {
  270. // require_once 'Zend/Loader.php';
  271. Zend_Loader::loadClass($classname);
  272. }
  273. return new $classname($options);
  274. }
  275. /**
  276. * Delete a message from the queue
  277. *
  278. * Returns true if the message is deleted, false if the deletion is
  279. * unsuccessful.
  280. *
  281. * @param Zend_Queue_Message $message
  282. * @return boolean
  283. * @throws Zend_Queue_Exception (unsupported)
  284. */
  285. public function deleteMessage(Zend_Queue_Message $message)
  286. {
  287. // require_once 'Zend/Queue/Exception.php';
  288. throw new Zend_Queue_Exception('deleteMessage() is not supported in ' . get_class($this));
  289. }
  290. /********************************************************************
  291. * Supporting functions
  292. *********************************************************************/
  293. /**
  294. * Return a list of queue capabilities functions
  295. *
  296. * $array['function name'] = true or false
  297. * true is supported, false is not supported.
  298. *
  299. * @param string $name
  300. * @return array
  301. */
  302. public function getCapabilities()
  303. {
  304. return array(
  305. 'create' => true,
  306. 'delete' => true,
  307. 'send' => true,
  308. 'receive' => true,
  309. 'deleteMessage' => false,
  310. 'getQueues' => true,
  311. 'count' => false,
  312. 'isExists' => true,
  313. );
  314. }
  315. /********************************************************************
  316. * Functions that are not part of the Zend_Queue_Adapter_Abstract
  317. *********************************************************************/
  318. /**
  319. * sends a command to MemcacheQ
  320. *
  321. * The memcache functions by php cannot handle all types of requests
  322. * supported by MemcacheQ
  323. * Non-standard requests are handled by this function.
  324. *
  325. * @param string $command - command to send to memcacheQ
  326. * @param array $terminator - strings to indicate end of memcacheQ response
  327. * @param boolean $include_term - include terminator in response
  328. * @return array
  329. * @throws Zend_Queue_Exception if connection cannot be opened
  330. */
  331. protected function _sendCommand($command, array $terminator, $include_term=false)
  332. {
  333. $fp = fsockopen($this->_host, $this->_port, $errno, $errstr, 10);
  334. if ($fp === false) {
  335. // require_once 'Zend/Queue/Exception.php';
  336. throw new Zend_Queue_Exception("Could not open a connection to $this->_host:$this->_port errno=$errno : $errstr");
  337. }
  338. $response = array();
  339. $cmd = $command . self::EOL;
  340. fwrite($fp, $cmd);
  341. $continue_reading = true;
  342. while (!feof($fp) && $continue_reading) {
  343. $resp = trim(fgets($fp, 1024));
  344. if (in_array($resp, $terminator)) {
  345. if ($include_term) {
  346. $response[] = $resp;
  347. }
  348. $continue_reading = false;
  349. } else {
  350. $response[] = $resp;
  351. }
  352. }
  353. $cmd = 'quit' . self::EOL;
  354. fwrite($fp, $cmd);
  355. fclose($fp);
  356. return $response;
  357. }
  358. }