PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/google/cache/Google_FileCache.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 137 lines | 84 code | 11 blank | 42 comment | 18 complexity | 7cb3f047c1e3774474de8e8b2f5bd638 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * This class implements a basic on disk storage. While that does
  19. * work quite well it's not the most elegant and scalable solution.
  20. * It will also get you into a heap of trouble when you try to run
  21. * this in a clustered environment. In those cases please use the
  22. * MySql back-end
  23. *
  24. * @author Chris Chabot <chabotc@google.com>
  25. */
  26. class Google_FileCache extends Google_Cache {
  27. private $path;
  28. public function __construct() {
  29. global $apiConfig;
  30. $this->path = $apiConfig['ioFileCache_directory'];
  31. }
  32. private function isLocked($storageFile) {
  33. // our lock file convention is simple: /the/file/path.lock
  34. return file_exists($storageFile . '.lock');
  35. }
  36. private function createLock($storageFile) {
  37. $storageDir = dirname($storageFile);
  38. if (! is_dir($storageDir)) {
  39. // @codeCoverageIgnoreStart
  40. if (! @mkdir($storageDir, 0755, true)) {
  41. // make sure the failure isn't because of a concurrency issue
  42. if (! is_dir($storageDir)) {
  43. throw new Google_CacheException("Could not create storage directory: $storageDir");
  44. }
  45. }
  46. // @codeCoverageIgnoreEnd
  47. }
  48. @touch($storageFile . '.lock');
  49. }
  50. private function removeLock($storageFile) {
  51. // suppress all warnings, if some other process removed it that's ok too
  52. @unlink($storageFile . '.lock');
  53. }
  54. private function waitForLock($storageFile) {
  55. // 20 x 250 = 5 seconds
  56. $tries = 20;
  57. $cnt = 0;
  58. do {
  59. // make sure PHP picks up on file changes. This is an expensive action but really can't be avoided
  60. clearstatcache();
  61. // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks..
  62. usleep(250);
  63. $cnt ++;
  64. } while ($cnt <= $tries && $this->isLocked($storageFile));
  65. if ($this->isLocked($storageFile)) {
  66. // 5 seconds passed, assume the owning process died off and remove it
  67. $this->removeLock($storageFile);
  68. }
  69. }
  70. private function getCacheDir($hash) {
  71. // use the first 2 characters of the hash as a directory prefix
  72. // this should prevent slowdowns due to huge directory listings
  73. // and thus give some basic amount of scalability
  74. return $this->path . '/' . substr($hash, 0, 2);
  75. }
  76. private function getCacheFile($hash) {
  77. return $this->getCacheDir($hash) . '/' . $hash;
  78. }
  79. public function get($key, $expiration = false) {
  80. $storageFile = $this->getCacheFile(md5($key));
  81. // See if this storage file is locked, if so we wait upto 5 seconds for the lock owning process to
  82. // complete it's work. If the lock is not released within that time frame, it's cleaned up.
  83. // This should give us a fair amount of 'Cache Stampeding' protection
  84. if ($this->isLocked($storageFile)) {
  85. $this->waitForLock($storageFile);
  86. }
  87. if (file_exists($storageFile) && is_readable($storageFile)) {
  88. $now = time();
  89. if (! $expiration || (($mtime = @filemtime($storageFile)) !== false && ($now - $mtime) < $expiration)) {
  90. if (($data = @file_get_contents($storageFile)) !== false) {
  91. $data = unserialize($data);
  92. return $data;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. public function set($key, $value) {
  99. $storageDir = $this->getCacheDir(md5($key));
  100. $storageFile = $this->getCacheFile(md5($key));
  101. if ($this->isLocked($storageFile)) {
  102. // some other process is writing to this file too, wait until it's done to prevent hickups
  103. $this->waitForLock($storageFile);
  104. }
  105. if (! is_dir($storageDir)) {
  106. if (! @mkdir($storageDir, 0755, true)) {
  107. throw new Google_CacheException("Could not create storage directory: $storageDir");
  108. }
  109. }
  110. // we serialize the whole request object, since we don't only want the
  111. // responseContent but also the postBody used, headers, size, etc
  112. $data = serialize($value);
  113. $this->createLock($storageFile);
  114. if (! @file_put_contents($storageFile, $data)) {
  115. $this->removeLock($storageFile);
  116. throw new Google_CacheException("Could not store data in the file");
  117. }
  118. $this->removeLock($storageFile);
  119. }
  120. public function delete($key) {
  121. $file = $this->getCacheFile(md5($key));
  122. if (! @unlink($file)) {
  123. throw new Google_CacheException("Cache file could not be deleted");
  124. }
  125. }
  126. }