Cache_memcached.php 4.9 KB

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