3434use OCA \Deck \InvalidAttachmentType ;
3535use OCA \Deck \NoPermissionException ;
3636use OCA \Deck \NotFoundException ;
37+ use OCA \Deck \Cache \AttachmentCacheHelper ;
3738use OCA \Deck \StatusException ;
3839use OCP \AppFramework \Db \IMapperException ;
3940use OCP \AppFramework \Http \Response ;
40- use OCP \ICache ;
41- use OCP \ICacheFactory ;
4241use OCP \IL10N ;
4342
4443class 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 ());
0 commit comments