RedisEngine.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. 'password' => false,
  62. 'timeout' => 0,
  63. 'persistent' => true
  64. ), $settings)
  65. );
  66. return $this->_connect();
  67. }
  68. /**
  69. * Connects to a Redis server
  70. *
  71. * @return boolean True if Redis server was connected
  72. */
  73. protected function _connect() {
  74. $return = false;
  75. try {
  76. $this->_Redis = new Redis();
  77. if (empty($this->settings['persistent'])) {
  78. $return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
  79. } else {
  80. $return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
  81. }
  82. } catch (RedisException $e) {
  83. return false;
  84. }
  85. if ($return && $this->settings['password']) {
  86. $return = $this->_Redis->auth($this->settings['password']);
  87. }
  88. return $return;
  89. }
  90. /**
  91. * Write data for key into cache.
  92. *
  93. * @param string $key Identifier for the data
  94. * @param mixed $value Data to be cached
  95. * @param integer $duration How long to cache the data, in seconds
  96. * @return boolean True if the data was successfully cached, false on failure
  97. */
  98. public function write($key, $value, $duration) {
  99. if (!is_int($value)) {
  100. $value = serialize($value);
  101. }
  102. if ($duration === 0) {
  103. return $this->_Redis->set($key, $value);
  104. }
  105. return $this->_Redis->setex($key, $duration, $value);
  106. }
  107. /**
  108. * Read a key from the cache
  109. *
  110. * @param string $key Identifier for the data
  111. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  112. */
  113. public function read($key) {
  114. $value = $this->_Redis->get($key);
  115. if (ctype_digit($value)) {
  116. $value = (int)$value;
  117. }
  118. if ($value !== false && is_string($value)) {
  119. $value = unserialize($value);
  120. }
  121. return $value;
  122. }
  123. /**
  124. * Increments the value of an integer cached key
  125. *
  126. * @param string $key Identifier for the data
  127. * @param integer $offset How much to increment
  128. * @return New incremented value, false otherwise
  129. * @throws CacheException when you try to increment with compress = true
  130. */
  131. public function increment($key, $offset = 1) {
  132. return (int)$this->_Redis->incrBy($key, $offset);
  133. }
  134. /**
  135. * Decrements the value of an integer cached key
  136. *
  137. * @param string $key Identifier for the data
  138. * @param integer $offset How much to subtract
  139. * @return New decremented value, false otherwise
  140. * @throws CacheException when you try to decrement with compress = true
  141. */
  142. public function decrement($key, $offset = 1) {
  143. return (int)$this->_Redis->decrBy($key, $offset);
  144. }
  145. /**
  146. * Delete a key from the cache
  147. *
  148. * @param string $key Identifier for the data
  149. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  150. */
  151. public function delete($key) {
  152. return $this->_Redis->delete($key) > 0;
  153. }
  154. /**
  155. * Delete all keys from the cache
  156. *
  157. * @param boolean $check
  158. * @return boolean True if the cache was successfully cleared, false otherwise
  159. */
  160. public function clear($check) {
  161. if ($check) {
  162. return true;
  163. }
  164. $keys = $this->_Redis->getKeys($this->settings['prefix'] . '*');
  165. $this->_Redis->del($keys);
  166. return true;
  167. }
  168. /**
  169. * Returns the `group value` for each of the configured groups
  170. * If the group initial value was not found, then it initializes
  171. * the group accordingly.
  172. *
  173. * @return array
  174. */
  175. public function groups() {
  176. $result = array();
  177. foreach ($this->settings['groups'] as $group) {
  178. $value = $this->_Redis->get($this->settings['prefix'] . $group);
  179. if (!$value) {
  180. $value = 1;
  181. $this->_Redis->set($this->settings['prefix'] . $group, $value);
  182. }
  183. $result[] = $group . $value;
  184. }
  185. return $result;
  186. }
  187. /**
  188. * Increments the group value to simulate deletion of all keys under a group
  189. * old values will remain in storage until they expire.
  190. *
  191. * @return boolean success
  192. */
  193. public function clearGroup($group) {
  194. return (bool)$this->_Redis->incr($this->settings['prefix'] . $group);
  195. }
  196. /**
  197. * Disconnects from the redis server
  198. *
  199. * @return void
  200. */
  201. public function __destruct() {
  202. if (!$this->settings['persistent']) {
  203. $this->_Redis->close();
  204. }
  205. }
  206. }