PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/accesspress-social-login-lite/google/Cache/File.php

https://gitlab.com/rio.munas/gddc
PHP | 145 lines | 99 code | 17 blank | 29 comment | 15 complexity | 6babb7be303e8b300f08fd9768c35cd1 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. require_once APSL_PLUGIN_DIR."google/Cache/Abstract.php";
  18. require_once APSL_PLUGIN_DIR."google/Cache/Exception.php";
  19. /*
  20. * This class implements a basic on disk storage. While that does
  21. * work quite well it's not the most elegant and scalable solution.
  22. * It will also get you into a heap of trouble when you try to run
  23. * this in a clustered environment.
  24. *
  25. * @author Chris Chabot <chabotc@google.com>
  26. */
  27. class Google_Cache_File extends Google_Cache_Abstract
  28. {
  29. const MAX_LOCK_RETRIES = 10;
  30. private $path;
  31. private $fh;
  32. public function __construct(Google_Client $client)
  33. {
  34. $this->path = $client->getClassConfig($this, 'directory');
  35. }
  36. public function get($key, $expiration = false)
  37. {
  38. $storageFile = $this->getCacheFile($key);
  39. $data = false;
  40. if (!file_exists($storageFile)) {
  41. return false;
  42. }
  43. if ($expiration) {
  44. $mtime = filemtime($storageFile);
  45. if ((time() - $mtime) >= $expiration) {
  46. $this->delete($key);
  47. return false;
  48. }
  49. }
  50. if ($this->acquireReadLock($storageFile)) {
  51. $data = fread($this->fh, filesize($storageFile));
  52. $data = unserialize($data);
  53. $this->unlock($storageFile);
  54. }
  55. return $data;
  56. }
  57. public function set($key, $value)
  58. {
  59. $storageFile = $this->getWriteableCacheFile($key);
  60. if ($this->acquireWriteLock($storageFile)) {
  61. // We serialize the whole request object, since we don't only want the
  62. // responseContent but also the postBody used, headers, size, etc.
  63. $data = serialize($value);
  64. $result = fwrite($this->fh, $data);
  65. $this->unlock($storageFile);
  66. }
  67. }
  68. public function delete($key)
  69. {
  70. $file = $this->getCacheFile($key);
  71. if (file_exists($file) && !unlink($file)) {
  72. throw new Google_Cache_Exception("Cache file could not be deleted");
  73. }
  74. }
  75. private function getWriteableCacheFile($file)
  76. {
  77. return $this->getCacheFile($file, true);
  78. }
  79. private function getCacheFile($file, $forWrite = false)
  80. {
  81. return $this->getCacheDir($file, $forWrite) . '/' . md5($file);
  82. }
  83. private function getCacheDir($file, $forWrite)
  84. {
  85. // use the first 2 characters of the hash as a directory prefix
  86. // this should prevent slowdowns due to huge directory listings
  87. // and thus give some basic amount of scalability
  88. $storageDir = $this->path . '/' . substr(md5($file), 0, 2);
  89. if ($forWrite && ! is_dir($storageDir)) {
  90. if (! mkdir($storageDir, 0755, true)) {
  91. throw new Google_Cache_Exception("Could not create storage directory: $storageDir");
  92. }
  93. }
  94. return $storageDir;
  95. }
  96. private function acquireReadLock($storageFile)
  97. {
  98. return $this->acquireLock(LOCK_SH, $storageFile);
  99. }
  100. private function acquireWriteLock($storageFile)
  101. {
  102. $rc = $this->acquireLock(LOCK_EX, $storageFile);
  103. if (!$rc) {
  104. $this->delete($storageFile);
  105. }
  106. return $rc;
  107. }
  108. private function acquireLock($type, $storageFile)
  109. {
  110. $mode = $type == LOCK_EX ? "w" : "r";
  111. $this->fh = fopen($storageFile, $mode);
  112. $count = 0;
  113. while (!flock($this->fh, $type | LOCK_NB)) {
  114. // Sleep for 10ms.
  115. usleep(10000);
  116. if (++$count < self::MAX_LOCK_RETRIES) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. public function unlock($storageFile)
  123. {
  124. if ($this->fh) {
  125. flock($this->fh, LOCK_UN);
  126. }
  127. }
  128. }