PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rediska/Key/List.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 342 lines | 153 code | 49 blank | 140 comment | 23 complexity | 37b4abebf2c101b22e67ba3cf59ac29e MD5 | raw file
  1. <?php
  2. // Require Rediska
  3. require_once dirname(__FILE__) . '/../../Rediska.php';
  4. /**
  5. * Rediska List key
  6. *
  7. * @author Ivan Shumkov
  8. * @package Rediska
  9. * @subpackage Key objects
  10. * @version 0.5.7
  11. * @link http://rediska.geometria-lab.net
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. */
  14. class Rediska_Key_List extends Rediska_Key_Abstract implements IteratorAggregate, ArrayAccess, Countable
  15. {
  16. /**
  17. * Append value to the end of List
  18. *
  19. * @param mixed $value Value
  20. * @param boolean[optional] $createIfNotExists Create list if not exists
  21. * @return boolean
  22. */
  23. public function append($value, $createIfNotExists = true)
  24. {
  25. $result = $this->_getRediskaOn()->appendToList($this->getName(), $value, $createIfNotExists);
  26. if (!is_null($this->getExpire()) && $result) {
  27. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  28. }
  29. return $result;
  30. }
  31. /**
  32. * Append value to the head of List
  33. *
  34. * @param mixed $value Value
  35. * @param boolean[optional] $createIfNotExists Create list if not exists
  36. * @return boolean
  37. */
  38. public function prepend($value, $createIfNotExists = true)
  39. {
  40. $result = $this->_getRediskaOn()->prependToList($this->getName(), $value, $createIfNotExists);
  41. if (!is_null($this->getExpire()) && $result) {
  42. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  43. }
  44. return $result;
  45. }
  46. /**
  47. * Get List length
  48. *
  49. * @return integer
  50. */
  51. public function getLength()
  52. {
  53. return $this->_getRediskaOn()->getListLength($this->getName());
  54. }
  55. /**
  56. * Trim the list at key to the specified range of elements
  57. *
  58. * @param integer $start Start index
  59. * @param integer $end End index
  60. * @return boolean
  61. */
  62. public function truncate($start, $end)
  63. {
  64. $result = $this->_getRediskaOn()->truncateList($this->getName(), $start, $end);
  65. if (!is_null($this->getExpire()) && $result) {
  66. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  67. }
  68. return $result;
  69. }
  70. /**
  71. * Return element of List by index
  72. *
  73. * @param integer $index Index
  74. * @return mixed
  75. */
  76. public function get($index)
  77. {
  78. return $this->_getRediskaOn()->getFromList($this->getName(), $index);
  79. }
  80. /**
  81. * Set a new value as the element at index position of the List
  82. *
  83. * @param integer $index Index
  84. * @param mixed $value Value
  85. * @return boolean
  86. */
  87. public function set($index, $value)
  88. {
  89. $result = $this->_getRediskaOn()->setToList($this->getName(), $index, $value);
  90. if (!is_null($this->getExpire()) && $result) {
  91. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  92. }
  93. return $result;
  94. }
  95. /**
  96. * Insert a new value as the element after the reference value
  97. *
  98. * @param mixed $referenceValue Reference value
  99. * @param mixed $value Value
  100. * @return integer|boolean
  101. */
  102. public function insertAfter($referenceValue, $value)
  103. {
  104. return $this->_getRediskaOn()->insertToListAfter($this->getName(), $referenceValue, $value);
  105. }
  106. /**
  107. * Insert a new value as the element before the reference value
  108. *
  109. * @param mixed $referenceValue Reference value
  110. * @param mixed $value Value
  111. * @return integer|boolean
  112. */
  113. public function insertBefore($referenceValue, $value)
  114. {
  115. return $this->_getRediskaOn()->insertToListBefore($this->getName(), $referenceValue, $value);
  116. }
  117. /**
  118. * Insert a new value as the element before or after the reference value
  119. *
  120. * @param string $position BEFORE or AFTER
  121. * @param mixed $referenceValue Reference value
  122. * @param mixed $value Value
  123. * @return integer|boolean
  124. */
  125. public function insert($position, $referenceValue, $value)
  126. {
  127. return $this->_getRediskaOn()->insertToList($this->getName(), $position, $referenceValue, $value);
  128. }
  129. /**
  130. * Delete element from list by value
  131. *
  132. * @throws Rediska_Exception
  133. * @param $value Element value
  134. * @param $count Limit of deleted items
  135. * @return integer
  136. */
  137. public function remove($value, $count = 0)
  138. {
  139. $result = $this->_getRediskaOn()->deleteFromList($this->getName(), $value, $count);
  140. if (!is_null($this->getExpire()) && $result) {
  141. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  142. }
  143. return $result;
  144. }
  145. /**
  146. * Return and remove the first element of the List
  147. *
  148. * @return mixed
  149. */
  150. public function shift()
  151. {
  152. $result = $this->_getRediskaOn()->shiftFromList($this->getName());
  153. if (!is_null($this->getExpire()) && $result) {
  154. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  155. }
  156. return $result;
  157. }
  158. /**
  159. * Return and remove the first element of the List and block if list empty or not exists
  160. *
  161. * @param $timeout Blocking timeout
  162. * @return mixed
  163. */
  164. public function shiftBlocking($timeout = 0)
  165. {
  166. $result = $this->_getRediskaOn()->shiftFromListBlocking($this->getName(), $timeout);
  167. if (!is_null($this->getExpire()) && $result) {
  168. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Return and remove the last element of the List
  174. *
  175. * @param string|Rediska_Key_List $pushTo After pop, push value to another list
  176. * @return mixed
  177. */
  178. public function pop($pushTo = null)
  179. {
  180. if ($pushTo instanceof Rediska_Key_List) {
  181. $pushTo = $pushTo->getName();
  182. }
  183. $result = $this->_getRediskaOn()->popFromList($this->getName(), $pushTo);
  184. if (!is_null($this->getExpire()) && $result) {
  185. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  186. }
  187. return $result;
  188. }
  189. /**
  190. * Return and remove the last element of the List and block if list empty or not exists.
  191. *
  192. * @param integer $timeout[optional] Blocking timeout. 0 for default - timeout is disabled.
  193. * @param string $pushToKey[optional] If not null - push value to another list.
  194. * @return mixed
  195. */
  196. public function popBlocking($timeout = 0, $pushToKey = null)
  197. {
  198. $result = $this->_getRediskaOn()->popFromListBlocking($this->getName(), $timeout, $pushToKey);
  199. if (!is_null($this->getExpire()) && $result) {
  200. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  201. }
  202. return $result;
  203. }
  204. /**
  205. * Get List values
  206. *
  207. * @param integer $start Start index
  208. * @param integer $end End index
  209. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  210. * Important: new connection will be created
  211. * @return array
  212. */
  213. public function getValues($start = 0, $end = -1, $responseIterator = false)
  214. {
  215. return $this->_getRediskaOn()->getList($this->getName(), $start, $end, $responseIterator);
  216. }
  217. /**
  218. * Get sorted the elements
  219. *
  220. * @param string|array $value Options or SORT query string (http://code.google.com/p/redis/wiki/SortCommand).
  221. * Important notes for SORT query string:
  222. * 1. If you set Rediska namespace option don't forget add it to key names.
  223. * 2. If you use more then one connection to Redis servers, it will choose by key name,
  224. * and key by you pattern's may not present on it.
  225. * @return array
  226. */
  227. public function sort($options = array())
  228. {
  229. return $this->_getRediskaOn()->sort($this->getName(), $options);
  230. }
  231. /**
  232. * Get List values
  233. *
  234. * @param integer $start Start index
  235. * @param integer $end End index
  236. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  237. * Important: new connection will be created
  238. * @return array
  239. */
  240. public function toArray($start = 0, $end = -1, $responseIterator = false)
  241. {
  242. return $this->getValues($start, $end, $responseIterator);
  243. }
  244. /**
  245. * Add array to List
  246. *
  247. * @param array $array
  248. */
  249. public function fromArray(array $array)
  250. {
  251. $pipeline = $this->_getRediskaOn()->pipeline();
  252. foreach($array as $item) {
  253. $pipeline->appendToList($this->getName(), $item);
  254. }
  255. if (!is_null($this->getExpire())) {
  256. $pipeline->expire($this->getName(), $this->getExpire(), $this->isExpireTimestamp());
  257. }
  258. $pipeline->execute();
  259. return true;
  260. }
  261. /**
  262. * Implement intrefaces
  263. */
  264. public function count()
  265. {
  266. return $this->getLength();
  267. }
  268. public function getIterator()
  269. {
  270. return new ArrayObject($this->toArray());
  271. }
  272. public function offsetSet($offset, $value)
  273. {
  274. if (is_null($offset)) {
  275. $this->append($value);
  276. } else {
  277. $this->set($offset, $value);
  278. }
  279. return $value;
  280. }
  281. public function offsetExists($offset)
  282. {
  283. return (boolean)$this->get($offset);
  284. }
  285. public function offsetUnset($offset)
  286. {
  287. throw new Rediska_Key_Exception("Redis not support delete by index. User 'remove' method for delete by value");
  288. }
  289. public function offsetGet($offset)
  290. {
  291. return $this->get($offset);
  292. }
  293. }