PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/storage/buzzFileStorage.php

http://buzz-php-client.googlecode.com/
PHP | 135 lines | 84 code | 11 blank | 40 comment | 18 complexity | 387781b9200d502f2bcf11e72b0bcf49 MD5 | raw file
Possible License(s): Apache-2.0
  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
  25. */
  26. class buzzFileStorage extends buzzStorage {
  27. private $path;
  28. public function __construct($path)
  29. {
  30. $this->path = $path;
  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. if (! @mkdir($storageDir, 0755, true)) {
  40. // make sure the failure isn't because of a concurency issue
  41. if (! is_dir($storageDir)) {
  42. throw new buzzStorageException("Could not create storage directory: $storageDir");
  43. }
  44. }
  45. }
  46. @touch($storageFile . '.lock');
  47. }
  48. private function removeLock($storageFile) {
  49. // suppress all warnings, if some other process removed it that's ok too
  50. @unlink($storageFile . '.lock');
  51. }
  52. private function waitForLock($storageFile) {
  53. // 20 x 250 = 5 seconds
  54. $tries = 20;
  55. $cnt = 0;
  56. do {
  57. // make sure PHP picks up on file changes. This is an expensive action but really can't be avoided
  58. clearstatcache();
  59. // 250 ms is a long time to sleep, but it does stop the server from burning all resources on polling locks..
  60. usleep(250);
  61. $cnt ++;
  62. } while ($cnt <= $tries && $this->isLocked($storageFile));
  63. if ($this->isLocked($storageFile)) {
  64. // 5 seconds passed, assume the owning process died off and remove it
  65. $this->removeLock($storageFile);
  66. }
  67. }
  68. private function getStorageDir($hash) {
  69. // use the first 2 characters of the hash as a directory prefix
  70. // this should prevent slowdowns due to huge directory listings
  71. // and thus give some basic amount of scalability
  72. return $this->path . '/' . substr($hash, 0, 2);
  73. }
  74. private function getStorageFile($hash) {
  75. return $this->getStorageDir($hash) . '/' . $hash;
  76. }
  77. public function get($key, $expiration = false) {
  78. $storageFile = $this->getStorageFile(md5($key));
  79. // See if this storage file is locked, if so we wait upto 5 seconds for the lock owning process to
  80. // complete it's work. If the lock is not released within that time frame, it's cleaned up.
  81. // This should give us a fair amount of 'Storage Stampeding' protection
  82. if ($this->isLocked($storageFile)) {
  83. $this->waitForLock($storageFile);
  84. }
  85. if (file_exists($storageFile) && is_readable($storageFile)) {
  86. $now = time();
  87. if (!$expiration || (($mtime = @filemtime($storageFile)) !== false && ($now - $mtime) < $expiration)) {
  88. if (($data = @file_get_contents($storageFile)) !== false) {
  89. $data = unserialize($data);
  90. return $data;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. public function set($key, $value) {
  97. $storageDir = $this->getStorageDir(md5($key));
  98. $storageFile = $this->getStorageFile(md5($key));
  99. if ($this->isLocked($storageFile)) {
  100. // some other process is writing to this file too, wait until it's done to prevent hickups
  101. $this->waitForLock($storageFile);
  102. }
  103. if (! is_dir($storageDir)) {
  104. if (! @mkdir($storageDir, 0755, true)) {
  105. throw new buzzStorageException("Could not create storage directory: $storageDir");
  106. }
  107. }
  108. // we serialize the whole request object, since we don't only want the
  109. // responseContent but also the postBody used, headers, size, etc
  110. $data = serialize($value);
  111. $this->createLock($storageFile);
  112. if (! @file_put_contents($storageFile, $data)) {
  113. $this->removeLock($storageFile);
  114. throw new buzzStorageException("Could not store data in the file");
  115. }
  116. $this->removeLock($storageFile);
  117. }
  118. public function delete($key) {
  119. $file = $this->getStorageFile(md5($key));
  120. if (! @unlink($file)) {
  121. throw new buzzStorageException("Storage file could not be deleted");
  122. }
  123. }
  124. }