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