PageRenderTime 301ms CodeModel.GetById 274ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Rediska/Key/Abstract.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 265 lines | 106 code | 32 blank | 127 comment | 7 complexity | 3b90dd4cbe43bac45915ecd43651a373 MD5 | raw file
  1. <?php
  2. /**
  3. * Rediska key abstract class
  4. *
  5. * @author Ivan Shumkov
  6. * @package Rediska
  7. * @subpackage Key objects
  8. * @version 0.5.7
  9. * @link http://rediska.geometria-lab.net
  10. * @license http://www.opensource.org/licenses/bsd-license.php
  11. */
  12. abstract class Rediska_Key_Abstract extends Rediska_Options_RediskaInstance
  13. {
  14. /**
  15. * Key name
  16. *
  17. * @var string
  18. */
  19. protected $_name;
  20. /**
  21. * Exception class name for options
  22. *
  23. * @var string
  24. */
  25. protected $_optionsException = 'Rediska_Key_Exception';
  26. /**
  27. * Options:
  28. *
  29. * expire - Expire time
  30. * expireIsTimestamp - Expire time is timestamp. For default false (in seconds)
  31. * serverAlias - Server alias or connection object
  32. * rediska - Rediska instance name, Rediska object or Rediska options for new instance
  33. *
  34. * @var array
  35. */
  36. protected $_options = array(
  37. 'serverAlias' => null,
  38. 'expire' => null,
  39. 'expireIsTimestamp' => false,
  40. );
  41. /**
  42. * Construct key
  43. *
  44. * @param string $name Key name
  45. * @param integer $options Options:
  46. * expire - Expire time
  47. * expireIsTimestamp - Expire time is timestamp. For default false (in seconds)
  48. * serverAlias - Server alias or connection object
  49. * rediska - Rediska instance name, Rediska object or Rediska options for new instance
  50. * @param string|Rediska_Connection $serverAlias Server alias or Rediska_Connection object where key is placed. Deprecated!
  51. */
  52. public function __construct($name, $options = array(), $serverAlias = null)
  53. {
  54. if (!is_array($options)) {
  55. throw new Rediska_Key_Exception("\$expire argument is deprectated. Use 'expire' option");
  56. }
  57. if (!is_null($serverAlias)) {
  58. throw new Rediska_Key_Exception("\$serverAlias argument is deprectated. Use 'serverAlias' option");
  59. }
  60. $this->setName($name);
  61. parent::__construct($options);
  62. }
  63. /**
  64. * Delete key
  65. *
  66. * @return boolean
  67. */
  68. public function delete()
  69. {
  70. return $this->_getRediskaOn()->delete($this->getName());
  71. }
  72. /**
  73. * Exists in db
  74. *
  75. * @return boolean
  76. */
  77. public function isExists()
  78. {
  79. return $this->_getRediskaOn()->exists($this->getName());
  80. }
  81. /**
  82. * Get key type
  83. *
  84. * @see Rediska#getType
  85. * @return string
  86. */
  87. public function getType()
  88. {
  89. return $this->_getRediskaOn()->getType($this->getName());
  90. }
  91. /**
  92. * Rename key
  93. *
  94. * @param string $newName
  95. * @param boolean $overwrite
  96. * @return boolean
  97. */
  98. public function rename($newName, $overwrite = true)
  99. {
  100. try {
  101. $this->_getRediskaOn()->rename($this->getName(), $newName, $overwrite);
  102. } catch (Rediska_Exception $e) {
  103. return false;
  104. }
  105. $this->setName($newName);
  106. if (!is_null($this->getExpire())) {
  107. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  108. }
  109. return true;
  110. }
  111. /**
  112. * Expire key
  113. *
  114. * @param integer $secondsOrTimestamp Time in seconds or timestamp
  115. * @param boolean $isTimestamp Time is timestamp? Default is false.
  116. * @return boolean
  117. */
  118. public function expire($secondsOrTimestamp, $isTimestamp = false)
  119. {
  120. return $this->_getRediskaOn()->expire($this->getName(), $secondsOrTimestamp, $isTimestamp);
  121. }
  122. /**
  123. * Get key lifetime
  124. *
  125. * @return integer
  126. */
  127. public function getLifetime()
  128. {
  129. return $this->_getRediskaOn()->getLifetime($this->getName());
  130. }
  131. /**
  132. * Move key to other Db
  133. *
  134. * @see Rediska#moveToDb
  135. * @param integer $dbIndex
  136. * @return boolean
  137. */
  138. public function moveToDb($dbIndex)
  139. {
  140. $result = $this->_getRediskaOn()->moveToDb($this->getName(), $dbIndex);
  141. if (!is_null($this->getExpire()) && $result) {
  142. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  143. }
  144. return $result;
  145. }
  146. /**
  147. * Get key name
  148. *
  149. * @return string
  150. */
  151. public function getName()
  152. {
  153. return $this->_name;
  154. }
  155. /**
  156. * Set key name
  157. *
  158. * @param string $name
  159. * @return Rediska_Key_Abstract
  160. */
  161. public function setName($name)
  162. {
  163. $this->_name = $name;
  164. return $this;
  165. }
  166. /**
  167. * Set expire time
  168. *
  169. * @param $secondsOrTimestamp Time in seconds or timestamp
  170. * @param $isTimestamp Time is timestamp? Default is false.
  171. * @return Rediska_Key_Abstract
  172. */
  173. public function setExpire($secondsOrTimestamp, $isTimestamp = false)
  174. {
  175. if ($secondsOrTimestamp !== null) {
  176. trigger_error('Expire option is deprecated, because expire behaviour was changed in Redis 2.2. Use expire method instead.', E_USER_WARNING);
  177. }
  178. $this->_options['expire'] = $secondsOrTimestamp;
  179. $this->_options['expireIsTimestamp'] = $isTimestamp;
  180. return $this;
  181. }
  182. /**
  183. * Get expire seconds or timestamp
  184. *
  185. * @return integer
  186. */
  187. public function getExpire()
  188. {
  189. return $this->_options['expire'];
  190. }
  191. /**
  192. * Is expire is timestamp
  193. *
  194. * @return boolean
  195. */
  196. public function isExpireTimestamp()
  197. {
  198. return $this->_options['expireIsTimestamp'];
  199. }
  200. /**
  201. * Set server alias
  202. *
  203. * @param $serverAlias
  204. * @return Rediska_Key_Abstract
  205. */
  206. public function setServerAlias($serverAlias)
  207. {
  208. $this->_options['serverAlias'] = $serverAlias;
  209. return $this;
  210. }
  211. /**
  212. * Get server alias
  213. *
  214. * @return null|string
  215. */
  216. public function getServerAlias()
  217. {
  218. return $this->_options['serverAlias'];
  219. }
  220. /**
  221. * Get rediska and set specified connection
  222. *
  223. * @return Rediska
  224. */
  225. protected function _getRediskaOn()
  226. {
  227. $rediska = $this->getRediska();
  228. if (!is_null($this->getServerAlias())) {
  229. $rediska = $rediska->on($this->getServerAlias());
  230. }
  231. return $rediska;
  232. }
  233. }