FileCache.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Cache with file system
  4. */
  5. class FileCache implements CacheInterface {
  6. protected $path;
  7. protected $param;
  8. public function loadData(){
  9. return json_decode(file_get_contents($this->getCacheFile()), true);
  10. }
  11. public function saveData($datas){
  12. $writeStream = file_put_contents($this->getCacheFile(), json_encode($datas, JSON_PRETTY_PRINT));
  13. if(!$writeStream) {
  14. throw new \Exception("Cannot write the cache... Do you have the right permissions ?");
  15. }
  16. return $this;
  17. }
  18. public function getTime(){
  19. $cacheFile = $this->getCacheFile();
  20. if(file_exists($cacheFile)){
  21. return filemtime($cacheFile);
  22. }
  23. return false;
  24. }
  25. public function purgeCache($duration){
  26. $cachePath = $this->getPath();
  27. if(file_exists($cachePath)){
  28. $cacheIterator = new RecursiveIteratorIterator(
  29. new RecursiveDirectoryIterator($cachePath),
  30. RecursiveIteratorIterator::CHILD_FIRST
  31. );
  32. foreach($cacheIterator as $cacheFile){
  33. if(in_array($cacheFile->getBasename(), array('.', '..')))
  34. continue;
  35. elseif($cacheFile->isFile()){
  36. if(filemtime($cacheFile->getPathname()) < time() - $duration)
  37. unlink($cacheFile->getPathname());
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * Set cache path
  44. * @return self
  45. */
  46. public function setPath($path){
  47. if(is_null($path) || !is_string($path)){
  48. throw new \Exception('The given path is invalid!');
  49. }
  50. $this->path = $path;
  51. // Make sure path ends with '/' or '\'
  52. $lastchar = substr($this->path, -1, 1);
  53. if($lastchar !== '/' && $lastchar !== '\\')
  54. $this->path .= '/';
  55. if(!is_dir($this->path))
  56. mkdir($this->path, 0755, true);
  57. return $this;
  58. }
  59. /**
  60. * Set HTTP GET parameters
  61. * @return self
  62. */
  63. public function setParameters(array $param){
  64. $this->param = array_map('strtolower', $param);
  65. return $this;
  66. }
  67. /**
  68. * Return cache path (and create if not exist)
  69. * @return string Cache path
  70. */
  71. protected function getPath(){
  72. if(is_null($this->path)){
  73. throw new \Exception('Call "setPath" first!');
  74. }
  75. return $this->path;
  76. }
  77. /**
  78. * Get the file name use for cache store
  79. * @return string Path to the file cache
  80. */
  81. protected function getCacheFile(){
  82. return $this->getPath() . $this->getCacheName();
  83. }
  84. /**
  85. * Determines file name for store the cache
  86. * return string
  87. */
  88. protected function getCacheName(){
  89. if(is_null($this->param)){
  90. throw new \Exception('Call "setParameters" first!');
  91. }
  92. return hash('md5', http_build_query($this->param)) . '.cache';
  93. }
  94. }