Google_FileCache

This class implements a basic on disk storage.

Defined (1)

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

/gdwpm-api/cache/Google_FileCache.php  
  1. class Google_FileCache extends Google_Cache { 
  2. private $path; 
  3.  
  4. public function __construct() { 
  5. global $gdwpm_apiConfig; 
  6. $this->path = $gdwpm_apiConfig['ioFileCache_directory']; 
  7.  
  8. private function isLocked($storageFile) { 
  9. // our lock file convention is simple: /the/file/path.lock 
  10. return file_exists($storageFile . '.lock'); 
  11.  
  12. private function createLock($storageFile) { 
  13. $storageDir = dirname($storageFile); 
  14. if (! is_dir($storageDir)) { 
  15. // @codeCoverageIgnoreStart 
  16. if (! @mkdir($storageDir, 0755, true)) { 
  17. // make sure the failure isn't because of a concurrency issue 
  18. if (! is_dir($storageDir)) { 
  19. throw new Google_CacheException("Could not create storage directory: $storageDir"); 
  20. // @codeCoverageIgnoreEnd 
  21. @touch($storageFile . '.lock'); 
  22.  
  23. private function removeLock($storageFile) { 
  24. // suppress all warnings, if some other process removed it that's ok too 
  25. @unlink($storageFile . '.lock'); 
  26.  
  27. private function waitForLock($storageFile) { 
  28. // 20 x 250 = 5 seconds 
  29. $tries = 20; 
  30. $cnt = 0; 
  31. do { 
  32. // make sure PHP picks up on file changes. This is an expensive action but really can't be avoided 
  33. clearstatcache(); 
  34. // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks.. 
  35. usleep(250); 
  36. $cnt ++; 
  37. } while ($cnt <= $tries && $this->isLocked($storageFile)); 
  38. if ($this->isLocked($storageFile)) { 
  39. // 5 seconds passed, assume the owning process died off and remove it 
  40. $this->removeLock($storageFile); 
  41.  
  42. private function getCacheDir($hash) { 
  43. // use the first 2 characters of the hash as a directory prefix 
  44. // this should prevent slowdowns due to huge directory listings 
  45. // and thus give some basic amount of scalability 
  46. return $this->path . '/' . substr($hash, 0, 2); 
  47.  
  48. private function getCacheFile($hash) { 
  49. return $this->getCacheDir($hash) . '/' . $hash; 
  50.  
  51. public function get($key, $expiration = false) { 
  52. $storageFile = $this->getCacheFile(md5($key)); 
  53. // See if this storage file is locked, if so we wait up to 5 seconds for the lock owning process to 
  54. // complete it's work. If the lock is not released within that time frame, it's cleaned up. 
  55. // This should give us a fair amount of 'Cache Stampeding' protection 
  56. if ($this->isLocked($storageFile)) { 
  57. $this->waitForLock($storageFile); 
  58. if (file_exists($storageFile) && is_readable($storageFile)) { 
  59. $now = time(); 
  60. if (! $expiration || (($mtime = @filemtime($storageFile)) !== false && ($now - $mtime) < $expiration)) { 
  61. if (($data = @file_get_contents($storageFile)) !== false) { 
  62. $data = unserialize($data); 
  63. return $data; 
  64. return false; 
  65.  
  66. public function set($key, $value) { 
  67. $storageDir = $this->getCacheDir(md5($key)); 
  68. $storageFile = $this->getCacheFile(md5($key)); 
  69. if ($this->isLocked($storageFile)) { 
  70. // some other process is writing to this file too, wait until it's done to prevent hiccups 
  71. $this->waitForLock($storageFile); 
  72. if (! is_dir($storageDir)) { 
  73. if (! @mkdir($storageDir, 0755, true)) { 
  74. throw new Google_CacheException("Could not create storage directory: $storageDir"); 
  75. // we serialize the whole request object, since we don't only want the 
  76. // responseContent but also the postBody used, headers, size, etc 
  77. $data = serialize($value); 
  78. $this->createLock($storageFile); 
  79. if (! @file_put_contents($storageFile, $data)) { 
  80. $this->removeLock($storageFile); 
  81. throw new Google_CacheException("Could not store data in the file"); 
  82. $this->removeLock($storageFile); 
  83.  
  84. public function delete($key) { 
  85. $file = $this->getCacheFile(md5($key)); 
  86. if (! @unlink($file)) { 
  87. throw new Google_CacheException("Cache file could not be deleted");