RedisEngine.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * Redis storage engine for cache
  4. *
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Cache.Engine
  17. * @since CakePHP(tm) v 2.2
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Redis storage engine for cache.
  22. *
  23. * @package Cake.Cache.Engine
  24. */
  25. class RedisEngine extends CacheEngine {
  26. /**
  27. * Redis wrapper.
  28. *
  29. * @var Redis
  30. */
  31. protected $_Redis = null;
  32. /**
  33. * Settings
  34. *
  35. * - server = string url or ip to the Redis server host
  36. * - port = integer port number to the Redis server (default: 6379)
  37. * - timeout = float timeout in seconds (default: 0)
  38. * - persistent = bool Connects to the Redis server with a persistent connection (default: true)
  39. *
  40. * @var array
  41. */
  42. public $settings = array();
  43. /**
  44. * Initialize the Cache Engine
  45. *
  46. * Called automatically by the cache frontend
  47. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  48. *
  49. * @param array $settings array of setting for the engine
  50. * @return boolean True if the engine has been successfully initialized, false if not
  51. */
  52. public function init($settings = array()) {
  53. if (!class_exists('Redis')) {
  54. return false;
  55. }
  56. parent::init(array_merge(array(
  57. 'engine' => 'Redis',
  58. 'prefix' => null,
  59. 'server' => '127.0.0.1',
  60. 'port' => 6379,
  61. 'timeout' => 0,
  62. 'persistent' => true
  63. ), $settings)
  64. );
  65. return $this->_connect();
  66. }
  67. /**
  68. * Connects to a Redis server
  69. *
  70. * @return boolean True if Redis server was connected
  71. */
  72. protected function _connect() {
  73. $return = false;
  74. try {
  75. $this->_Redis = new Redis();
  76. if (empty($this->settings['persistent'])) {
  77. $return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
  78. } else {
  79. $return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
  80. }
  81. } catch (RedisException $e) {
  82. return false;
  83. }
  84. return $return;
  85. }
  86. /**
  87. * Write data for key into cache.
  88. *
  89. * @param string $key Identifier for the data
  90. * @param mixed $value Data to be cached
  91. * @param integer $duration How long to cache the data, in seconds
  92. * @return boolean True if the data was successfully cached, false on failure
  93. */
  94. public function write($key, $value, $duration) {
  95. if (!is_int($value)) {
  96. $value = serialize($value);
  97. }
  98. if ($duration === 0) {
  99. return $this->_Redis->set($key, $value);
  100. }
  101. return $this->_Redis->setex($key, $duration, $value);
  102. }
  103. /**
  104. * Read a key from the cache
  105. *
  106. * @param string $key Identifier for the data
  107. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  108. */
  109. public function read($key) {
  110. $value = $this->_Redis->get($key);
  111. if (ctype_digit($value)) {
  112. $value = (int)$value;
  113. }
  114. if ($value !== false && is_string($value)) {
  115. $value = unserialize($value);
  116. }
  117. return $value;
  118. }
  119. /**
  120. * Increments the value of an integer cached key
  121. *
  122. * @param string $key Identifier for the data
  123. * @param integer $offset How much to increment
  124. * @return New incremented value, false otherwise
  125. * @throws CacheException when you try to increment with compress = true
  126. */
  127. public function increment($key, $offset = 1) {
  128. return (int)$this->_Redis->incrBy($key, $offset);
  129. }
  130. /**
  131. * Decrements the value of an integer cached key
  132. *
  133. * @param string $key Identifier for the data
  134. * @param integer $offset How much to subtract
  135. * @return New decremented value, false otherwise
  136. * @throws CacheException when you try to decrement with compress = true
  137. */
  138. public function decrement($key, $offset = 1) {
  139. return (int)$this->_Redis->decrBy($key, $offset);
  140. }
  141. /**
  142. * Delete a key from the cache
  143. *
  144. * @param string $key Identifier for the data
  145. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  146. */
  147. public function delete($key) {
  148. return $this->_Redis->delete($key) > 0;
  149. }
  150. /**
  151. * Delete all keys from the cache
  152. *
  153. * @param boolean $check
  154. * @return boolean True if the cache was successfully cleared, false otherwise
  155. */
  156. public function clear($check) {
  157. if ($check) {
  158. return true;
  159. }
  160. $keys = $this->_Redis->getKeys($this->settings['prefix'] . '*');
  161. $this->_Redis->del($keys);
  162. return true;
  163. }
  164. /**
  165. * Returns the `group value` for each of the configured groups
  166. * If the group initial value was not found, then it initializes
  167. * the group accordingly.
  168. *
  169. * @return array
  170. **/
  171. public function groups() {
  172. $result = array();
  173. foreach ($this->settings['groups'] as $group) {
  174. $value = $this->_Redis->get($this->settings['prefix'] . $group);
  175. if (!$value) {
  176. $value = 1;
  177. $this->_Redis->set($this->settings['prefix'] . $group, $value);
  178. }
  179. $result[] = $group . $value;
  180. }
  181. return $result;
  182. }
  183. /**
  184. * Increments the group value to simulate deletion of all keys under a group
  185. * old values will remain in storage until they expire.
  186. *
  187. * @return boolean success
  188. **/
  189. public function clearGroup($group) {
  190. return (bool)$this->_Redis->incr($this->settings['prefix'] . $group);
  191. }
  192. /**
  193. * Disconnects from the redis server
  194. *
  195. * @return voind
  196. **/
  197. public function __destruct() {
  198. if (!$this->settings['persistent']) {
  199. $this->_Redis->close();
  200. }
  201. }
  202. }