DefaultMultiGetRegion.php
9 months ago
DefaultRegion.php
9 months ago
FileLockRegion.php
9 months ago
UpdateTimestampCache.php
9 months ago
index.php
9 months ago
FileLockRegion.php
152 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Cache\Region; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\Cache\CacheEntry; |
| 6 | use MailPoetVendor\Doctrine\ORM\Cache\CacheKey; |
| 7 | use MailPoetVendor\Doctrine\ORM\Cache\CollectionCacheEntry; |
| 8 | use MailPoetVendor\Doctrine\ORM\Cache\ConcurrentRegion; |
| 9 | use MailPoetVendor\Doctrine\ORM\Cache\Lock; |
| 10 | use MailPoetVendor\Doctrine\ORM\Cache\Region; |
| 11 | use InvalidArgumentException; |
| 12 | use function array_filter; |
| 13 | use function array_map; |
| 14 | use function chmod; |
| 15 | use function file_get_contents; |
| 16 | use function file_put_contents; |
| 17 | use function fileatime; |
| 18 | use function glob; |
| 19 | use function is_dir; |
| 20 | use function is_file; |
| 21 | use function is_writable; |
| 22 | use function mkdir; |
| 23 | use function sprintf; |
| 24 | use function time; |
| 25 | use function unlink; |
| 26 | use const DIRECTORY_SEPARATOR; |
| 27 | use const LOCK_EX; |
| 28 | class FileLockRegion implements ConcurrentRegion |
| 29 | { |
| 30 | public const LOCK_EXTENSION = 'lock'; |
| 31 | private $region; |
| 32 | private $directory; |
| 33 | private $lockLifetime; |
| 34 | public function __construct(Region $region, $directory, $lockLifetime) |
| 35 | { |
| 36 | if (!is_dir($directory) && !@mkdir($directory, 0775, \true)) { |
| 37 | throw new InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $directory)); |
| 38 | } |
| 39 | if (!is_writable($directory)) { |
| 40 | throw new InvalidArgumentException(sprintf('The directory "%s" is not writable.', $directory)); |
| 41 | } |
| 42 | $this->region = $region; |
| 43 | $this->directory = $directory; |
| 44 | $this->lockLifetime = $lockLifetime; |
| 45 | } |
| 46 | private function isLocked(CacheKey $key, ?Lock $lock = null) : bool |
| 47 | { |
| 48 | $filename = $this->getLockFileName($key); |
| 49 | if (!is_file($filename)) { |
| 50 | return \false; |
| 51 | } |
| 52 | $time = $this->getLockTime($filename); |
| 53 | $content = $this->getLockContent($filename); |
| 54 | if (!$content || !$time) { |
| 55 | @unlink($filename); |
| 56 | return \false; |
| 57 | } |
| 58 | if ($lock && $content === $lock->value) { |
| 59 | return \false; |
| 60 | } |
| 61 | // outdated lock |
| 62 | if ($time + $this->lockLifetime <= time()) { |
| 63 | @unlink($filename); |
| 64 | return \false; |
| 65 | } |
| 66 | return \true; |
| 67 | } |
| 68 | private function getLockFileName(CacheKey $key) : string |
| 69 | { |
| 70 | return $this->directory . DIRECTORY_SEPARATOR . $key->hash . '.' . self::LOCK_EXTENSION; |
| 71 | } |
| 72 | private function getLockContent(string $filename) |
| 73 | { |
| 74 | return @file_get_contents($filename); |
| 75 | } |
| 76 | private function getLockTime(string $filename) |
| 77 | { |
| 78 | return @fileatime($filename); |
| 79 | } |
| 80 | public function getName() |
| 81 | { |
| 82 | return $this->region->getName(); |
| 83 | } |
| 84 | public function contains(CacheKey $key) |
| 85 | { |
| 86 | if ($this->isLocked($key)) { |
| 87 | return \false; |
| 88 | } |
| 89 | return $this->region->contains($key); |
| 90 | } |
| 91 | public function get(CacheKey $key) |
| 92 | { |
| 93 | if ($this->isLocked($key)) { |
| 94 | return null; |
| 95 | } |
| 96 | return $this->region->get($key); |
| 97 | } |
| 98 | public function getMultiple(CollectionCacheEntry $collection) |
| 99 | { |
| 100 | if (array_filter(array_map([$this, 'isLocked'], $collection->identifiers))) { |
| 101 | return null; |
| 102 | } |
| 103 | return $this->region->getMultiple($collection); |
| 104 | } |
| 105 | public function put(CacheKey $key, CacheEntry $entry, ?Lock $lock = null) |
| 106 | { |
| 107 | if ($this->isLocked($key, $lock)) { |
| 108 | return \false; |
| 109 | } |
| 110 | return $this->region->put($key, $entry); |
| 111 | } |
| 112 | public function evict(CacheKey $key) |
| 113 | { |
| 114 | if ($this->isLocked($key)) { |
| 115 | @unlink($this->getLockFileName($key)); |
| 116 | } |
| 117 | return $this->region->evict($key); |
| 118 | } |
| 119 | public function evictAll() |
| 120 | { |
| 121 | // The check below is necessary because on some platforms glob returns false |
| 122 | // when nothing matched (even though no errors occurred) |
| 123 | $filenames = glob(sprintf('%s/*.%s', $this->directory, self::LOCK_EXTENSION)); |
| 124 | if ($filenames) { |
| 125 | foreach ($filenames as $filename) { |
| 126 | @unlink($filename); |
| 127 | } |
| 128 | } |
| 129 | return $this->region->evictAll(); |
| 130 | } |
| 131 | public function lock(CacheKey $key) |
| 132 | { |
| 133 | if ($this->isLocked($key)) { |
| 134 | return null; |
| 135 | } |
| 136 | $lock = Lock::createLockRead(); |
| 137 | $filename = $this->getLockFileName($key); |
| 138 | if (!@file_put_contents($filename, $lock->value, LOCK_EX)) { |
| 139 | return null; |
| 140 | } |
| 141 | chmod($filename, 0664); |
| 142 | return $lock; |
| 143 | } |
| 144 | public function unlock(CacheKey $key, Lock $lock) |
| 145 | { |
| 146 | if ($this->isLocked($key, $lock)) { |
| 147 | return \false; |
| 148 | } |
| 149 | return @unlink($this->getLockFileName($key)); |
| 150 | } |
| 151 | } |
| 152 |