Google_Cache_File

This class implements a basic on disk storage.

Defined (1)

The class is defined in the following location(s).

/lib/Google/Cache/File.php  
  1. class Google_Cache_File extends Google_Cache_Abstract 
  2. const MAX_LOCK_RETRIES = 10; 
  3. private $path; 
  4. private $fh; 
  5.  
  6. public function __construct(Google_Client $client) 
  7. $this->path = $client->getClassConfig($this, 'directory'); 
  8.  
  9. public function get($key, $expiration = false) 
  10. $storageFile = $this->getCacheFile($key); 
  11. $data = false; 
  12.  
  13. if (!file_exists($storageFile)) { 
  14. return false; 
  15.  
  16. if ($expiration) { 
  17. $mtime = filemtime($storageFile); 
  18. if ((time() - $mtime) >= $expiration) { 
  19. $this->delete($key); 
  20. return false; 
  21.  
  22. if ($this->acquireReadLock($storageFile)) { 
  23. $data = fread($this->fh, filesize($storageFile)); 
  24. $data = unserialize($data); 
  25. $this->unlock($storageFile); 
  26.  
  27. return $data; 
  28.  
  29. public function set($key, $value) 
  30. $storageFile = $this->getWriteableCacheFile($key); 
  31. if ($this->acquireWriteLock($storageFile)) { 
  32. // We serialize the whole request object, since we don't only want the 
  33. // responseContent but also the postBody used, headers, size, etc. 
  34. $data = serialize($value); 
  35. $result = fwrite($this->fh, $data); 
  36. $this->unlock($storageFile); 
  37.  
  38. public function delete($key) 
  39. $file = $this->getCacheFile($key); 
  40. if (file_exists($file) && !unlink($file)) { 
  41. throw new Google_Cache_Exception("Cache file could not be deleted"); 
  42.  
  43. private function getWriteableCacheFile($file) 
  44. return $this->getCacheFile($file, true); 
  45.  
  46. private function getCacheFile($file, $forWrite = false) 
  47. return $this->getCacheDir($file, $forWrite) . '/' . md5($file); 
  48.  
  49. private function getCacheDir($file, $forWrite) 
  50. // use the first 2 characters of the hash as a directory prefix 
  51. // this should prevent slowdowns due to huge directory listings 
  52. // and thus give some basic amount of scalability 
  53. $storageDir = $this->path . '/' . substr(md5($file), 0, 2); 
  54. if ($forWrite && ! is_dir($storageDir)) { 
  55. if (! mkdir($storageDir, 0755, true)) { 
  56. throw new Google_Cache_Exception("Could not create storage directory: $storageDir"); 
  57. return $storageDir; 
  58.  
  59. private function acquireReadLock($storageFile) 
  60. return $this->acquireLock(LOCK_SH, $storageFile); 
  61.  
  62. private function acquireWriteLock($storageFile) 
  63. $rc = $this->acquireLock(LOCK_EX, $storageFile); 
  64. if (!$rc) { 
  65. $this->delete($storageFile); 
  66. return $rc; 
  67.  
  68. private function acquireLock($type, $storageFile) 
  69. $mode = $type == LOCK_EX ? "w" : "r"; 
  70. $this->fh = fopen($storageFile, $mode); 
  71. $count = 0; 
  72. while (!flock($this->fh, $type | LOCK_NB)) { 
  73. // Sleep for 10ms. 
  74. usleep(10000); 
  75. if (++$count < self::MAX_LOCK_RETRIES) { 
  76. return false; 
  77. return true; 
  78.  
  79. public function unlock($storageFile) 
  80. if ($this->fh) { 
  81. flock($this->fh, LOCK_UN);