RedisEngine.php 5.7 KB

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