/lib/Cake/Cache/Engine/RedisEngine.php

https://github.com/shama/cakephp · PHP · 222 lines · 96 code · 19 blank · 107 comment · 13 complexity · ac6c88eab82c0ff9924f28cf12add9fe MD5 · raw file

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