PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rediska/Key/Hash.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 286 lines | 115 code | 35 blank | 136 comment | 11 complexity | ead44a6c42ed989be1053b1b478b8b6f MD5 | raw file
  1. <?php
  2. // Require Rediska
  3. require_once dirname(__FILE__) . '/../../Rediska.php';
  4. /**
  5. * Rediska hash 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_Hash extends Rediska_Key_Abstract implements IteratorAggregate, ArrayAccess, Countable
  15. {
  16. /**
  17. * Construct key
  18. *
  19. * @param string $name Key name
  20. * @param integer $options Options:
  21. * expire - Expire time
  22. * expireIsTimestamp - Expire time is timestamp. For default false (in seconds)
  23. * serverAlias - Server alias or connection object
  24. * rediska - Rediska instance name, Rediska object or Rediska options for new instance
  25. * @param string|Rediska_Connection $serverAlias Server alias or Rediska_Connection object where key is placed. Deprecated!
  26. */
  27. public function __construct($name, $options = array(), $serverAlias = null)
  28. {
  29. parent::__construct($name, $options, $serverAlias);
  30. $this->_throwIfNotSupported();
  31. }
  32. /**
  33. * Set value to a hash field or fields
  34. *
  35. * @param array|string $fieldOrData Field or array of many fields and values: field => value
  36. * @param mixed $value Value for single field
  37. * @param boolean $overwrite Overwrite for single field (if false don't set and return false if key already exist). For default true.
  38. * @return boolean
  39. */
  40. public function set($fieldOrData, $value = null, $overwrite = true)
  41. {
  42. $result = $this->_getRediskaOn()->setToHash($this->getName(), $fieldOrData, $value, $overwrite);
  43. if (!is_null($this->getExpire()) && ((!$overwrite && $result) || ($overwrite))) {
  44. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  45. }
  46. return $result;
  47. }
  48. /**
  49. * Magic for set a field
  50. *
  51. * @param string $field
  52. * @param mixed $value
  53. * @return boolean
  54. */
  55. public function __set($field, $value)
  56. {
  57. $this->set($field, $value);
  58. return $value;
  59. }
  60. /**
  61. * Array magic for set a field
  62. *
  63. * @param string $field
  64. * @param mixed $value
  65. * @return boolean
  66. */
  67. public function offsetSet($field, $value)
  68. {
  69. if (is_null($field)) {
  70. throw new Rediska_Key_Exception('Field must be present');
  71. }
  72. $this->set($field, $value);
  73. return $value;
  74. }
  75. /**
  76. * Get value from hash field or fields
  77. *
  78. * @param string $name Key name
  79. * @param string|array $fieldOrFields Field or fields
  80. * @return mixed
  81. */
  82. public function get($fieldOrFields)
  83. {
  84. return $this->_getRediskaOn()->getFromHash($this->getName(), $fieldOrFields);
  85. }
  86. /**
  87. * Magic for get a field
  88. *
  89. * @param string $field
  90. * @return mixed
  91. */
  92. public function __get($field)
  93. {
  94. return $this->get($field);
  95. }
  96. /**
  97. * Array magic for get a field
  98. *
  99. * @param string $name
  100. * @return mixed
  101. */
  102. public function offsetGet($field)
  103. {
  104. return $this->get($field);
  105. }
  106. /**
  107. * Increment field value in hash
  108. *
  109. * @param mixed $field Field
  110. * @param number $amount[optional] Increment amount. Default: 1
  111. * @return integer
  112. */
  113. public function increment($field, $amount = 1)
  114. {
  115. $result = $this->_getRediskaOn()->incrementInHash($this->getName(), $field, $amount);
  116. if (!is_null($this->getExpire()) && $result) {
  117. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Test if field is present in hash
  123. *
  124. * @param mixed $field Field
  125. * @return boolean
  126. */
  127. public function exists($field)
  128. {
  129. return $this->_getRediskaOn()->existsInHash($this->getName(), $field);
  130. }
  131. /**
  132. * Magic for test if field is present in hash
  133. *
  134. * @param string $field
  135. * @return boolean
  136. */
  137. public function __isset($field)
  138. {
  139. return $this->exists($field);
  140. }
  141. /**
  142. * Array magic for test if field is present in hash
  143. *
  144. * @param string $field
  145. * @return boolean
  146. */
  147. public function offsetExists($field)
  148. {
  149. return $this->exists($field);
  150. }
  151. /**
  152. * Remove field from hash
  153. *
  154. * @param mixed $field Field
  155. * @return boolean
  156. */
  157. public function remove($field)
  158. {
  159. $result = $this->_getRediskaOn()->deleteFromHash($this->getName(), $field);
  160. if (!is_null($this->getExpire()) && $result) {
  161. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  162. }
  163. return $result;
  164. }
  165. /**
  166. * Magic for remove field from hash
  167. *
  168. * @param string $field
  169. * @return boolean
  170. */
  171. public function __unset($field)
  172. {
  173. return $this->remove($field);
  174. }
  175. /**
  176. * Array magic for remove field from hash
  177. *
  178. * @param string $field
  179. * @return boolean
  180. */
  181. public function offsetUnset($field)
  182. {
  183. return $this->remove($field);
  184. }
  185. /**
  186. * Get hash fields
  187. *
  188. * @return array
  189. */
  190. public function getFields()
  191. {
  192. return $this->_getRediskaOn()->getHashFields($this->getName());
  193. }
  194. /**
  195. * Get hash values
  196. *
  197. * @return array
  198. */
  199. public function getValues()
  200. {
  201. return $this->_getRediskaOn()->getHashValues($this->getName());
  202. }
  203. /**
  204. * Get hash fields and values
  205. *
  206. * @return array
  207. */
  208. public function getFieldsAndValues()
  209. {
  210. return $this->_getRediskaOn()->getHash($this->getName());
  211. }
  212. /**
  213. * Get hash length
  214. *
  215. * @return intger
  216. */
  217. public function getLength()
  218. {
  219. return $this->_getRediskaOn()->getHashLength($this->getName());
  220. }
  221. /**
  222. * Get hash as array
  223. *
  224. * @return array
  225. */
  226. public function toArray()
  227. {
  228. return $this->getFieldsAndValues();
  229. }
  230. /* Countable implementation */
  231. public function count()
  232. {
  233. return $this->getLength();
  234. }
  235. /* IteratorAggregate implementation */
  236. public function getIterator()
  237. {
  238. return new ArrayObject($this->toArray());
  239. }
  240. /**
  241. * Throw if PubSub not supported by Redis
  242. */
  243. protected function _throwIfNotSupported()
  244. {
  245. $version = '1.3.10';
  246. $redisVersion = $this->getRediska()->getOption('redisVersion');
  247. if (version_compare($version, $this->getRediska()->getOption('redisVersion')) == 1) {
  248. throw new Rediska_PubSub_Exception("Publish/Subscribe requires {$version}+ version of Redis server. Current version is {$redisVersion}. To change it specify 'redisVersion' option.");
  249. }
  250. }
  251. }