From 510b75e28bcbb761bee9b9b4c6ee488bfbc87ba8 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 8 Jan 2025 14:04:58 +0100 Subject: [PATCH] Revert "remove caching for shares" --- lib/Service/MembershipService.php | 6 ++++ lib/Service/ShareWrapperService.php | 54 ++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/lib/Service/MembershipService.php b/lib/Service/MembershipService.php index 53ab2d09f..769fd8926 100644 --- a/lib/Service/MembershipService.php +++ b/lib/Service/MembershipService.php @@ -350,6 +350,9 @@ function (Membership $membership): string { if (!in_array($item->getCircleId(), $circleIds)) { $deprecated[] = $item; $this->membershipRequest->delete($item); + + // clearing the getSharedWith() cache for singleId related to the membership + $this->shareWrapperService->clearCache($item->getSingleId()); } } @@ -379,6 +382,9 @@ private function createNewMemberships(array $memberships, array $known): array { $this->membershipRequest->insert($membership); $new[] = $membership; } + + // clearing the getSharedWith() cache for singleId related to the membership + $this->shareWrapperService->clearCache($membership->getSingleId()); } return $new; diff --git a/lib/Service/ShareWrapperService.php b/lib/Service/ShareWrapperService.php index ecc850d47..61c4103a6 100644 --- a/lib/Service/ShareWrapperService.php +++ b/lib/Service/ShareWrapperService.php @@ -38,10 +38,13 @@ use OCA\Circles\Model\FederatedUser; use OCA\Circles\Model\Probes\CircleProbe; use OCA\Circles\Model\ShareWrapper; +use OCA\Circles\Tools\Exceptions\InvalidItemException; use OCA\Circles\Tools\Traits\TDeserialize; use OCA\Circles\Tools\Traits\TStringTools; use OCP\Files\Folder; use OCP\Files\NotFoundException; +use OCP\ICache; +use OCP\ICacheFactory; use OCP\Share\IShare; /** @@ -53,9 +56,26 @@ class ShareWrapperService { use TStringTools; use TDeserialize; - public function __construct( - private ShareWrapperRequest $shareWrapperRequest, - ) { + public const CACHE_SHARED_WITH = 'circles/getSharedWith'; + public const CACHE_SHARED_WITH_TTL = 900; + + + /** @var ShareWrapperRequest */ + private $shareWrapperRequest; + + private ICache $cache; + + + /** + * ShareWrapperService constructor. + * + * @param ICacheFactory $cacheFactory + * @param ShareWrapperRequest $shareWrapperRequest + */ + public function __construct(ICacheFactory $cacheFactory, ShareWrapperRequest $shareWrapperRequest) { + $this->cache = $cacheFactory->createDistributed(self::CACHE_SHARED_WITH); + + $this->shareWrapperRequest = $shareWrapperRequest; } @@ -78,6 +98,7 @@ public function searchShare(string $singleId, int $nodeId): ShareWrapper { * @throws NotFoundException */ public function save(IShare $share): void { + $this->cache->clear(''); $this->shareWrapperRequest->save($share); } @@ -86,6 +107,7 @@ public function save(IShare $share): void { * @param ShareWrapper $shareWrapper */ public function update(ShareWrapper $shareWrapper): void { + $this->cache->clear(''); $this->shareWrapperRequest->update($shareWrapper); } @@ -94,6 +116,7 @@ public function update(ShareWrapper $shareWrapper): void { * @param ShareWrapper $shareWrapper */ public function delete(ShareWrapper $shareWrapper): void { + $this->cache->clear(''); $this->shareWrapperRequest->delete((int)$shareWrapper->getId()); } @@ -108,6 +131,7 @@ public function deleteUserSharesToCircle(string $circleId, string $userId): void throw new Exception('$initiator cannot be empty'); } + $this->cache->clear(''); $this->shareWrapperRequest->deleteSharesToCircle($circleId, $userId); } @@ -116,6 +140,7 @@ public function deleteUserSharesToCircle(string $circleId, string $userId): void * @param string $circleId */ public function deleteAllSharesToCircle(string $circleId): void { + $this->cache->clear(''); $this->shareWrapperRequest->deleteSharesToCircle($circleId, ''); } @@ -205,7 +230,22 @@ public function getSharedWith( int $nodeId, ?CircleProbe $probe ): array { - return $this->shareWrapperRequest->getSharedWith($federatedUser, $nodeId, $probe); + $key = $this->generateSharedWithCacheKey($federatedUser, $nodeId, $probe->getChecksum()); + + $cachedData = $this->cache->get($key); + try { + if (!is_string($cachedData)) { + throw new InvalidItemException(); + } + + return $this->deserializeList($cachedData, ShareWrapper::class); + } catch (InvalidItemException $e) { + } + + $shares = $this->shareWrapperRequest->getSharedWith($federatedUser, $nodeId, $probe); + $this->cache->set($key, json_encode($shares), self::CACHE_SHARED_WITH_TTL); + + return $shares; } @@ -269,6 +309,11 @@ public function getChild(IShare $share, FederatedUser $federatedUser): ShareWrap } + public function clearCache(string $singleId): void { + $this->cache->clear($singleId); + } + + /** * @param FederatedUser $federatedUser * @param IShare $share @@ -279,6 +324,7 @@ public function getChild(IShare $share, FederatedUser $federatedUser): ShareWrap * @throws RequestBuilderException */ private function createChild(IShare $share, FederatedUser $federatedUser): ShareWrapper { + $this->cache->clear(''); $share->setSharedWith($federatedUser->getSingleId()); $childId = $this->shareWrapperRequest->save($share, (int)$share->getId());