Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2. class fileCacheHandler
  3. {
  4. private static $_instance = null;
  5. private static $cacheDir = "/dev/shm/cache_file/";
  6. public static function getInstance()
  7. {
  8. if (!static::$_instance) {
  9. static::$_instance = new static;
  10. }
  11. if(!is_dir(static::$cacheDir)){
  12. mkdir(static::$cacheDir,0777);
  13. }
  14. return static::$_instance;
  15. }
  16.  
  17. public function set($k, $v)
  18. {
  19. $cacheFile = static::$cacheDir.$k;
  20. return file_put_contents($cacheFile, $v);
  21. }
  22.  
  23. public function get($k)
  24. {
  25. $cacheFile = static::$cacheDir.$k;
  26. clearstatcache(true, $cacheFile);
  27. $mtime = filemtime($cacheFile);
  28. if (time() > intval($mtime)) {
  29. return false;
  30. }
  31. return file_get_contents($cacheFile);
  32. }
  33.  
  34. public function expire($k, $expiresAfter = 300)
  35. {
  36. $cacheFile = static::$cacheDir.$k;
  37. return touch($cacheFile, time() + $expiresAfter);
  38. }
  39.  
  40. public function clearCache()
  41. {
  42. $res = true;
  43. $files = array_diff(scandir(static::$cacheDir), array('.','..'));
  44. foreach ($files as $file) {
  45. $f = sprintf('%s/%s', static::$cacheDir, $file);
  46. $res = $res && (is_file($f) ? unlink($f) : false);
  47. }
  48. return $res;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement