diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-08-12 18:57:56 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2022-08-31 16:20:06 +0200 |
commit | f58218deeab3a56d5a6a1dfb2047901155daa74d (patch) | |
tree | fb9afe032304d67e2039fb5127e250049bd1a636 | |
parent | 31be855522fe68956cb38aaca2a9544c5a75f230 (diff) | |
download | nextcloud-server-f58218deeab3a56d5a6a1dfb2047901155daa74d.tar.gz nextcloud-server-f58218deeab3a56d5a6a1dfb2047901155daa74d.zip |
Add cache invalidation
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | lib/private/Collaboration/Reference/ReferenceManager.php | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php index 5cab10c0ef5..29084902400 100644 --- a/lib/private/Collaboration/Reference/ReferenceManager.php +++ b/lib/private/Collaboration/Reference/ReferenceManager.php @@ -32,6 +32,7 @@ use OCP\ICacheFactory; class ReferenceManager implements IReferenceManager { public const URL_PATTERN = '/(\s|\n|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)/mi'; + public const CACHE_TTL = 60; /** @var IReferenceProvider[] */ private array $providers = []; @@ -54,19 +55,13 @@ class ReferenceManager implements IReferenceManager { } public function resolveReference(string $referenceId): ?IReference { - $matchedProvider = null; - foreach ($this->providers as $provider) { - $matchedProvider = $provider->matchReference($referenceId) ? $provider : null; - } + $matchedProvider = $this->getMatchedProvider($referenceId); if ($matchedProvider === null) { - $matchedProvider = $this->linkReferenceProvider; + return null; } - $cacheKey = md5(serialize([ - $matchedProvider->isGloballyCachable() ? 0 : $matchedProvider->getCacheKey($referenceId), - $referenceId - ])); + $cacheKey = $this->getCacheKey($matchedProvider, $referenceId); $cached = $this->cache->get($cacheKey); if ($cached) { return Reference::fromCache($cached); @@ -74,13 +69,49 @@ class ReferenceManager implements IReferenceManager { $reference = $matchedProvider->resolveReference($referenceId); if ($reference) { - $this->cache->set($cacheKey, Reference::toCache($reference), 60); + $this->cache->set($cacheKey, Reference::toCache($reference), self::CACHE_TTL); return $reference; } return null; } + private function getMatchedProvider(string $referenceId): ?IReferenceProvider { + $matchedProvider = null; + foreach ($this->providers as $provider) { + $matchedProvider = $provider->matchReference($referenceId) ? $provider : null; + } + + if ($matchedProvider === null && $this->linkReferenceProvider->matchReference($referenceId)) { + $matchedProvider = $this->linkReferenceProvider; + } + + return $matchedProvider; + } + + private function getCacheKey(IReferenceProvider $provider, string $referenceId): string { + return md5($referenceId) . ( + $provider->isGloballyCachable() + ? '' + : '-' . md5($provider->getCacheKey($referenceId)) + ); + } + + public function invalidateCache(string $referenceId, ?string $providerCacheKey = null): void { + $matchedProvider = $this->getMatchedProvider($referenceId); + + if ($matchedProvider === null) { + return; + } + + if ($providerCacheKey === null) { + $this->cache->clear(md5($referenceId)); + return; + } + + $this->cache->remove($this->getCacheKey($matchedProvider, $referenceId)); + } + public function registerReferenceProvider(IReferenceProvider $provider): void { $this->providers[] = $provider; } |