ARedisList.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Represents a redis list.
  4. * <pre>
  5. * $list = new ARedisList("myList");
  6. * $list[] = "an item"; // instantly saved to redis
  7. * $list[] = "another item"; // instantly saved to redis
  8. * echo count($list); // 2
  9. * echo $list->pop() // "another item"
  10. * echo count($list); // 1
  11. * </pre>
  12. * @author Charles Pick
  13. * @package packages.redis
  14. */
  15. class ARedisList extends ARedisIterableEntity {
  16. /**
  17. * Adds an item to the list
  18. * @param mixed $item the item to add
  19. * @return boolean true if the item was added, otherwise false
  20. */
  21. public function add($item) {
  22. if ($this->name === null) {
  23. throw new CException(get_class($this)." requires a name!");
  24. }
  25. if (!$this->getConnection()->getClient()->rpush($this->name,$item)) {
  26. return false;
  27. }
  28. $this->_data = null;
  29. $this->_count = null;
  30. return true;
  31. }
  32. /**
  33. * Removes an item from the list
  34. * @param mixed $item the item to remove
  35. * @return boolean true if the item was removed, otherwise false
  36. */
  37. public function remove($item) {
  38. if ($this->name === null) {
  39. throw new CException(get_class($this)." requires a name!");
  40. }
  41. if (!$this->getConnection()->getClient()->lrem($this->name,$item,1)) {
  42. return false;
  43. }
  44. $this->_data = null;
  45. $this->_count = null;
  46. return true;
  47. }
  48. /**
  49. * Adds an item to the end of the list
  50. * @param mixed $item the item to add
  51. * @return boolean true if the item was added, otherwise false
  52. */
  53. public function push($item) {
  54. return $this->add($item);
  55. }
  56. /**
  57. * Adds an item to the start of the list
  58. * @param mixed $item the item to add
  59. * @return boolean true if the item was added, otherwise false
  60. */
  61. public function unshift($item) {
  62. if ($this->name === null) {
  63. throw new CException(get_class($this)." requires a name!");
  64. }
  65. if (!$this->getConnection()->getClient()->lpush($this->name,$item)) {
  66. return false;
  67. }
  68. $this->_data = null;
  69. $this->_count = null;
  70. return true;
  71. }
  72. /**
  73. * Removes and returns the first item from the list
  74. * @return mixed the item that was removed from the list
  75. */
  76. public function shift() {
  77. if ($this->name === null) {
  78. throw new CException(get_class($this)." requires a name!");
  79. }
  80. $item = $this->getConnection()->getClient()->lpop($this->name);
  81. $this->_data = null;
  82. $this->_count = null;
  83. return $item;
  84. }
  85. /**
  86. * Removes and returns the last item from the list
  87. * @return mixed the item that was removed from the list
  88. */
  89. public function pop() {
  90. if ($this->name === null) {
  91. throw new CException(get_class($this)." requires a name!");
  92. }
  93. $item = $this->getConnection()->getClient()->rpop($this->name);
  94. $this->_data = null;
  95. $this->_count = null;
  96. return $item;
  97. }
  98. /**
  99. * Gets a range of items in the list
  100. * @param integer $start the 0 based index to start from
  101. * @param integer $stop the 0 based index to end at
  102. * @return array the items in the range
  103. */
  104. public function range($start = 0, $stop = -1) {
  105. if ($this->name === null) {
  106. throw new CException(get_class($this)." requires a name!");
  107. }
  108. return $this->getConnection()->getClient()->lrange($this->name, $start, $stop);
  109. }
  110. /**
  111. * Trims the list so that it will only contain the specified range of items
  112. * @param integer $start the 0 based index to start from
  113. * @param integer $stop the 0 based index to end at
  114. * @return boolean true if the trim was successful
  115. */
  116. public function trim($start, $stop) {
  117. if ($this->name === null) {
  118. throw new CException(get_class($this)." requires a name!");
  119. }
  120. return $this->getConnection()->getClient()->ltrim($this->name, $start, $stop) ? true : false;
  121. }
  122. /**
  123. * Gets the number of items in the list
  124. * @return integer the number of items in the list
  125. */
  126. public function getCount() {
  127. if ($this->_count === null) {
  128. if ($this->name === null) {
  129. throw new CException(get_class($this)." requires a name!");
  130. }
  131. $this->_count = (int) $this->getConnection()->getClient()->lSize($this->name);
  132. }
  133. return $this->_count;
  134. }
  135. /**
  136. * Gets all the members in the list
  137. * @param boolean $forceRefresh whether to force a refresh or not
  138. * @return array the members in the list
  139. */
  140. public function getData($forceRefresh = false) {
  141. if ($forceRefresh || $this->_data === null) {
  142. $this->_data = $this->range(0,-1);
  143. }
  144. return $this->_data;
  145. }
  146. /**
  147. * Copies iterable data into the list.
  148. * Note, existing data in the list will be cleared first.
  149. * @param mixed $data the data to be copied from, must be an array or object implementing Traversable
  150. * @throws CException If data is neither an array nor a Traversable.
  151. */
  152. public function copyFrom($data)
  153. {
  154. if(is_array($data) || ($data instanceof Traversable))
  155. {
  156. if($this->_count>0)
  157. $this->clear();
  158. if($data instanceof CList)
  159. $data=$data->_data;
  160. foreach($data as $item) {
  161. $this->add($item);
  162. }
  163. }
  164. else if($data!==null)
  165. throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.'));
  166. }
  167. /**
  168. * Returns whether there is an item at the specified offset.
  169. * This method is required by the interface ArrayAccess.
  170. * @param integer $offset the offset to check on
  171. * @return boolean
  172. */
  173. public function offsetExists($offset)
  174. {
  175. return ($offset>=0 && $offset<$this->getCount());
  176. }
  177. /**
  178. * Returns the item at the specified offset.
  179. * This method is required by the interface ArrayAccess.
  180. * @param integer $offset the offset to retrieve item.
  181. * @return mixed the item at the offset
  182. */
  183. public function offsetGet($offset)
  184. {
  185. return $this->_data[$offset];
  186. }
  187. /**
  188. * Sets the item at the specified offset.
  189. * This method is required by the interface ArrayAccess.
  190. * @param integer $offset the offset to set item
  191. * @param mixed $item the item value
  192. */
  193. public function offsetSet($offset,$item)
  194. {
  195. $this->add($item);
  196. }
  197. /**
  198. * Unsets the item at the specified offset.
  199. * This method is required by the interface ArrayAccess.
  200. * @param integer $offset the offset to unset item
  201. */
  202. public function offsetUnset($offset)
  203. {
  204. $this->remove($this->_data[$offset]);
  205. }
  206. }