RedisEngine.php 5.5 KB

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