X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FApp%2FRouter.php;h=181f5368d51de5668a9f5d77bb419166330c7ade;hb=85d162d1d2a738c6d21e834fe1b5cb579c4f581e;hp=c18c048eaa8db0ac3a32a94f7d2a56c8534d2587;hpb=054c301ef0345c4ff9f35cfd08717757eab17b9d;p=friendica.git diff --git a/src/App/Router.php b/src/App/Router.php index c18c048eaa..181f5368d5 100644 --- a/src/App/Router.php +++ b/src/App/Router.php @@ -26,10 +26,11 @@ use FastRoute\DataGenerator\GroupCountBased; use FastRoute\Dispatcher; use FastRoute\RouteCollector; use FastRoute\RouteParser\Std; -use Friendica\Core\Cache\Duration; -use Friendica\Core\Cache\ICache; +use Friendica\Core\Cache\Enum\Duration; +use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\Hook; use Friendica\Core\L10n; +use Friendica\Core\Lock\Capability\ICanLock; use Friendica\Network\HTTPException; /** @@ -44,11 +45,12 @@ use Friendica\Network\HTTPException; */ class Router { - const DELETE = 'DELETE'; - const GET = 'GET'; - const PATCH = 'PATCH'; - const POST = 'POST'; - const PUT = 'PUT'; + const DELETE = 'DELETE'; + const GET = 'GET'; + const PATCH = 'PATCH'; + const POST = 'POST'; + const PUT = 'PUT'; + const OPTIONS = 'OPTIONS'; const ALLOWED_METHODS = [ self::DELETE, @@ -56,6 +58,7 @@ class Router self::PATCH, self::POST, self::PUT, + self::OPTIONS ]; /** @var RouteCollector */ @@ -74,9 +77,12 @@ class Router /** @var L10n */ private $l10n; - /** @var ICache */ + /** @var ICanCache */ private $cache; + /** @var ICanLock */ + private $lock; + /** @var string */ private $baseRoutesFilepath; @@ -84,14 +90,15 @@ class Router * @param array $server The $_SERVER variable * @param string $baseRoutesFilepath The path to a base routes file to leverage cache, can be empty * @param L10n $l10n - * @param ICache $cache + * @param ICanCache $cache * @param RouteCollector|null $routeCollector */ - public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICache $cache, RouteCollector $routeCollector = null) + public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, RouteCollector $routeCollector = null) { $this->baseRoutesFilepath = $baseRoutesFilepath; $this->l10n = $l10n; $this->cache = $cache; + $this->lock = $lock; $httpMethod = $server['REQUEST_METHOD'] ?? self::GET; $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; @@ -299,11 +306,20 @@ class Router return $routerDispatchData; } + if (!$this->lock->acquire('getCachedDispatchData', 0)) { + // Immediately return uncached data when we can't aquire a lock + return $this->getDispatchData(); + } + $routerDispatchData = $this->getDispatchData(); $this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY); if (!empty($routesFileModifiedTime)) { - $this->cache->set('lastRoutesFileMtime', $routesFileModifiedTime, Duration::MONTH); + $this->cache->set('lastRoutesFileModifiedTime', $routesFileModifiedTime, Duration::MONTH); + } + + if ($this->lock->isLocked('getCachedDispatchData')) { + $this->lock->release('getCachedDispatchData'); } return $routerDispatchData;