Cache_file.php 4.0 KB

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