FileCache.php 2.6 KB

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