RedisEngine.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. *
  37. * @var array
  38. */
  39. public $settings = array();
  40. /**
  41. * Initialize the Cache Engine
  42. *
  43. * Called automatically by the cache frontend
  44. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  45. *
  46. * @param array $settings array of setting for the engine
  47. * @return boolean True if the engine has been successfully initialized, false if not
  48. */
  49. public function init($settings = array()) {
  50. if (!class_exists('Redis')) {
  51. return false;
  52. }
  53. parent::init(array_merge(array(
  54. 'engine' => 'Redis',
  55. 'prefix' => null,
  56. 'server' => '127.0.0.1',
  57. 'port' => null,
  58. 'persistent' => true,
  59. 'serialize' => true
  60. ), $settings)
  61. );
  62. return $this->_connect();
  63. }
  64. /**
  65. * Connects to a Redis server
  66. *
  67. * @return boolean True if Redis server was connected
  68. */
  69. protected function _connect() {
  70. $return = false;
  71. try {
  72. $this->_Redis = new Redis();
  73. if (empty($this->settings['persistent'])) {
  74. $return = $this->_Redis->connect($this->settings['server']);
  75. } else {
  76. $return = $this->_Redis->pconnect($this->settings['server']);
  77. }
  78. } catch (RedisException $e) {
  79. return false;
  80. }
  81. return $return;
  82. }
  83. /**
  84. * Write data for key into cache.
  85. *
  86. * @param string $key Identifier for the data
  87. * @param mixed $value Data to be cached
  88. * @param integer $duration How long to cache the data, in seconds
  89. * @return boolean True if the data was successfully cached, false on failure
  90. */
  91. public function write($key, $value, $duration) {
  92. if (!is_int($value)) {
  93. $value = serialize($value);
  94. }
  95. if ($duration === 0) {
  96. return $this->_Redis->set($key, $value);
  97. }
  98. return $this->_Redis->setex($key, $duration, $value);
  99. }
  100. /**
  101. * Read a key from the cache
  102. *
  103. * @param string $key Identifier for the data
  104. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  105. */
  106. public function read($key) {
  107. $value = $this->_Redis->get($key);
  108. if (is_numeric($value)) {
  109. $value = (int) $value;
  110. }
  111. if ($value !== false && is_string($value)) {
  112. $value = unserialize($value);
  113. }
  114. return $value;
  115. }
  116. /**
  117. * Increments the value of an integer cached key
  118. *
  119. * @param string $key Identifier for the data
  120. * @param integer $offset How much to increment
  121. * @return New incremented value, false otherwise
  122. * @throws CacheException when you try to increment with compress = true
  123. */
  124. public function increment($key, $offset = 1) {
  125. return (int) $this->_Redis->incrBy($key, $offset);
  126. }
  127. /**
  128. * Decrements the value of an integer cached key
  129. *
  130. * @param string $key Identifier for the data
  131. * @param integer $offset How much to subtract
  132. * @return New decremented value, false otherwise
  133. * @throws CacheException when you try to decrement with compress = true
  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. * @return voind
  193. **/
  194. public function __destruct() {
  195. if (!$this->settings['persistent']) {
  196. $this->_Redis->close();
  197. }
  198. }
  199. }