RedisEngine.php 5.7 KB

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