PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/cache/engines/WaxCacheFilesystem.php

http://github.com/phpwax/phpwax
PHP | 83 lines | 58 code | 18 blank | 7 comment | 19 complexity | 947b4d68074c8413c8a77744db0182af MD5 | raw file
  1. <?php
  2. /**
  3. * @package PHP-Wax
  4. */
  5. /**
  6. * Engine for caching of data / objects to filesystem.
  7. * @package PHP-Wax
  8. */
  9. class WaxCacheFilesystem implements CacheEngine{
  10. public $key = false;
  11. public $root_dir = false;
  12. public $lifetime = 3600;
  13. public $writable = false;
  14. public function __construct($key, $options) {
  15. foreach($options as $k=>$option) $this->$k = $option;
  16. if(!$this->root_dir) $this->root_dir = CACHE_DIR;
  17. $this->key = $key;
  18. if(!is_writable($this->file_path()) && !is_dir($this->file_path())) mkdir($this->file_path(),0777,true);
  19. if(!is_writable($this->file())) $this->writable = false;
  20. else $this->writable = true;
  21. }
  22. public function get() {
  23. if($content = $this->valid()) return $content;
  24. return false;
  25. }
  26. public function set($value) {
  27. if(!is_readable($this->file())) file_put_contents($this->file(), $value);
  28. }
  29. public function valid() {
  30. if(!is_readable($this->file()) ) return false;
  31. $mtime = filemtime($this->file());
  32. if(time() > $mtime + $this->lifetime){
  33. $this->expire();
  34. return false;
  35. }else return file_get_contents($this->file());
  36. }
  37. public function expire($query = false) {
  38. if(!$query && is_readable($this->file())) return unlink($this->file());
  39. else $this->flush($query);
  40. }
  41. public function file() {
  42. return $this->root_dir.$this->key;
  43. }
  44. public function file_path() {
  45. return $this->root_dir.$this->key_path($this->key);
  46. }
  47. public function flush($query) {
  48. if(strlen($query)>1) {
  49. if(is_dir($this->root_dir.$query)) {
  50. File::recursively_delete($this->root_dir.$query);
  51. }
  52. } else foreach(scandir($this->root_dir) as $dir) File::recursively_delete($this->root_dir.$dir);
  53. }
  54. public function key_path($full_key) {
  55. if(strpos($full_key, "/")) {
  56. $path = substr($full_key, 0, strrpos($full_key, "/")+1);
  57. return $path;
  58. } else return "";
  59. }
  60. public function key_name($full_key) {
  61. if(strpos($full_key, "/")) {
  62. return substr(strrchr($full_key, "/"),1);
  63. } else return $full_key;
  64. }
  65. }