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