FileCache.php 2.7 KB

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