PageRenderTime 78ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/incubator/library/Zend/Queue/Adapter/Cache.php

https://github.com/jorgenils/zend-framework
PHP | 292 lines | 162 code | 33 blank | 97 comment | 22 complexity | a0d51d63a4668bbc40812ad1049a20a9 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_Abstract
  24. */
  25. require_once 'Zend/Queue/Adapter/Abstract.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-2008 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_Cache extends Zend_Queue_Adapter_Abstract implements Countable {
  36. const DEFAULT_QUEUE_KEY = 'zf-queues';
  37. const DEFAULT_CACHE_DIR = '/tmp/';
  38. /**
  39. * @var Zend_Cache
  40. */
  41. protected $_cache = null;
  42. /**
  43. * Constructor
  44. *
  45. * @param array $config
  46. */
  47. public function __construct($config)
  48. {
  49. parent::__construct($config);
  50. $options = &$this->_config['driver_options'];
  51. if (!array_key_exists('type', $options)) {
  52. /**
  53. * @see Zend_Queue_Adapter_Exception
  54. */
  55. require_once 'Zend/Queue/Adapter/Exception.php';
  56. throw new Zend_Queue_Adapter_Exception("Configuration array must have a key for 'type' for the cache type to use");
  57. }
  58. switch ($options['type']) {
  59. case 'Memcached':
  60. if (!array_key_exists('servers', $options)) {
  61. /**
  62. * @see Zend_Queue_Adapter_Exception
  63. */
  64. require_once 'Zend/Queue/Adapter/Exception.php';
  65. throw new Zend_Queue_Adapter_Exception("Configuration array must have a key for 'servers' for the servers to use");
  66. }
  67. break;
  68. case 'File':
  69. if (!array_key_exists('cache_dir', $options)) {
  70. $options['cache_dir'] = self::DEFAULT_CACHE_DIR;
  71. }
  72. break;
  73. case 'Zend_Platform':
  74. break;
  75. }
  76. $frontendOptions = array(
  77. 'lifetime' => 7200, // cache lifetime of 2 hours
  78. 'automatic_serialization' => true,
  79. 'logging' => false,
  80. 'write_control' => true
  81. );
  82. /**
  83. * @see Zend_Cache
  84. */
  85. require_once 'Zend/Cache.php';
  86. $this->_cache = Zend_Cache::factory('Core', $options['type'], $frontendOptions, $options);
  87. }
  88. /**
  89. * Create a new queue
  90. *
  91. * @param string $name queue name
  92. * @param integer $timeout default visiblity timeout
  93. * @return boolean True
  94. */
  95. public function create($name, $timeout=null)
  96. {
  97. if ($timeout === null) {
  98. $timeout = 30;
  99. }
  100. $queue = array(
  101. 'name' => $name,
  102. 'timeout' => $timeout
  103. );
  104. $queues = $this->_cache->load(self::DEFAULT_QUEUE_KEY);
  105. if ($queues === false) {
  106. $queues = array($queue);
  107. }
  108. else if (is_array($queues)) {
  109. $queues[] = $queue;
  110. }
  111. else {
  112. $queues = array($queues);
  113. $queues[] = $queue;
  114. }
  115. $ret = $this->_cache->save($queues, self::DEFAULT_QUEUE_KEY, array(), 0);
  116. if ($ret) {
  117. $this->setActiveQueue($name);
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * Delete a queue and all of it's messages
  124. *
  125. * @param string $name queue name
  126. * @return boolean True
  127. */
  128. public function delete($name)
  129. {
  130. $queues = $this->_cache->load(self::DEFAULT_QUEUE_KEY);
  131. if (is_array($queues)) {
  132. foreach ($queues as $key=>&$queue) {
  133. if ($queue['name'] == $name) {
  134. unset($queues[$key]);
  135. break;
  136. }
  137. }
  138. }
  139. // Delete any messages in the queue
  140. $this->_cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array($name));
  141. return $this->_cache->save($queues, self::DEFAULT_QUEUE_KEY, array(), 0);
  142. }
  143. /**
  144. * Delete a message from the queue
  145. *
  146. * @param string $handle
  147. * @return boolean
  148. */
  149. public function deleteMessage($handle)
  150. {
  151. return $this->_cache->remove($handle);
  152. }
  153. /**
  154. * Get an array of all available queues
  155. *
  156. * @return array
  157. */
  158. public function getQueues()
  159. {
  160. $queues = $this->_cache->load(self::DEFAULT_QUEUE_KEY);
  161. if ($queues === false) {
  162. return array();
  163. }
  164. $queue_names = array();
  165. foreach ($queues as $queue) {
  166. $queue_names[] = $queue['name'];
  167. }
  168. return $queue_names;
  169. }
  170. /**
  171. * Return the first element in the queue
  172. *
  173. * @param integer $max_msgs
  174. * @param integer $timeout
  175. * @return Zend_Queue_Message_Iterator
  176. */
  177. public function receive($max_msgs=null, $timeout=null)
  178. {
  179. // Default to returning only one message
  180. if ($max_msgs === null) {
  181. $max_msgs = 1;
  182. }
  183. if ($timeout === null) {
  184. $timeout = $this->_timeout;
  185. }
  186. $msgs = array();
  187. $info = $this->_message->info();
  188. $db = $this->_message->getAdapter();
  189. $query = $db->select();
  190. $query->from($info['name'], array('*'));
  191. $query->where('queue_id=?', $this->getActiveQueue());
  192. $query->where('handle IS NULL OR timeout+'.(int)$timeout.' < ?', microtime(true));
  193. $query->limit($max_msgs);
  194. foreach ($db->fetchAll($query) as $data) {
  195. try {
  196. $data['handle'] = md5(uniqid(rand(), true));
  197. $msgs[] = $data;
  198. $update = array(
  199. 'handle' => $data['handle'],
  200. 'timeout' => microtime(true)
  201. );
  202. $where = $db->quoteInto('message_id=?', $data['message_id']);
  203. $db->update($info['name'], $update, $where);
  204. }
  205. catch (Exception $e) {
  206. /**
  207. * @see Zend_Queue_Adapter_Exception
  208. */
  209. require_once 'Zend/Queue/Adapter/Exception.php';
  210. throw new Zend_Queue_Adapter_Exception($e->getMessage(), $e->getCode());
  211. }
  212. }
  213. $config = array(
  214. 'queue' => $this,
  215. 'data' => $msgs,
  216. 'msgClass' => $this->_msgClass
  217. );
  218. Zend_Loader::loadClass($this->_msgsetClass);
  219. return new $this->_msgsetClass($config);
  220. }
  221. /**
  222. * Push an element onto the end of the queue
  223. *
  224. * @param mixed $message message to send to the queue
  225. * @param string $name queue name
  226. * @return Zend_Queue_Message
  227. */
  228. public function send($message, $name=null)
  229. {
  230. if ($name !== null) {
  231. $this->setActiveQueue($name);
  232. }
  233. else {
  234. $name = $this->getActiveQueue();
  235. }
  236. $data = array(
  237. 'message_id' => md5(uniqid(rand(), true)),
  238. 'handle' => null,
  239. 'body' => $message,
  240. 'md5' => md5($message)
  241. );
  242. $this->_cache->save($data, $data['message_id'], array($name));
  243. $config = array(
  244. 'queue' => $this,
  245. 'data' => $data
  246. );
  247. Zend_Loader::loadClass($this->_msgClass);
  248. return new $this->_msgClass($config);
  249. }
  250. /**
  251. * Returns the length of the queue
  252. *
  253. * @return integer
  254. */
  255. public function count()
  256. {
  257. return -1;
  258. }
  259. }