PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/FILECACHE.php

https://bitbucket.org/vodolaz095/trivia_core
PHP | 162 lines | 123 code | 20 blank | 19 comment | 11 complexity | e5318f75a7d38f6dcbc1315ac292c0f3 MD5 | raw file
  1. <?php
  2. class FileCacheException extends Exception
  3. {
  4. }
  5. class FILECACHE
  6. {
  7. protected static $instance;
  8. private $folder='/var/tmp/trivia/';
  9. private static function init()
  10. {
  11. if ( is_null(self::$instance) )
  12. {
  13. self::$instance = new FILECACHE();
  14. }
  15. return self::$instance;
  16. }
  17. private function __construct()
  18. {
  19. /* for now - do nothing...*/
  20. }
  21. public static function setCacheDir($directory=null)
  22. {
  23. $fc=FILECACHE::init();
  24. if($directory and is_dir($directory))
  25. {
  26. if(is_writable($directory))
  27. {
  28. $fc->folder=$directory;
  29. }
  30. else
  31. {
  32. throw new FileCacheException('Cache directory '.$directory.' is not writable!');
  33. }
  34. }
  35. elseif(mkdir($directory,0777))
  36. {
  37. $fc->folder=$directory;
  38. }
  39. else
  40. {
  41. throw new FileCacheException('Cache directory '.$directory.' does not exists and we cannot create it!');
  42. }
  43. }
  44. public static function set($key,$value_or_closure,$duration,$devel=false)
  45. {
  46. $ans=FILECACHE::get($key,$devel);
  47. if($ans)
  48. {
  49. return $ans;
  50. }
  51. else
  52. {
  53. if(is_scalar($value_or_closure))
  54. $a=$value_or_closure;
  55. else
  56. $a=$value_or_closure();
  57. if($devel)
  58. $ans='<div title="saved to filecache with key *'.$key.'*" and ttl='.$duration.'>'.$a.'</div>';
  59. else
  60. $ans=$a;
  61. return FILECACHE::setValue($key,$a,$duration,$devel) ? $ans : false;
  62. }
  63. }
  64. protected static function setValue($key,$value,$ttl)
  65. {
  66. if(preg_match('~^[a-z0-9_]+$~i',$key))
  67. {
  68. $fc=FILECACHE::init();
  69. $time=$ttl+time();
  70. foreach (glob($fc->folder.$key.'.*.tmp') as $filename)
  71. {
  72. //echo __FUNCTION__.'unlinking '.$filename.PHP_EOL;
  73. unlink($filename);
  74. }
  75. return (file_put_contents($fc->folder.$key.'.'.$time.'.tmp',$value)? $value : false);
  76. }
  77. else
  78. {
  79. throw new FileCacheException($key.' - is a bad key name!');
  80. }
  81. }
  82. public static function get($key,$devel=false)
  83. {
  84. $fc=FILECACHE::init();
  85. foreach (glob($fc->folder.$key.'.*.tmp') as $filename)
  86. {
  87. $strlen=strlen($fc->folder);
  88. if(preg_match('~^([a-z0-9_]+)\.(\d+)\.tmp$~i',substr($filename,$strlen),$a))
  89. {
  90. $novue=$a[2]-time();
  91. if($novue>0)
  92. {
  93. $a=file_get_contents($filename);
  94. if($devel) $a='<div title="retrived from filecache with key *'.$key.'* from directory *'.$filename.'*" and ttl='.$novue.'>'.$a.'</div>';
  95. return $a;
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. }
  108. public static function del($key)
  109. {
  110. $fc=FILECACHE::init();
  111. foreach (glob($fc->folder.$key.'.*.tmp') as $filename)
  112. {
  113. unlink($filename);
  114. }
  115. }
  116. public static function flush()
  117. {
  118. $fc=FILECACHE::init();
  119. foreach (glob($fc->folder.'*.tmp') as $filename)
  120. {
  121. if(!unlink($filename)) throw new FileCacheException('Unable to remove filecache entry '.$filename.'!');
  122. }
  123. return true;
  124. }
  125. }
  126. /*
  127. //example of code
  128. date_default_timezone_set('UTC');
  129. echo 'Closure test:'.PHP_EOL;
  130. //FILECACHE::flush();
  131. $text='Lalala - setted on '.date('r');
  132. echo date('r').PHP_EOL;
  133. echo FILECACHE::run('lalala',function(){
  134. return 'closure '.date('r');
  135. },5,true).PHP_EOL;;
  136. //echo 'setting = '.FILECACHE::set('blablabla',date('r'),5,true).PHP_EOL;
  137. echo 'getting = '.FILECACHE::get('blablabla',true).PHP_EOL;
  138. //FILECACHE::flush();
  139. //*/