FileCache.php 2.4 KB

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