Skip to content

Commit

Permalink
Move all caching to helper
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed May 11, 2022
1 parent ed3be36 commit 03f4006
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 223 deletions.
52 changes: 52 additions & 0 deletions lib/Cache/AttachmentCacheHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* @copyright Copyright (c) 2020 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/>.
*
*/

declare(strict_types=1);


namespace OCA\Deck\Cache;

use OCP\ICache;
use OCP\ICacheFactory;

class AttachmentCacheHelper {
/** @var ICache */
private $cache;

public function __construct(ICacheFactory $cacheFactory) {
$this->cache = $cacheFactory->createDistributed('deck-attachments');
}

public function getAttachmentCount(int $cardId): ?int {
return $this->cache->get('count-' . $cardId);
}

public function setAttachmentCount(int $cardId, int $count): void {
$this->cache->set('count-' . $cardId, $count);
}

public function clearAttachmentCount(int $cardId): void {
$this->cache->remove('count-' . $cardId);
}
}
31 changes: 16 additions & 15 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
use OCA\Deck\InvalidAttachmentType;
use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException;
use OCA\Deck\Cache\AttachmentCacheHelper;
use OCA\Deck\StatusException;
use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Http\Response;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IL10N;

class AttachmentService {
Expand All @@ -49,23 +48,24 @@ class AttachmentService {

/** @var IAttachmentService[] */
private $services = [];
/** @var Application */
private $application;
/** @var ICache */
private $cache;
/** @var AttachmentCacheHelper */
private $attachmentCacheHelper;
/** @var IL10N */
private $l10n;
/** @var ActivityManager */
private $activityManager;
/** @var ChangeHelper */
private $changeHelper;

public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n, ActivityManager $activityManager) {
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, AttachmentCacheHelper $attachmentCacheHelper, $userId, IL10N $l10n, ActivityManager $activityManager) {
$this->attachmentMapper = $attachmentMapper;
$this->cardMapper = $cardMapper;
$this->permissionService = $permissionService;
$this->userId = $userId;
$this->application = $application;
$this->cache = $cacheFactory->createDistributed('deck-card-attachments-');
$this->attachmentCacheHelper = $attachmentCacheHelper;
$this->l10n = $l10n;
$this->activityManager = $activityManager;
$this->changeHelper = $changeHelper;
Expand Down Expand Up @@ -137,17 +137,18 @@ public function findAll($cardId, $withDeleted = false) {

/**
* @param $cardId
* @param bool $update | Force the update of the cached values
* @return int|mixed
* @throws BadRequestException
* @throws InvalidAttachmentType
* @throws \OCP\DB\Exception
*/
public function count($cardId, $update = false) {
public function count($cardId) {
if (is_numeric($cardId) === false) {
throw new BadRequestException('card id must be a number');
}

$count = $this->cache->get('card-' . $cardId);
if ($update || !$count) {
$count = $this->attachmentCacheHelper->getAttachmentCount((int)$cardId);
if ($count === null) {
$count = count($this->attachmentMapper->findAll($cardId));

foreach (array_keys($this->services) as $attachmentType) {
Expand All @@ -157,7 +158,7 @@ public function count($cardId, $update = false) {
}
}

$this->cache->set('card-' . $cardId, $count);
$this->attachmentCacheHelper->setAttachmentCount((int)$cardId, $count);
}

return $count;
Expand Down Expand Up @@ -187,7 +188,7 @@ public function create($cardId, $type, $data) {

$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);

$this->cache->clear('card-' . $cardId);
$this->attachmentCacheHelper->clearAttachmentCount((int)$cardId);
$attachment = new Attachment();
$attachment->setCardId($cardId);
$attachment->setType($type);
Expand Down Expand Up @@ -299,7 +300,7 @@ public function update($cardId, $attachmentId, $data, $type = 'deck_file') {
}

$this->permissionService->checkPermission($this->cardMapper, $attachment->getCardId(), Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $attachment->getCardId());
$this->attachmentCacheHelper->clearAttachmentCount($cardId);

$attachment->setData($data);
try {
Expand Down Expand Up @@ -357,7 +358,7 @@ public function delete(int $cardId, int $attachmentId, string $type = 'deck_file
}
}

$this->cache->clear('card-' . $attachment->getCardId());
$this->attachmentCacheHelper->clearAttachmentCount($cardId);
$this->changeHelper->cardChanged($attachment->getCardId());
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $attachment, ActivityManager::SUBJECT_ATTACHMENT_DELETE);
return $attachment;
Expand All @@ -371,7 +372,7 @@ public function restore(int $cardId, int $attachmentId, string $type = 'deck_fil
}

$this->permissionService->checkPermission($this->cardMapper, $attachment->getCardId(), Acl::PERMISSION_EDIT);
$this->cache->clear('card-' . $attachment->getCardId());
$this->attachmentCacheHelper->clearAttachmentCount($cardId);

try {
$service = $this->getService($attachment->getType());
Expand Down
145 changes: 0 additions & 145 deletions lib/Sharing/DeckShareFileCountHelper.php

This file was deleted.

Loading

0 comments on commit 03f4006

Please sign in to comment.