ARedisHash.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Represents a persistent hash stored in redis.
  4. * <pre>
  5. * $hash = new ARedisHash("myHash");
  6. * $hash['a key'] = "some value"; // value is instantly saved to redis
  7. * $hash['another key'] = "some other value"; // value is instantly saved to redis
  8. * </pre>
  9. * @author Charles Pick
  10. * @package packages.redis
  11. */
  12. class ARedisHash extends ARedisIterableEntity {
  13. /**
  14. * Adds an item to the hash
  15. * @param string $key the hash key
  16. * @param mixed $value the item to add
  17. * @return boolean true if the item was added, otherwise false
  18. */
  19. public function add($key, $value) {
  20. if ($this->name === null) {
  21. throw new CException(get_class($this)." requires a name!");
  22. }
  23. if (!$this->getConnection()->getClient()->hset($this->name,$key, $value)) {
  24. return false;
  25. }
  26. $this->_data = null;
  27. $this->_count = null;
  28. return true;
  29. }
  30. /**
  31. * Removes an item from the hash
  32. * @param string $key the hash key to remove
  33. * @return boolean true if the item was removed, otherwise false
  34. */
  35. public function remove($key) {
  36. if ($this->name === null) {
  37. throw new CException(get_class($this)." requires a name!");
  38. }
  39. if (!$this->getConnection()->getClient()->hdel($this->name,$key)) {
  40. return false;
  41. }
  42. $this->_data = null;
  43. $this->_count = null;
  44. return true;
  45. }
  46. /**
  47. * Returns an iterator for traversing the items in the hash.
  48. * This method is required by the interface IteratorAggregate.
  49. * @return Iterator an iterator for traversing the items in the hash.
  50. */
  51. public function getIterator()
  52. {
  53. return new CMapIterator($this->getData());
  54. }
  55. /**
  56. * Gets the number of items in the hash
  57. * @return integer the number of items in the set
  58. */
  59. public function getCount() {
  60. if ($this->_count === null) {
  61. if ($this->name === null) {
  62. throw new CException(get_class($this)." requires a name!");
  63. }
  64. $this->_count = $this->getConnection()->getClient()->hlen($this->name);
  65. }
  66. return $this->_count;
  67. }
  68. /**
  69. * Gets all the members in the sorted set
  70. * @param boolean $forceRefresh whether to force a refresh or not
  71. * @return array the members in the set
  72. */
  73. public function getData($forceRefresh = false) {
  74. if ($forceRefresh || $this->_data === null) {
  75. if ($this->name === null) {
  76. throw new CException(get_class($this)." requires a name!");
  77. }
  78. $this->_data = $this->getConnection()->getClient()->hgetall($this->name);
  79. }
  80. return $this->_data;
  81. }
  82. /**
  83. * Returns whether there is an item at the specified offset.
  84. * This method is required by the interface ArrayAccess.
  85. * @param integer $offset the offset to check on
  86. * @return boolean
  87. */
  88. public function offsetExists($offset)
  89. {
  90. return ($offset>=0 && $offset<$this->getCount());
  91. }
  92. /**
  93. * Returns the item at the specified offset.
  94. * This method is required by the interface ArrayAccess.
  95. * @param integer $offset the offset to retrieve item.
  96. * @return mixed the item at the offset
  97. */
  98. public function offsetGet($offset)
  99. {
  100. $data = $this->getData();
  101. return $data[$offset];
  102. }
  103. /**
  104. * Sets the item at the specified offset.
  105. * This method is required by the interface ArrayAccess.
  106. * @param integer $offset the offset to set item
  107. * @param mixed $item the item value
  108. */
  109. public function offsetSet($offset,$item)
  110. {
  111. $this->add($offset,$item);
  112. }
  113. /**
  114. * Unsets the item at the specified offset.
  115. * This method is required by the interface ArrayAccess.
  116. * @param integer $offset the offset to unset item
  117. */
  118. public function offsetUnset($offset)
  119. {
  120. $this->remove($offset);
  121. }
  122. }