Cache_memcached.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2006 - 2014 EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 2.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter Memcached Caching Class
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Libraries
  22. * @category Core
  23. * @author EllisLab Dev Team
  24. * @link
  25. */
  26. class CI_Cache_memcached extends CI_Driver {
  27. private $_memcached; // Holds the memcached object
  28. protected $_memcache_conf = array(
  29. 'default' => array(
  30. 'default_host' => '127.0.0.1',
  31. 'default_port' => 11211,
  32. 'default_weight' => 1
  33. )
  34. );
  35. // ------------------------------------------------------------------------
  36. /**
  37. * Fetch from cache
  38. *
  39. * @param mixed unique key id
  40. * @return mixed data on success/false on failure
  41. */
  42. public function get($id)
  43. {
  44. $data = $this->_memcached->get($id);
  45. return (is_array($data)) ? $data[0] : FALSE;
  46. }
  47. // ------------------------------------------------------------------------
  48. /**
  49. * Save
  50. *
  51. * @param string unique identifier
  52. * @param mixed data being cached
  53. * @param int time to live
  54. * @return boolean true on success, false on failure
  55. */
  56. public function save($id, $data, $ttl = 60)
  57. {
  58. if (get_class($this->_memcached) == 'Memcached')
  59. {
  60. return $this->_memcached->set($id, array($data, time(), $ttl), $ttl);
  61. }
  62. else if (get_class($this->_memcached) == 'Memcache')
  63. {
  64. return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl);
  65. }
  66. return FALSE;
  67. }
  68. // ------------------------------------------------------------------------
  69. /**
  70. * Delete from Cache
  71. *
  72. * @param mixed key to be deleted.
  73. * @return boolean true on success, false on failure
  74. */
  75. public function delete($id)
  76. {
  77. return $this->_memcached->delete($id);
  78. }
  79. // ------------------------------------------------------------------------
  80. /**
  81. * Clean the Cache
  82. *
  83. * @return boolean false on failure/true on success
  84. */
  85. public function clean()
  86. {
  87. return $this->_memcached->flush();
  88. }
  89. // ------------------------------------------------------------------------
  90. /**
  91. * Cache Info
  92. *
  93. * @param null type not supported in memcached
  94. * @return mixed array on success, false on failure
  95. */
  96. public function cache_info($type = NULL)
  97. {
  98. return $this->_memcached->getStats();
  99. }
  100. // ------------------------------------------------------------------------
  101. /**
  102. * Get Cache Metadata
  103. *
  104. * @param mixed key to get cache metadata on
  105. * @return mixed FALSE on failure, array on success.
  106. */
  107. public function get_metadata($id)
  108. {
  109. $stored = $this->_memcached->get($id);
  110. if (count($stored) !== 3)
  111. {
  112. return FALSE;
  113. }
  114. list($data, $time, $ttl) = $stored;
  115. return array(
  116. 'expire' => $time + $ttl,
  117. 'mtime' => $time,
  118. 'data' => $data
  119. );
  120. }
  121. // ------------------------------------------------------------------------
  122. /**
  123. * Setup memcached.
  124. */
  125. private function _setup_memcached()
  126. {
  127. // Try to load memcached server info from the config file.
  128. $CI =& get_instance();
  129. if ($CI->config->load('memcached', TRUE, TRUE))
  130. {
  131. if (is_array($CI->config->config['memcached']))
  132. {
  133. $this->_memcache_conf = NULL;
  134. foreach ($CI->config->config['memcached'] as $name => $conf)
  135. {
  136. $this->_memcache_conf[$name] = $conf;
  137. }
  138. }
  139. }
  140. $this->_memcached = new Memcached();
  141. foreach ($this->_memcache_conf as $name => $cache_server)
  142. {
  143. if ( ! array_key_exists('hostname', $cache_server))
  144. {
  145. $cache_server['hostname'] = $this->_default_options['default_host'];
  146. }
  147. if ( ! array_key_exists('port', $cache_server))
  148. {
  149. $cache_server['port'] = $this->_default_options['default_port'];
  150. }
  151. if ( ! array_key_exists('weight', $cache_server))
  152. {
  153. $cache_server['weight'] = $this->_default_options['default_weight'];
  154. }
  155. $this->_memcached->addServer(
  156. $cache_server['hostname'], $cache_server['port'], $cache_server['weight']
  157. );
  158. }
  159. }
  160. // ------------------------------------------------------------------------
  161. /**
  162. * Is supported
  163. *
  164. * Returns FALSE if memcached is not supported on the system.
  165. * If it is, we setup the memcached object & return TRUE
  166. */
  167. public function is_supported()
  168. {
  169. if ( ! extension_loaded('memcached'))
  170. {
  171. log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
  172. return FALSE;
  173. }
  174. $this->_setup_memcached();
  175. return TRUE;
  176. }
  177. }
  178. /* End of file Cache_memcached.php */
  179. /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */