PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/dooframework/cache/DooPhpCache.php

http://github.com/darkredz/DooPHP
PHP | 200 lines | 106 code | 26 blank | 68 comment | 20 complexity | 6d3a7886939ee1da42504c4825e7f0a6 MD5 | raw file
  1. <?php
  2. /**
  3. * DooPhpCache class file.
  4. *
  5. * @author Leng Sheng Hong <darkredz@gmail.com>
  6. * @link http://www.doophp.com/
  7. * @copyright Copyright &copy; 2009 Leng Sheng Hong
  8. * @license http://www.doophp.com/license
  9. */
  10. /**
  11. * DooPhpCache provides file based caching which convert data into PHP variables.
  12. *
  13. * @author Leng Sheng Hong <darkredz@gmail.com>
  14. * @version $Id: DooPhpCache.php 1000 2009-08-29 14:18:10
  15. * @package doo.cache
  16. * @since 1.2
  17. */
  18. class DooPhpCache {
  19. private $_directory;
  20. /**
  21. * Option to hash the Cache ID into md5 hash string
  22. * @var bool
  23. */
  24. public $hashing = true;
  25. public function __construct($path='') {
  26. if ( $path=='' ) {
  27. if(isset(Doo::conf()->CACHE_PATH))
  28. $this->_directory = Doo::conf()->CACHE_PATH;
  29. else
  30. $this->_directory = Doo::conf()->SITE_PATH . Doo::conf()->PROTECTED_FOLDER . 'cache/';
  31. }else{
  32. $this->_directory = $path;
  33. }
  34. }
  35. /**
  36. * Retrieves a value from cache with an Id.
  37. *
  38. * @param string $id A unique key identifying the cache
  39. * @return mixed The value stored in cache. Return null if no cache found or already expired.
  40. */
  41. public function get($id) {
  42. if($this->hashing===true)
  43. $cfile = $this->_directory . md5($id) . '.php';
  44. else
  45. $cfile = $this->_directory . $id . '.php';
  46. if (file_exists($cfile)){
  47. include $cfile ;
  48. if(time() < $data[0]){
  49. return $data[1];
  50. }else{
  51. unlink($cfile);
  52. }
  53. }
  54. }
  55. /**
  56. * Retrieves a value from cache with an Id from different directories
  57. *
  58. * @param string $folder Directory name for the cache files stored
  59. * @param string $id A unique key identifying the cache
  60. * @return mixed The value stored in cache. Return null if no cache found or already expired.
  61. */
  62. public function getIn($folder, $id) {
  63. if($this->hashing===true)
  64. $cfile = $this->_directory . $folder .'/'. md5($id) .'.php';
  65. else
  66. $cfile = $this->_directory . $folder .'/'. $id .'.php';
  67. if (file_exists($cfile)){
  68. include $cfile ;
  69. if(time() < $data[0]){
  70. return $data[1];
  71. }else{
  72. unlink($cfile);
  73. }
  74. }
  75. }
  76. /**
  77. * Adds a cache with an unique Id.
  78. *
  79. * @param string $id Unique Id of the cache
  80. * @param mixed $value Cache data value to be stored.
  81. * @param int $expire Duration to determine if the cache is expired.
  82. * @return bool
  83. */
  84. public function set($id, $value, $expire=0) {
  85. if($expire===0)
  86. $expire = time()+31536000;
  87. else
  88. $expire = time()+$expire;
  89. if($this->hashing===true)
  90. return file_put_contents($this->_directory . md5($id) . '.php', '<?php $data = array('.$expire.', '. var_export($value, true) . '); ?>', LOCK_EX);
  91. return file_put_contents($this->_directory . $id . '.php', '<?php $data = array('.$expire.', '. var_export($value, true) . '); ?>', LOCK_EX);
  92. }
  93. /**
  94. * Store cache in different directories
  95. *
  96. * @param string $folder Directory name for the cache files to be created and stored
  97. * @param string $id Unique Id of the cache
  98. * @param mixed $value Cache data value to be stored.
  99. * @param int $expire Duration to determine if the cache is expired.
  100. * @return bool
  101. */
  102. public function setIn($folder, $id, $value, $expire=0) {
  103. $cfile = $this->_directory.$folder.'/';
  104. if(!file_exists($cfile))
  105. mkdir($cfile);
  106. if($this->hashing===true)
  107. $cfile .= md5($id) . '.php';
  108. else
  109. $cfile .= $id . '.php';
  110. if($expire===0)
  111. $expire = time()+31536000;
  112. else
  113. $expire = time()+$expire;
  114. return file_put_contents($cfile, '<?php $data = array('.$expire.', '. var_export($value, true) . '); ?>', LOCK_EX);
  115. }
  116. /**
  117. * Delete a cache file by Id
  118. * @param $id Id of the cache
  119. * @return mixed
  120. */
  121. public function flush($id) {
  122. if($this->hashing===true)
  123. $cfile = $this->_directory.md5($id).'.php';
  124. else
  125. $cfile = $this->_directory.$id.'.php';
  126. if (file_exists($cfile)) {
  127. unlink($cfile);
  128. return true;
  129. }
  130. return false;
  131. }
  132. /**
  133. * Deletes all data cache files
  134. * @return bool
  135. */
  136. public function flushAll() {
  137. $handle = opendir($this->_directory);
  138. while(($file = readdir($handle)) !== false) {
  139. if (is_file($file))
  140. unlink($file);
  141. }
  142. return true;
  143. }
  144. /**
  145. * Deletes all data cache in a folder
  146. * @param string $folder
  147. */
  148. public function flushAllIn($folder){
  149. $cfile = $this->_directory.$folder.'/';
  150. if(file_exists($cfile)){
  151. $handle = opendir($cfile);
  152. while(($file = readdir($handle)) !== false) {
  153. $file = $cfile.$file;
  154. if (is_file($file)){
  155. unlink( $file );
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * Deletes a data cache in a folder identified by an ID
  162. * @param string $folder
  163. * @param string $id
  164. */
  165. public function flushIn($folder, $id){
  166. if($this->hashing===true)
  167. $cfile = $this->_directory.$folder.'/'.md5($id).'.php';
  168. else
  169. $cfile = $this->_directory.$folder.'/'.$id.'.php';
  170. if(file_exists($cfile)){
  171. unlink( $file );
  172. }
  173. }
  174. }