PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/application/libraries/Cache.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 258 lines | 114 code | 76 blank | 68 comment | 28 complexity | 6a2e346c3ddd2515ec627c94cfc6cac7 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. //http://codeigniter.com/forums/viewthread/57117/
  3. class Cache
  4. {
  5. public $options = array(
  6. 'cache_postfix' => '.cache', //Prefix to all cache filenames
  7. 'expiry_postfix' => '.exp', //Expiry file prefix
  8. 'group_postfix' => '.group', //Group directory prefix
  9. 'default_ttl' => 3600 //Default time to live = 3600 seconds (One hour).
  10. );
  11. /**
  12. * Constructor
  13. *
  14. * @param Options to override defaults
  15. */
  16. function __construct($options = NULL)
  17. {
  18. if ($options != NULL) $this->options = array_merge($this->options, $options);
  19. }
  20. /**
  21. * Check if a item has a (valid) cache
  22. *
  23. * @param Cache id
  24. * @param Cache group id (optional)
  25. * @return Boolean indicating if cache available
  26. */
  27. function is_cached($cache_id, $cache_group = NULL)
  28. {
  29. if ($this->_get_expiry($cache_id, $cache_group) > time()) return TRUE;
  30. $this->remove($cache_id, $cache_group);
  31. return FALSE;
  32. }
  33. /**
  34. * Save an item to the cache
  35. *
  36. * @param Cache id
  37. * @param Data object
  38. * @param Cache group id (optional)
  39. * @param Time to live for this item
  40. */
  41. function save($cache_id, $data, $cache_group = NULL, $ttl = NULL)
  42. {
  43. if ($cache_group !== NULL)
  44. {
  45. $group_dir = $this->_group_dir($cache_group);
  46. if (!file_exists($group_dir)) mkdir($group_dir);
  47. }
  48. $file = $this->_file($cache_id, $cache_group);
  49. $cache_file = $file.$this->options['cache_postfix'];
  50. $expiry_file = $file.$this->options['expiry_postfix'];
  51. if ($ttl === NULL) $ttl = $this->options['default_ttl'];
  52. //
  53. // Ok, so setting ttl = 0 is not quite forever, but 1000 years
  54. // Is your PHP code going to be running for 1000 years? If so dont use this library (or just regenerate the cache then)!
  55. //
  56. if ($ttl == 0) $ttl = 31536000000; //1000 years in seconds
  57. $expire_time = time() + $ttl;
  58. $f1 = fopen($expiry_file, 'w');
  59. $f2 = fopen($cache_file, 'w');
  60. flock($f1, LOCK_EX);
  61. flock($f2, LOCK_EX);
  62. fwrite($f1, $expire_time);
  63. fwrite($f2, serialize($data));
  64. flock($f1, LOCK_UN);
  65. flock($f2, LOCK_UN);
  66. fclose($f1);
  67. fclose($f2);
  68. }
  69. /**
  70. * Get and return an item from the cache
  71. *
  72. * @param Cache Id
  73. * @param Cache group Id
  74. * @param Should I check the expiry time?
  75. * @return The object or NULL if not available
  76. */
  77. function get($cache_id, $cache_group = NULL, $skip_checking = FALSE)
  78. {
  79. if (!$skip_checking && !$this->is_cached($cache_id, $cache_group)) return NULL;
  80. $cache_file = $this->_file($cache_id, $cache_group).$this->options['cache_postfix'];
  81. if (!is_file($cache_file)) return NULL;
  82. return unserialize(file_get_contents($cache_file));
  83. }
  84. /**
  85. * Remove an item from the cache
  86. *
  87. * @param Cache Id
  88. * @param Cache group Id
  89. */
  90. function remove($cache_id, $cache_group = NULL)
  91. {
  92. $file = $this->_file($cache_id, $cache_group);
  93. $cache_file = $file.$this->options['cache_postfix'];
  94. $expiry_file = $file.$this->options['expiry_postfix'];
  95. @unlink($cache_file);
  96. @unlink($expiry_file);
  97. }
  98. /**
  99. * Remove an entire group
  100. *
  101. * @param Cache group Id
  102. */
  103. function remove_group($cache_group)
  104. {
  105. $group_dir = $this->_group_dir($cache_group);
  106. //
  107. // Empty the directory
  108. //
  109. if (!file_exists($group_dir) || !$dh = @opendir($group_dir)) return;
  110. while (($obj = readdir($dh))) {
  111. if($obj=='.' || $obj=='..') continue;
  112. @unlink($group_dir.'/'.$obj);
  113. }
  114. closedir($dh);
  115. //
  116. // Delete the dir for tidyness
  117. //
  118. @rmdir($group_dir);
  119. }
  120. /**
  121. * Remove an array of cached items
  122. *
  123. * @param Array of cache ids
  124. * @param Cache group Id
  125. */
  126. function remove_ids($cache_ids, $cache_group = NULL)
  127. {
  128. if (!is_array($cache_ids)) $cache_ids = array($cache_ids);
  129. //
  130. // Hash all ids
  131. //
  132. $hashes = array();
  133. foreach($cache_ids as $cache_id)
  134. {
  135. $hashes[] = md5($cache_id);
  136. }
  137. $group_dir = $this->_group_dir($cache_group);
  138. //
  139. // Delete matching files
  140. //
  141. if(!$dh = @opendir($group_dir)) return;
  142. $filecount = 0;
  143. $delcount = 0;
  144. while (($obj = readdir($dh))) {
  145. if($obj=='.' || $obj=='..') continue;
  146. $parts = explode(".", $obj);
  147. $hash = $parts[0];
  148. if (in_array($hash, $hashes))
  149. {
  150. @unlink($group_dir.'/'.$obj);
  151. $delcount++;
  152. }
  153. $filecount ++;
  154. }
  155. closedir($dh);
  156. //
  157. // Delete the dir if empty
  158. //
  159. if ($filecount == $delcount) @rmdir($group_dir);
  160. }
  161. //
  162. // Private methods
  163. //
  164. private function _get_expiry($cache_id, $cache_group = NULL)
  165. {
  166. $file = $this->_file($cache_id, $cache_group).$this->options['expiry_postfix'];
  167. if (!is_file($file)) return 0;
  168. return intval(file_get_contents($file));
  169. }
  170. function _file($cache_id, $cache_group = NULL)
  171. {
  172. return $this->_group_dir($cache_group).'/'.md5($cache_id);
  173. }
  174. private function _group_dir($cache_group)
  175. {
  176. $CI =& get_instance();
  177. $dir = ($cache_group != NULL) ? md5($cache_group).$this->options['group_postfix'] : '';
  178. $cache_path = ($CI->config->item('cache_path') != '') ? $CI->config->item('cache_path') : APPPATH.'cache/';
  179. return $cache_path.$dir;
  180. }
  181. }
  182. ?>