You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
7.3 KiB

7 years ago
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 2.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Memcached Caching Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Core
  45. * @author EllisLab Dev Team
  46. * @link
  47. */
  48. class CI_Cache_memcached extends CI_Driver {
  49. /**
  50. * Holds the memcached object
  51. *
  52. * @var object
  53. */
  54. protected $_memcached;
  55. /**
  56. * Memcached configuration
  57. *
  58. * @var array
  59. */
  60. protected $_config = array(
  61. 'default' => array(
  62. 'host' => '127.0.0.1',
  63. 'port' => 11211,
  64. 'weight' => 1
  65. )
  66. );
  67. // ------------------------------------------------------------------------
  68. /**
  69. * Class constructor
  70. *
  71. * Setup Memcache(d)
  72. *
  73. * @return void
  74. */
  75. public function __construct()
  76. {
  77. // Try to load memcached server info from the config file.
  78. $CI =& get_instance();
  79. $defaults = $this->_config['default'];
  80. if ($CI->config->load('memcached', TRUE, TRUE))
  81. {
  82. $this->_config = $CI->config->config['memcached'];
  83. }
  84. if (class_exists('Memcached', FALSE))
  85. {
  86. $this->_memcached = new Memcached();
  87. }
  88. elseif (class_exists('Memcache', FALSE))
  89. {
  90. $this->_memcached = new Memcache();
  91. }
  92. else
  93. {
  94. log_message('error', 'Cache: Failed to create Memcache(d) object; extension not loaded?');
  95. return;
  96. }
  97. foreach ($this->_config as $cache_server)
  98. {
  99. isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host'];
  100. isset($cache_server['port']) OR $cache_server['port'] = $defaults['port'];
  101. isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight'];
  102. if ($this->_memcached instanceof Memcache)
  103. {
  104. // Third parameter is persistence and defaults to TRUE.
  105. $this->_memcached->addServer(
  106. $cache_server['hostname'],
  107. $cache_server['port'],
  108. TRUE,
  109. $cache_server['weight']
  110. );
  111. }
  112. elseif ($this->_memcached instanceof Memcached)
  113. {
  114. $this->_memcached->addServer(
  115. $cache_server['hostname'],
  116. $cache_server['port'],
  117. $cache_server['weight']
  118. );
  119. }
  120. }
  121. }
  122. // ------------------------------------------------------------------------
  123. /**
  124. * Fetch from cache
  125. *
  126. * @param string $id Cache ID
  127. * @return mixed Data on success, FALSE on failure
  128. */
  129. public function get($id)
  130. {
  131. $data = $this->_memcached->get($id);
  132. return is_array($data) ? $data[0] : $data;
  133. }
  134. // ------------------------------------------------------------------------
  135. /**
  136. * Save
  137. *
  138. * @param string $id Cache ID
  139. * @param mixed $data Data being cached
  140. * @param int $ttl Time to live
  141. * @param bool $raw Whether to store the raw value
  142. * @return bool TRUE on success, FALSE on failure
  143. */
  144. public function save($id, $data, $ttl = 60, $raw = FALSE)
  145. {
  146. if ($raw !== TRUE)
  147. {
  148. $data = array($data, time(), $ttl);
  149. }
  150. if ($this->_memcached instanceof Memcached)
  151. {
  152. return $this->_memcached->set($id, $data, $ttl);
  153. }
  154. elseif ($this->_memcached instanceof Memcache)
  155. {
  156. return $this->_memcached->set($id, $data, 0, $ttl);
  157. }
  158. return FALSE;
  159. }
  160. // ------------------------------------------------------------------------
  161. /**
  162. * Delete from Cache
  163. *
  164. * @param mixed $id key to be deleted.
  165. * @return bool true on success, false on failure
  166. */
  167. public function delete($id)
  168. {
  169. return $this->_memcached->delete($id);
  170. }
  171. // ------------------------------------------------------------------------
  172. /**
  173. * Increment a raw value
  174. *
  175. * @param string $id Cache ID
  176. * @param int $offset Step/value to add
  177. * @return mixed New value on success or FALSE on failure
  178. */
  179. public function increment($id, $offset = 1)
  180. {
  181. return $this->_memcached->increment($id, $offset);
  182. }
  183. // ------------------------------------------------------------------------
  184. /**
  185. * Decrement a raw value
  186. *
  187. * @param string $id Cache ID
  188. * @param int $offset Step/value to reduce by
  189. * @return mixed New value on success or FALSE on failure
  190. */
  191. public function decrement($id, $offset = 1)
  192. {
  193. return $this->_memcached->decrement($id, $offset);
  194. }
  195. // ------------------------------------------------------------------------
  196. /**
  197. * Clean the Cache
  198. *
  199. * @return bool false on failure/true on success
  200. */
  201. public function clean()
  202. {
  203. return $this->_memcached->flush();
  204. }
  205. // ------------------------------------------------------------------------
  206. /**
  207. * Cache Info
  208. *
  209. * @return mixed array on success, false on failure
  210. */
  211. public function cache_info()
  212. {
  213. return $this->_memcached->getStats();
  214. }
  215. // ------------------------------------------------------------------------
  216. /**
  217. * Get Cache Metadata
  218. *
  219. * @param mixed $id key to get cache metadata on
  220. * @return mixed FALSE on failure, array on success.
  221. */
  222. public function get_metadata($id)
  223. {
  224. $stored = $this->_memcached->get($id);
  225. if (count($stored) !== 3)
  226. {
  227. return FALSE;
  228. }
  229. list($data, $time, $ttl) = $stored;
  230. return array(
  231. 'expire' => $time + $ttl,
  232. 'mtime' => $time,
  233. 'data' => $data
  234. );
  235. }
  236. // ------------------------------------------------------------------------
  237. /**
  238. * Is supported
  239. *
  240. * Returns FALSE if memcached is not supported on the system.
  241. * If it is, we setup the memcached object & return TRUE
  242. *
  243. * @return bool
  244. */
  245. public function is_supported()
  246. {
  247. return (extension_loaded('memcached') OR extension_loaded('memcache'));
  248. }
  249. // ------------------------------------------------------------------------
  250. /**
  251. * Class destructor
  252. *
  253. * Closes the connection to Memcache(d) if present.
  254. *
  255. * @return void
  256. */
  257. public function __destruct()
  258. {
  259. if ($this->_memcached instanceof Memcache)
  260. {
  261. $this->_memcached->close();
  262. }
  263. elseif ($this->_memcached instanceof Memcached && method_exists($this->_memcached, 'quit'))
  264. {
  265. $this->_memcached->quit();
  266. }
  267. }
  268. }