Cache.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 Caching Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Core
  22. * @author ExpressionEngine Dev Team
  23. * @link
  24. */
  25. class CI_Cache extends CI_Driver_Library {
  26. protected $valid_drivers = array(
  27. 'cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy'
  28. );
  29. protected $_cache_path = NULL; // Path of cache files (if file-based cache)
  30. protected $_adapter = 'dummy';
  31. protected $_backup_driver;
  32. // ------------------------------------------------------------------------
  33. /**
  34. * Constructor
  35. *
  36. * @param array
  37. */
  38. public function __construct($config = array())
  39. {
  40. if ( ! empty($config))
  41. {
  42. $this->_initialize($config);
  43. }
  44. }
  45. // ------------------------------------------------------------------------
  46. /**
  47. * Get
  48. *
  49. * Look for a value in the cache. If it exists, return the data
  50. * if not, return FALSE
  51. *
  52. * @param string
  53. * @return mixed value that is stored/FALSE on failure
  54. */
  55. public function get($id)
  56. {
  57. return $this->{$this->_adapter}->get($id);
  58. }
  59. // ------------------------------------------------------------------------
  60. /**
  61. * Cache Save
  62. *
  63. * @param string Unique Key
  64. * @param mixed Data to store
  65. * @param int Length of time (in seconds) to cache the data
  66. *
  67. * @return boolean true on success/false on failure
  68. */
  69. public function save($id, $data, $ttl = 60)
  70. {
  71. return $this->{$this->_adapter}->save($id, $data, $ttl);
  72. }
  73. // ------------------------------------------------------------------------
  74. /**
  75. * Delete from Cache
  76. *
  77. * @param mixed unique identifier of the item in the cache
  78. * @return boolean true on success/false on failure
  79. */
  80. public function delete($id)
  81. {
  82. return $this->{$this->_adapter}->delete($id);
  83. }
  84. // ------------------------------------------------------------------------
  85. /**
  86. * Clean the cache
  87. *
  88. * @return boolean false on failure/true on success
  89. */
  90. public function clean()
  91. {
  92. return $this->{$this->_adapter}->clean();
  93. }
  94. // ------------------------------------------------------------------------
  95. /**
  96. * Cache Info
  97. *
  98. * @param string user/filehits
  99. * @return mixed array on success, false on failure
  100. */
  101. public function cache_info($type = 'user')
  102. {
  103. return $this->{$this->_adapter}->cache_info($type);
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * Get Cache Metadata
  108. *
  109. * @param mixed key to get cache metadata on
  110. * @return mixed return value from child method
  111. */
  112. public function get_metadata($id)
  113. {
  114. return $this->{$this->_adapter}->get_metadata($id);
  115. }
  116. // ------------------------------------------------------------------------
  117. /**
  118. * Initialize
  119. *
  120. * Initialize class properties based on the configuration array.
  121. *
  122. * @param array
  123. * @return void
  124. */
  125. private function _initialize($config)
  126. {
  127. $default_config = array(
  128. 'adapter',
  129. 'memcached'
  130. );
  131. foreach ($default_config as $key)
  132. {
  133. if (isset($config[$key]))
  134. {
  135. $param = '_'.$key;
  136. $this->{$param} = $config[$key];
  137. }
  138. }
  139. if (isset($config['backup']))
  140. {
  141. if (in_array('cache_'.$config['backup'], $this->valid_drivers))
  142. {
  143. $this->_backup_driver = $config['backup'];
  144. }
  145. }
  146. }
  147. // ------------------------------------------------------------------------
  148. /**
  149. * Is the requested driver supported in this environment?
  150. *
  151. * @param string The driver to test.
  152. * @return array
  153. */
  154. public function is_supported($driver)
  155. {
  156. static $support = array();
  157. if ( ! isset($support[$driver]))
  158. {
  159. $support[$driver] = $this->{$driver}->is_supported();
  160. }
  161. return $support[$driver];
  162. }
  163. // ------------------------------------------------------------------------
  164. /**
  165. * __get()
  166. *
  167. * @param child
  168. * @return object
  169. */
  170. public function __get($child)
  171. {
  172. $obj = parent::__get($child);
  173. if ( ! $this->is_supported($child))
  174. {
  175. $this->_adapter = $this->_backup_driver;
  176. }
  177. return $obj;
  178. }
  179. // ------------------------------------------------------------------------
  180. }
  181. // End Class
  182. /* End of file Cache.php */
  183. /* Location: ./system/libraries/Cache/Cache.php */