PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 230 lines | 101 code | 17 blank | 112 comment | 14 complexity | e63a1959a7c4a12dba8f78cae3dae789 MD5 | raw file
  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. * - database = integer database number to use for connection
  35. * - port = integer port number to the Redis server (default: 6379)
  36. * - timeout = float timeout in seconds (default: 0)
  37. * - persistent = boolean Connects to the Redis server with a persistent connection (default: true)
  38. * - unix_socket = path to the unix socket file (default: false)
  39. *
  40. * @var array
  41. */
  42. public $settings = array();
  43. /**
  44. * Initialize the Cache Engine
  45. *
  46. * Called automatically by the cache frontend
  47. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  48. *
  49. * @param array $settings array of setting for the engine
  50. * @return bool True if the engine has been successfully initialized, false if not
  51. */
  52. public function init($settings = array()) {
  53. if (!class_exists('Redis')) {
  54. return false;
  55. }
  56. parent::init(array_merge(array(
  57. 'engine' => 'Redis',
  58. 'prefix' => Inflector::slug(APP_DIR) . '_',
  59. 'server' => '127.0.0.1',
  60. 'database' => 0,
  61. 'port' => 6379,
  62. 'password' => false,
  63. 'timeout' => 0,
  64. 'persistent' => true,
  65. 'unix_socket' => false
  66. ), $settings)
  67. );
  68. return $this->_connect();
  69. }
  70. /**
  71. * Connects to a Redis server
  72. *
  73. * @return bool True if Redis server was connected
  74. */
  75. protected function _connect() {
  76. try {
  77. $this->_Redis = new Redis();
  78. if (!empty($this->settings['unix_socket'])) {
  79. $return = $this->_Redis->connect($this->settings['unix_socket']);
  80. } elseif (empty($this->settings['persistent'])) {
  81. $return = $this->_Redis->connect($this->settings['server'], $this->settings['port'], $this->settings['timeout']);
  82. } else {
  83. $persistentId = $this->settings['port'] . $this->settings['timeout'] . $this->settings['database'];
  84. $return = $this->_Redis->pconnect($this->settings['server'], $this->settings['port'], $this->settings['timeout'], $persistentId);
  85. }
  86. } catch (RedisException $e) {
  87. $return = false;
  88. }
  89. if (!$return) {
  90. return false;
  91. }
  92. if ($this->settings['password'] && !$this->_Redis->auth($this->settings['password'])) {
  93. return false;
  94. }
  95. return $this->_Redis->select($this->settings['database']);
  96. }
  97. /**
  98. * Write data for key into cache.
  99. *
  100. * @param string $key Identifier for the data
  101. * @param mixed $value Data to be cached
  102. * @param int $duration How long to cache the data, in seconds
  103. * @return bool True if the data was successfully cached, false on failure
  104. */
  105. public function write($key, $value, $duration) {
  106. if (!is_int($value)) {
  107. $value = serialize($value);
  108. }
  109. if ($duration === 0) {
  110. return $this->_Redis->set($key, $value);
  111. }
  112. return $this->_Redis->setex($key, $duration, $value);
  113. }
  114. /**
  115. * Read a key from the cache
  116. *
  117. * @param string $key Identifier for the data
  118. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  119. */
  120. public function read($key) {
  121. $value = $this->_Redis->get($key);
  122. if (ctype_digit($value)) {
  123. $value = (int)$value;
  124. }
  125. if ($value !== false && is_string($value)) {
  126. $value = unserialize($value);
  127. }
  128. return $value;
  129. }
  130. /**
  131. * Increments the value of an integer cached key
  132. *
  133. * @param string $key Identifier for the data
  134. * @param int $offset How much to increment
  135. * @return New incremented value, false otherwise
  136. * @throws CacheException when you try to increment with compress = true
  137. */
  138. public function increment($key, $offset = 1) {
  139. return (int)$this->_Redis->incrBy($key, $offset);
  140. }
  141. /**
  142. * Decrements the value of an integer cached key
  143. *
  144. * @param string $key Identifier for the data
  145. * @param int $offset How much to subtract
  146. * @return New decremented value, false otherwise
  147. * @throws CacheException when you try to decrement with compress = true
  148. */
  149. public function decrement($key, $offset = 1) {
  150. return (int)$this->_Redis->decrBy($key, $offset);
  151. }
  152. /**
  153. * Delete a key from the cache
  154. *
  155. * @param string $key Identifier for the data
  156. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  157. */
  158. public function delete($key) {
  159. return $this->_Redis->delete($key) > 0;
  160. }
  161. /**
  162. * Delete all keys from the cache
  163. *
  164. * @param bool $check Whether or not expiration keys should be checked. If
  165. * true, no keys will be removed as cache will rely on redis TTL's.
  166. * @return bool True if the cache was successfully cleared, false otherwise
  167. */
  168. public function clear($check) {
  169. if ($check) {
  170. return true;
  171. }
  172. $keys = $this->_Redis->getKeys($this->settings['prefix'] . '*');
  173. $this->_Redis->del($keys);
  174. return true;
  175. }
  176. /**
  177. * Returns the `group value` for each of the configured groups
  178. * If the group initial value was not found, then it initializes
  179. * the group accordingly.
  180. *
  181. * @return array
  182. */
  183. public function groups() {
  184. $result = array();
  185. foreach ($this->settings['groups'] as $group) {
  186. $value = $this->_Redis->get($this->settings['prefix'] . $group);
  187. if (!$value) {
  188. $value = 1;
  189. $this->_Redis->set($this->settings['prefix'] . $group, $value);
  190. }
  191. $result[] = $group . $value;
  192. }
  193. return $result;
  194. }
  195. /**
  196. * Increments the group value to simulate deletion of all keys under a group
  197. * old values will remain in storage until they expire.
  198. *
  199. * @param string $group The group name to clear.
  200. * @return bool success
  201. */
  202. public function clearGroup($group) {
  203. return (bool)$this->_Redis->incr($this->settings['prefix'] . $group);
  204. }
  205. /**
  206. * Disconnects from the redis server
  207. */
  208. public function __destruct() {
  209. if (!$this->settings['persistent']) {
  210. $this->_Redis->close();
  211. }
  212. }
  213. }