Skip to content

Commit 03f4006

Browse files
committed
Move all caching to helper
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent ed3be36 commit 03f4006

7 files changed

Lines changed: 111 additions & 223 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
5+
*
6+
* @author Julius Härtl <jus@bitgrid.net>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
28+
namespace OCA\Deck\Cache;
29+
30+
use OCP\ICache;
31+
use OCP\ICacheFactory;
32+
33+
class AttachmentCacheHelper {
34+
/** @var ICache */
35+
private $cache;
36+
37+
public function __construct(ICacheFactory $cacheFactory) {
38+
$this->cache = $cacheFactory->createDistributed('deck-attachments');
39+
}
40+
41+
public function getAttachmentCount(int $cardId): ?int {
42+
return $this->cache->get('count-' . $cardId);
43+
}
44+
45+
public function setAttachmentCount(int $cardId, int $count): void {
46+
$this->cache->set('count-' . $cardId, $count);
47+
}
48+
49+
public function clearAttachmentCount(int $cardId): void {
50+
$this->cache->remove('count-' . $cardId);
51+
}
52+
}

lib/Service/AttachmentService.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434
use OCA\Deck\InvalidAttachmentType;
3535
use OCA\Deck\NoPermissionException;
3636
use OCA\Deck\NotFoundException;
37+
use OCA\Deck\Cache\AttachmentCacheHelper;
3738
use OCA\Deck\StatusException;
3839
use OCP\AppFramework\Db\IMapperException;
3940
use OCP\AppFramework\Http\Response;
40-
use OCP\ICache;
41-
use OCP\ICacheFactory;
4241
use OCP\IL10N;
4342

4443
class AttachmentService {
@@ -49,23 +48,24 @@ class AttachmentService {
4948

5049
/** @var IAttachmentService[] */
5150
private $services = [];
51+
/** @var Application */
5252
private $application;
53-
/** @var ICache */
54-
private $cache;
53+
/** @var AttachmentCacheHelper */
54+
private $attachmentCacheHelper;
5555
/** @var IL10N */
5656
private $l10n;
5757
/** @var ActivityManager */
5858
private $activityManager;
5959
/** @var ChangeHelper */
6060
private $changeHelper;
6161

62-
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n, ActivityManager $activityManager) {
62+
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, ChangeHelper $changeHelper, PermissionService $permissionService, Application $application, AttachmentCacheHelper $attachmentCacheHelper, $userId, IL10N $l10n, ActivityManager $activityManager) {
6363
$this->attachmentMapper = $attachmentMapper;
6464
$this->cardMapper = $cardMapper;
6565
$this->permissionService = $permissionService;
6666
$this->userId = $userId;
6767
$this->application = $application;
68-
$this->cache = $cacheFactory->createDistributed('deck-card-attachments-');
68+
$this->attachmentCacheHelper = $attachmentCacheHelper;
6969
$this->l10n = $l10n;
7070
$this->activityManager = $activityManager;
7171
$this->changeHelper = $changeHelper;
@@ -137,17 +137,18 @@ public function findAll($cardId, $withDeleted = false) {
137137

138138
/**
139139
* @param $cardId
140-
* @param bool $update | Force the update of the cached values
141140
* @return int|mixed
142141
* @throws BadRequestException
142+
* @throws InvalidAttachmentType
143+
* @throws \OCP\DB\Exception
143144
*/
144-
public function count($cardId, $update = false) {
145+
public function count($cardId) {
145146
if (is_numeric($cardId) === false) {
146147
throw new BadRequestException('card id must be a number');
147148
}
148149

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

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

160-
$this->cache->set('card-' . $cardId, $count);
161+
$this->attachmentCacheHelper->setAttachmentCount((int)$cardId, $count);
161162
}
162163

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

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

190-
$this->cache->clear('card-' . $cardId);
191+
$this->attachmentCacheHelper->clearAttachmentCount((int)$cardId);
191192
$attachment = new Attachment();
192193
$attachment->setCardId($cardId);
193194
$attachment->setType($type);
@@ -299,7 +300,7 @@ public function update($cardId, $attachmentId, $data, $type = 'deck_file') {
299300
}
300301

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

304305
$attachment->setData($data);
305306
try {
@@ -357,7 +358,7 @@ public function delete(int $cardId, int $attachmentId, string $type = 'deck_file
357358
}
358359
}
359360

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

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

376377
try {
377378
$service = $this->getService($attachment->getType());

lib/Sharing/DeckShareFileCountHelper.php

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)