/system/libraries/Cache/drivers/Cache_file.php

https://github.com/andigehle/CodeIgniter · PHP · 203 lines · 76 code · 29 blank · 98 comment · 8 complexity · efa808cfaf462d024a3856ae7b9cf6f2 MD5 · raw file

  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.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 2.0
  25. * @filesource
  26. */
  27. /**
  28. * CodeIgniter Memcached Caching Class
  29. *
  30. * @package CodeIgniter
  31. * @subpackage Libraries
  32. * @category Core
  33. * @author EllisLab Dev Team
  34. * @link
  35. */
  36. class CI_Cache_file extends CI_Driver {
  37. /**
  38. * Directory in which to save cache files
  39. *
  40. * @var string
  41. */
  42. protected $_cache_path;
  43. /**
  44. * Initialize file-based cache
  45. */
  46. public function __construct()
  47. {
  48. $CI =& get_instance();
  49. $CI->load->helper('file');
  50. $path = $CI->config->item('cache_path');
  51. $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
  52. }
  53. // ------------------------------------------------------------------------
  54. /**
  55. * Fetch from cache
  56. *
  57. * @param mixed unique key id
  58. * @return mixed data on success/false on failure
  59. */
  60. public function get($id)
  61. {
  62. if ( ! file_exists($this->_cache_path.$id))
  63. {
  64. return FALSE;
  65. }
  66. $data = unserialize(read_file($this->_cache_path.$id));
  67. if (time() > $data['time'] + $data['ttl'])
  68. {
  69. unlink($this->_cache_path.$id);
  70. return FALSE;
  71. }
  72. return $data['data'];
  73. }
  74. // ------------------------------------------------------------------------
  75. /**
  76. * Save into cache
  77. *
  78. * @param string unique key
  79. * @param mixed data to store
  80. * @param int length of time (in seconds) the cache is valid
  81. * - Default is 60 seconds
  82. * @return bool true on success/false on failure
  83. */
  84. public function save($id, $data, $ttl = 60)
  85. {
  86. $contents = array(
  87. 'time' => time(),
  88. 'ttl' => $ttl,
  89. 'data' => $data
  90. );
  91. if (write_file($this->_cache_path.$id, serialize($contents)))
  92. {
  93. @chmod($this->_cache_path.$id, 0660);
  94. return TRUE;
  95. }
  96. return FALSE;
  97. }
  98. // ------------------------------------------------------------------------
  99. /**
  100. * Delete from Cache
  101. *
  102. * @param mixed unique identifier of item in cache
  103. * @return bool true on success/false on failure
  104. */
  105. public function delete($id)
  106. {
  107. return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
  108. }
  109. // ------------------------------------------------------------------------
  110. /**
  111. * Clean the Cache
  112. *
  113. * @return bool false on failure/true on success
  114. */
  115. public function clean()
  116. {
  117. return delete_files($this->_cache_path);
  118. }
  119. // ------------------------------------------------------------------------
  120. /**
  121. * Cache Info
  122. *
  123. * Not supported by file-based caching
  124. *
  125. * @param string user/filehits
  126. * @return mixed FALSE
  127. */
  128. public function cache_info($type = NULL)
  129. {
  130. return get_dir_file_info($this->_cache_path);
  131. }
  132. // ------------------------------------------------------------------------
  133. /**
  134. * Get Cache Metadata
  135. *
  136. * @param mixed key to get cache metadata on
  137. * @return mixed FALSE on failure, array on success.
  138. */
  139. public function get_metadata($id)
  140. {
  141. if ( ! file_exists($this->_cache_path.$id))
  142. {
  143. return FALSE;
  144. }
  145. $data = unserialize(read_file($this->_cache_path.$id));
  146. if (is_array($data))
  147. {
  148. $mtime = filemtime($this->_cache_path.$id);
  149. if ( ! isset($data['data']['ttl']))
  150. {
  151. return FALSE;
  152. }
  153. return array(
  154. 'expire' => $mtime + $data['data']['ttl'],
  155. 'mtime' => $mtime
  156. );
  157. }
  158. return FALSE;
  159. }
  160. // ------------------------------------------------------------------------
  161. /**
  162. * Is supported
  163. *
  164. * In the file driver, check to see that the cache directory is indeed writable
  165. *
  166. * @return bool
  167. */
  168. public function is_supported()
  169. {
  170. return is_really_writable($this->_cache_path);
  171. }
  172. }
  173. /* End of file Cache_file.php */
  174. /* Location: ./system/libraries/Cache/drivers/Cache_file.php */