Cache_file.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_file extends CI_Driver {
  27. protected $_cache_path;
  28. /**
  29. * Constructor
  30. */
  31. public function __construct()
  32. {
  33. $CI =& get_instance();
  34. $CI->load->helper('file');
  35. $path = $CI->config->item('cache_path');
  36. $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
  37. }
  38. // ------------------------------------------------------------------------
  39. /**
  40. * Fetch from cache
  41. *
  42. * @param mixed unique key id
  43. * @return mixed data on success/false on failure
  44. */
  45. public function get($id)
  46. {
  47. if ( ! file_exists($this->_cache_path.$id))
  48. {
  49. return FALSE;
  50. }
  51. $data = read_file($this->_cache_path.$id);
  52. $data = unserialize($data);
  53. if (time() > $data['time'] + $data['ttl'])
  54. {
  55. unlink($this->_cache_path.$id);
  56. return FALSE;
  57. }
  58. return $data['data'];
  59. }
  60. // ------------------------------------------------------------------------
  61. /**
  62. * Save into cache
  63. *
  64. * @param string unique key
  65. * @param mixed data to store
  66. * @param int length of time (in seconds) the cache is valid
  67. * - Default is 60 seconds
  68. * @return boolean true on success/false on failure
  69. */
  70. public function save($id, $data, $ttl = 60)
  71. {
  72. $contents = array(
  73. 'time' => time(),
  74. 'ttl' => $ttl,
  75. 'data' => $data
  76. );
  77. if (write_file($this->_cache_path.$id, serialize($contents)))
  78. {
  79. @chmod($this->_cache_path.$id, 0777);
  80. return TRUE;
  81. }
  82. return FALSE;
  83. }
  84. // ------------------------------------------------------------------------
  85. /**
  86. * Delete from Cache
  87. *
  88. * @param mixed unique identifier of item in cache
  89. * @return boolean true on success/false on failure
  90. */
  91. public function delete($id)
  92. {
  93. return unlink($this->_cache_path.$id);
  94. }
  95. // ------------------------------------------------------------------------
  96. /**
  97. * Clean the Cache
  98. *
  99. * @return boolean false on failure/true on success
  100. */
  101. public function clean()
  102. {
  103. return delete_files($this->_cache_path);
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * Cache Info
  108. *
  109. * Not supported by file-based caching
  110. *
  111. * @param string user/filehits
  112. * @return mixed FALSE
  113. */
  114. public function cache_info($type = NULL)
  115. {
  116. return get_dir_file_info($this->_cache_path);
  117. }
  118. // ------------------------------------------------------------------------
  119. /**
  120. * Get Cache Metadata
  121. *
  122. * @param mixed key to get cache metadata on
  123. * @return mixed FALSE on failure, array on success.
  124. */
  125. public function get_metadata($id)
  126. {
  127. if ( ! file_exists($this->_cache_path.$id))
  128. {
  129. return FALSE;
  130. }
  131. $data = read_file($this->_cache_path.$id);
  132. $data = unserialize($data);
  133. if (is_array($data))
  134. {
  135. $mtime = filemtime($this->_cache_path.$id);
  136. if ( ! isset($data['ttl']))
  137. {
  138. return FALSE;
  139. }
  140. return array(
  141. 'expire' => $mtime + $data['ttl'],
  142. 'mtime' => $mtime
  143. );
  144. }
  145. return FALSE;
  146. }
  147. // ------------------------------------------------------------------------
  148. /**
  149. * Is supported
  150. *
  151. * In the file driver, check to see that the cache directory is indeed writable
  152. *
  153. * @return boolean
  154. */
  155. public function is_supported()
  156. {
  157. return is_really_writable($this->_cache_path);
  158. }
  159. }
  160. /* End of file Cache_file.php */
  161. /* Location: ./system/libraries/Cache/drivers/Cache_file.php */