|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemInterface; |
| 15 | +use Psr\Cache\CacheItemPoolInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * LockRegistry is used internally by existing adapters to protect against cache stampede. |
| 19 | + * |
| 20 | + * It does so by wrapping the computation of items in a pool of locks. |
| 21 | + * Foreach each apps, there can be at most 20 concurrent processes that |
| 22 | + * compute items at the same time and only one per cache-key. |
| 23 | + * |
| 24 | + * @author Nicolas Grekas <p@tchwork.com> |
| 25 | + */ |
| 26 | +class LockRegistry |
| 27 | +{ |
| 28 | + private static $save; |
| 29 | + private static $openedFiles = array(); |
| 30 | + private static $lockedFiles = array(); |
| 31 | + |
| 32 | + /** |
| 33 | + * The number of items in this list controls the max number of concurrent processes. |
| 34 | +
8000
*/ |
| 35 | + private static $files = array( |
| 36 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AbstractAdapter.php', |
| 37 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AdapterInterface.php', |
| 38 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ApcuAdapter.php', |
| 39 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ArrayAdapter.php', |
| 40 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ChainAdapter.php', |
| 41 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineAdapter.php', |
| 42 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemAdapter.php', |
| 43 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'MemcachedAdapter.php', |
| 44 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'NullAdapter.php', |
| 45 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PdoAdapter.php', |
| 46 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpArrayAdapter.php', |
| 47 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpFilesAdapter.php', |
| 48 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ProxyAdapter.php', |
| 49 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'RedisAdapter.php', |
| 50 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'SimpleCacheAdapter.php', |
| 51 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapter.php', |
| 52 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapterInterface.php', |
| 53 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TraceableAdapter.php', |
| 54 | + __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TraceableTagAwareAdapter.php', |
| 55 | + ); |
| 56 | + |
| 57 | + /** |
| 58 | + * Defines a set of existing files that will be used as keys to acquire locks. |
| 59 | + * |
| 60 | + * @return array The previously defined set of files |
| 61 | + */ |
| 62 | + public static function setFiles(array $files): array |
| 63 | + { |
| 64 | + $previousFiles = self::$files; |
| 65 | + self::$files = $files; |
| 66 | + |
| 67 | + foreach (self::$openedFiles as $k => $file) { |
| 68 | + flock($file, LOCK_UN); |
| 69 | + fclose($file); |
| 70 | + } |
| 71 | + self::$openedFiles = self::$lockedFiles = array(); |
| 72 | + |
| 73 | + return $previousFiles; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @internal |
| 78 | + */ |
| 79 | + public static function save(string $key, CacheItemPoolInterface $pool, CacheItemInterface $item, callable $callback, float $startTime, &$value): bool |
| 80 | + { |
| 81 | + self::$save = self::$save ?? \Closure::bind( |
| 82 | + function (CacheItemPoolInterface $pool, CacheItemInterface $item, $value, float $startTime) { |
| 83 | + if ($item instanceof CacheItem && $startTime && $item->expiry > $endTime = microtime(true)) { |
| 84 | + $item->newMetadata[CacheItem::METADATA_EXPIRY] = $item->expiry; |
| 85 | + $item->newMetadata[CacheItem::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime); |
| 86 | + } |
| 87 | + $pool->save($item->set($value)); |
| 88 | + |
| 89 | + return $value; |
| 90 | + }, |
| 91 | + null, |
| 92 | + CacheItem::class |
| 93 | + ); |
| 94 | + |
| 95 | + $key = self::$files ? crc32($key) % \count(self::$files) : -1; |
| 96 | + |
| 97 | + if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) { |
| 98 | + $value = (self::$save)($pool, $item, $callback($item), $startTime); |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + // race to get the lock in non-blocking mode |
| 105 | + if (flock($lock, LOCK_EX | LOCK_NB)) { |
| 106 | + self::$lockedFiles[$key] = true; |
| 107 | + $value = (self::$save)($pool, $item, $callback($item), $startTime); |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | + // if we failed the race, retry locking in blocking mode to wait for the winner |
| 112 | + flock($lock, LOCK_SH); |
| 113 | + } finally { |
| 114 | + flock($lock, LOCK_UN); |
| 115 | + self::$lockedFiles[$key] = false; |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + private static function open(int $key) |
| 122 | + { |
| 123 | + if ($h = self::$openedFiles[$key] ?? null) { |
| 124 | + return $h; |
| 125 | + } |
| 126 | + if ($h = fopen(self::$files[$key], 'rb')) { |
| 127 | + return self::$openedFiles[$key] = $h; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments