diff options
author | Julius Härtl <jus@bitgrid.net> | 2022-08-30 19:53:12 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2022-08-31 16:24:35 +0200 |
commit | 80f6a5834ab9e679c1a0df6a1aee82107ca5cfe3 (patch) | |
tree | b493860f52e4fe7b358ecb91897eb19b7da1caac /lib/private | |
parent | a392235e23c3b8af1d0eba4dbac18b18de826a1c (diff) | |
download | nextcloud-server-80f6a5834ab9e679c1a0df6a1aee82107ca5cfe3.tar.gz nextcloud-server-80f6a5834ab9e679c1a0df6a1aee82107ca5cfe3.zip |
Refactor cache handling
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Collaboration/Reference/File/FileReferenceEventListener.php | 61 | ||||
-rw-r--r-- | lib/private/Collaboration/Reference/File/FileReferenceProvider.php (renamed from lib/private/Collaboration/Reference/FileReferenceProvider.php) | 51 | ||||
-rw-r--r-- | lib/private/Collaboration/Reference/LinkReferenceProvider.php | 13 | ||||
-rw-r--r-- | lib/private/Collaboration/Reference/ReferenceManager.php | 37 |
4 files changed, 131 insertions, 31 deletions
diff --git a/lib/private/Collaboration/Reference/File/FileReferenceEventListener.php b/lib/private/Collaboration/Reference/File/FileReferenceEventListener.php new file mode 100644 index 00000000000..6ccae9903dc --- /dev/null +++ b/lib/private/Collaboration/Reference/File/FileReferenceEventListener.php @@ -0,0 +1,61 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OC\Collaboration\Reference\File; + +use OCP\Collaboration\Reference\IReferenceManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Events\Node\NodeDeletedEvent; +use OCP\Share\Events\ShareCreatedEvent; +use OCP\Share\Events\ShareDeletedEvent; + +class FileReferenceEventListener implements \OCP\EventDispatcher\IEventListener { + private IReferenceManager $manager; + + public function __construct(IReferenceManager $manager) { + $this->manager = $manager; + } + + public static function register(IEventDispatcher $eventDispatcher): void { + $eventDispatcher->addServiceListener(NodeDeletedEvent::class, FileReferenceEventListener::class); + $eventDispatcher->addServiceListener(ShareDeletedEvent::class, FileReferenceEventListener::class); + $eventDispatcher->addServiceListener(ShareCreatedEvent::class, FileReferenceEventListener::class); + } + + /** + * @inheritDoc + */ + public function handle(Event $event): void { + if ($event instanceof NodeDeletedEvent) { + $this->manager->invalidateCache((string)$event->getNode()->getId()); + } + if ($event instanceof ShareDeletedEvent) { + $this->manager->invalidateCache((string)$event->getShare()->getNodeId()); + } + if ($event instanceof ShareCreatedEvent) { + $this->manager->invalidateCache((string)$event->getShare()->getNodeId()); + } + } +} diff --git a/lib/private/Collaboration/Reference/FileReferenceProvider.php b/lib/private/Collaboration/Reference/File/FileReferenceProvider.php index 358092ff508..ecb7229e21e 100644 --- a/lib/private/Collaboration/Reference/FileReferenceProvider.php +++ b/lib/private/Collaboration/Reference/File/FileReferenceProvider.php @@ -22,8 +22,9 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -namespace OC\Collaboration\Reference; +namespace OC\Collaboration\Reference\File; +use OC\Collaboration\Reference\Reference; use OC\User\NoUserException; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceProvider; @@ -50,8 +51,38 @@ class FileReferenceProvider implements IReferenceProvider { } public function matchReference(string $referenceText): bool { - return str_starts_with($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) - || str_starts_with($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')); + return $this->getFilesAppLinkId($referenceText) !== null; + } + + private function getFilesAppLinkId(string $referenceText): ?int { + $start = $this->urlGenerator->getAbsoluteURL('/apps/files'); + $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files'); + + $fileId = null; + + if (mb_strpos($referenceText, $start) === 0) { + $parts = parse_url($referenceText); + parse_str($parts['query'], $query); + $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; + $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; + } + + if (mb_strpos($referenceText, $startIndex) === 0) { + $parts = parse_url($referenceText); + parse_str($parts['query'], $query); + $fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId; + $fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId; + } + + if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) { + $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText); + } + + if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) { + $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText); + } + + return $fileId !== null ? (int)$fileId : null; } public function resolveReference(string $referenceText): ?IReference { @@ -77,12 +108,14 @@ class FileReferenceProvider implements IReferenceProvider { throw new NotFoundException(); } - $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $reference->getId()); - $fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $fileId); + $fileId = $this->getFilesAppLinkId($reference->getId()); + if ($fileId === null) { + throw new NotFoundException(); + } try { $userFolder = $this->rootFolder->getUserFolder($this->userId); - $files = $userFolder->getById((int)$fileId); + $files = $userFolder->getById($fileId); if (empty($files)) { throw new NotFoundException(); @@ -110,11 +143,11 @@ class FileReferenceProvider implements IReferenceProvider { } } - public function isGloballyCacheable(): bool { - return false; + public function getCachePrefix(string $referenceId): string { + return (string)$this->getFilesAppLinkId($referenceId); } - public function getCacheKey(string $referenceId): string { + public function getCacheKey(string $referenceId): ?string { return $this->userId ?? ''; } } diff --git a/lib/private/Collaboration/Reference/LinkReferenceProvider.php b/lib/private/Collaboration/Reference/LinkReferenceProvider.php index 7ac6aca5f89..0c2052b6d7d 100644 --- a/lib/private/Collaboration/Reference/LinkReferenceProvider.php +++ b/lib/private/Collaboration/Reference/LinkReferenceProvider.php @@ -106,12 +106,13 @@ class LinkReferenceProvider implements IReferenceProvider { $responseBody = (string)$response->getBody(); - $reference->setUrl($reference->getId()); - // OpenGraph handling $consumer = new Consumer(); + $consumer->useFallbackMode = true; $object = $consumer->loadHtml($responseBody); + $reference->setUrl($reference->getId()); + if ($object->title) { $reference->setTitle($object->title); } @@ -145,11 +146,11 @@ class LinkReferenceProvider implements IReferenceProvider { } } - public function isGloballyCacheable(): bool { - return true; + public function getCachePrefix(string $referenceId): string { + return $referenceId; } - public function getCacheKey(string $referenceId): string { - return ''; + public function getCacheKey(string $referenceId): ?string { + return null; } } diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php index 471f0efb3be..562c9944651 100644 --- a/lib/private/Collaboration/Reference/ReferenceManager.php +++ b/lib/private/Collaboration/Reference/ReferenceManager.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace OC\Collaboration\Reference; use OC\AppFramework\Bootstrap\Coordinator; +use OC\Collaboration\Reference\File\FileReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -62,6 +63,17 @@ class ReferenceManager implements IReferenceManager { }, $references); } + public function getReferenceFromCache(string $referenceId): ?IReference { + $matchedProvider = $this->getMatchedProvider($referenceId); + + if ($matchedProvider === null) { + return null; + } + + $cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId); + return $this->getReferenceByCacheKey($cacheKey); + } + public function getReferenceByCacheKey(string $cacheKey): ?IReference { $cached = $this->cache->get($cacheKey); if ($cached) { @@ -78,7 +90,7 @@ class ReferenceManager implements IReferenceManager { return null; } - $cacheKey = $this->getCacheKey($matchedProvider, $referenceId); + $cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId); $cached = $this->cache->get($cacheKey); if ($cached) { return Reference::fromCache($cached); @@ -109,27 +121,20 @@ class ReferenceManager implements IReferenceManager { return $matchedProvider; } - private function getCacheKey(IReferenceProvider $provider, string $referenceId): string { - return md5($referenceId) . ( - $provider->isGloballyCacheable() - ? '' - : '-' . md5($provider->getCacheKey($referenceId)) + private function getFullCacheKey(IReferenceProvider $provider, string $referenceId): string { + $cacheKey = $provider->getCacheKey($referenceId); + return md5($provider->getCachePrefix($referenceId)) . ( + $cacheKey !== null ? ('-' . md5($cacheKey)) : '' ); } - 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)); + public function invalidateCache(string $cachePrefix, ?string $cacheKey = null): void { + if ($cacheKey === null) { + $this->cache->clear(md5($cachePrefix)); return; } - $this->cache->remove($this->getCacheKey($matchedProvider, $referenceId)); + $this->cache->remove(md5($cachePrefix) . '-' . md5($cacheKey)); } /** |