Skip to content

Commit dfc4d61

Browse files
author
Konrad Michalik
authored
Merge pull request #2 from xima-media/news-cache
News cache
2 parents a848654 + e2c44ad commit dfc4d61

8 files changed

Lines changed: 155 additions & 47 deletions

File tree

Classes/Backend/ToolbarItems/NewsItem.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Xima\XimaTypo3InternalNews\Backend\ToolbarItems;
46

57
use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
@@ -33,7 +35,7 @@ public function checkAccess(): bool
3335
*/
3436
public function getItem(): string
3537
{
36-
$items = $this->newsRepository->findAllByCurrentUser()->toArray();
38+
$items = $this->newsRepository->findAllByCurrentUser();
3739
$newItemsCount = count(array_filter($items, fn ($item) => $item->isNew()));
3840
$view = GeneralUtility::makeInstance(StandaloneView::class);
3941
$view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:' . Configuration::EXT_KEY

Classes/Controller/DateController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828

2929
public function notifiesAction(): ResponseInterface
3030
{
31-
$newsList = $this->newsRepository->findAllByCurrentUser()->toArray();
31+
$newsList = $this->newsRepository->findAllByCurrentUser();
3232
$notifies = DateService::getNotifyDatesByNewsList($newsList);
3333

3434
return new JsonResponse([

Classes/Domain/Repository/NewsRepository.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,54 @@
55
namespace Xima\XimaTypo3InternalNews\Domain\Repository;
66

77
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
8-
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
98
use TYPO3\CMS\Extbase\Persistence\Repository;
9+
use Xima\XimaTypo3InternalNews\Service\CacheService;
1010

1111
class NewsRepository extends Repository
1212
{
13+
public function __construct(
14+
private CacheService $cache
15+
) {
16+
parent::__construct();
17+
}
18+
1319
protected $defaultOrderings = [
1420
'tstamp' => QueryInterface::ORDER_DESCENDING,
1521
];
1622

17-
public function findAllByCurrentUser(int|null $limit = null): QueryResultInterface|null
23+
public function findAllByCurrentUser(int|null $limit = null): array|null
1824
{
25+
$userGroups = array_keys($GLOBALS['BE_USER']->userGroups);
26+
$cacheIdentifier = $this->cache->generateCacheIdentifier($userGroups);
27+
if ($this->cache->has($cacheIdentifier)) {
28+
return $this->cache->get($cacheIdentifier);
29+
}
30+
1931
if ($GLOBALS['BE_USER']->isAdmin()) {
20-
return $this->findAll();
32+
$result = $this->findAll()->toArray();
33+
$this->cache->set($cacheIdentifier, $result);
34+
return $result;
2135
}
22-
$userGroups = array_keys($GLOBALS['BE_USER']->userGroups);
2336

2437
$query = $this->createQuery();
25-
$query->matching(
26-
$query->logicalOr(
27-
$query->equals('be_group', 0),
28-
$query->in('be_group', $userGroups)
29-
)
30-
);
38+
39+
if (!empty($userGroups)) {
40+
$query->matching(
41+
$query->logicalOr(
42+
$query->equals('be_group', 0),
43+
$query->in('be_group', $userGroups)
44+
)
45+
);
46+
} else {
47+
$query->equals('be_group', 0);
48+
}
3149

3250
if ($limit) {
3351
$query->setLimit($limit);
3452
}
35-
return $query->execute();
53+
54+
$result = $query->execute()->toArray();
55+
$this->cache->set($cacheIdentifier, $result);
56+
return $result;
3657
}
3758
}

Classes/Hooks/DataHandler.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Xima\XimaTypo3InternalNews\Hooks;
6+
7+
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
8+
9+
class DataHandler
10+
{
11+
public function __construct(private FrontendInterface $cache)
12+
{
13+
}
14+
15+
public function clearCachePostProc(array $params): void
16+
{
17+
$this->cache->flushByTags(array_keys($params['tags']));
18+
}
19+
}

Classes/Service/CacheService.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Xima\XimaTypo3InternalNews\Service;
6+
7+
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
8+
use Xima\XimaTypo3InternalNews\Configuration;
9+
10+
class CacheService
11+
{
12+
public function __construct(
13+
private FrontendInterface $cache
14+
) {
15+
}
16+
17+
public function has(string $identifier): bool
18+
{
19+
return $this->cache->has($identifier);
20+
}
21+
22+
public function get(string $identifier): mixed
23+
{
24+
return $this->cache->get($identifier);
25+
}
26+
27+
public function set(string $identifier, mixed $data): void
28+
{
29+
$this->cache->set($identifier, $data, $this->collectCacheTags($data));
30+
}
31+
32+
public function generateCacheIdentifier(array $userGroups = []): string
33+
{
34+
if ($GLOBALS['BE_USER']->isAdmin()) {
35+
return Configuration::EXT_KEY . '--all';
36+
}
37+
sort($userGroups);
38+
return Configuration::EXT_KEY . '--' . implode('_', $userGroups);
39+
}
40+
41+
private function collectCacheTags(array $data): array
42+
{
43+
$tags = ['tx_ximatypo3internalnews_domain_model_news'];
44+
foreach ($data as $item) {
45+
$tags[] = 'tx_ximatypo3internalnews_domain_model_news_' . $item->getUid();
46+
}
47+
return $tags;
48+
}
49+
}

Classes/Widgets/Provider/InternalNewsDataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public function __construct(protected NewsRepository $newsRepository)
1515

1616
public function getItems(): array
1717
{
18-
return $this->newsRepository->findAllByCurrentUser()->toArray();
18+
return $this->newsRepository->findAllByCurrentUser();
1919
}
2020
}

Configuration/Services.yaml

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
services:
2-
_defaults:
3-
autowire: true
4-
autoconfigure: true
5-
public: false
6-
Xima\XimaTypo3InternalNews\:
7-
resource: '../Classes/*'
8-
exclude: '../Classes/Domain/Model/*'
9-
Xima\XimaTypo3InternalNews\Widgets\Provider\CreateInternalNewsButtonProvider:
10-
arguments:
11-
$title: 'LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.button.create'
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: false
6+
Xima\XimaTypo3InternalNews\:
7+
resource: "../Classes/*"
8+
exclude: "../Classes/Domain/Model/*"
129

13-
Xima\XimaTypo3InternalNews\Widgets\Provider\ListInternalNewsButtonProvider:
14-
arguments:
15-
$title: 'LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.button.list'
16-
dashboard.widget.InternalNews-news:
17-
class: 'Xima\XimaTypo3InternalNews\Widgets\InternalNewsWidget'
18-
arguments:
19-
$dataProvider: '@Xima\XimaTypo3InternalNews\Widgets\Provider\InternalNewsDataProvider'
20-
$buttons:
21-
- '@Xima\XimaTypo3InternalNews\Widgets\Provider\CreateInternalNewsButtonProvider'
22-
- '@Xima\XimaTypo3InternalNews\Widgets\Provider\ListInternalNewsButtonProvider'
23-
$options:
24-
refreshAvailable: true
25-
tags:
26-
- name: dashboard.widget
27-
identifier: 'internalNews-news'
28-
groupNames: 'news'
29-
title: 'LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.title'
30-
description: 'LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.description'
31-
iconIdentifier: 'internal-news-news-color'
32-
height: 'medium'
10+
cache.xima_typo3_internal_news_cache:
11+
class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
12+
factory: ['@TYPO3\CMS\Core\Cache\CacheManager', "getCache"]
13+
arguments: ["xima_typo3_internal_news_cache"]
14+
15+
Xima\XimaTypo3InternalNews\Service\CacheService:
16+
arguments:
17+
$cache: "@cache.xima_typo3_internal_news_cache"
18+
19+
Xima\XimaTypo3InternalNews\Hooks\DataHandler:
20+
public: true
21+
arguments:
22+
$cache: "@cache.xima_typo3_internal_news_cache"
23+
24+
Xima\XimaTypo3InternalNews\Widgets\Provider\CreateInternalNewsButtonProvider:
25+
arguments:
26+
$title: "LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.button.create"
27+
28+
Xima\XimaTypo3InternalNews\Widgets\Provider\ListInternalNewsButtonProvider:
29+
arguments:
30+
$title: "LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.button.list"
31+
32+
dashboard.widget.InternalNews-news:
33+
class: 'Xima\XimaTypo3InternalNews\Widgets\InternalNewsWidget'
34+
arguments:
35+
$dataProvider: '@Xima\XimaTypo3InternalNews\Widgets\Provider\InternalNewsDataProvider'
36+
$buttons:
37+
- '@Xima\XimaTypo3InternalNews\Widgets\Provider\CreateInternalNewsButtonProvider'
38+
- '@Xima\XimaTypo3InternalNews\Widgets\Provider\ListInternalNewsButtonProvider'
39+
$options:
40+
refreshAvailable: true
41+
tags:
42+
- name: dashboard.widget
43+
identifier: "internalNews-news"
44+
groupNames: "news"
45+
title: "LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.title"
46+
description: "LLL:EXT:xima_typo3_internal_news/Resources/Private/Language/locallang.xlf:widgets.internalNews.news.description"
47+
iconIdentifier: "internal-news-news-color"
48+
height: "medium"

ext_localconf.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
if (!defined('TYPO3')) {
4-
die('Access denied.');
5-
}
3+
use Xima\XimaTypo3InternalNews\Hooks\DataHandler;
64

75
$GLOBALS['TYPO3_CONF_VARS']['BE']['toolbarItems'][1726561044] = \Xima\XimaTypo3InternalNews\Backend\ToolbarItems\NewsItem::class;
6+
7+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['xima_typo3_internal_news_cache'] ??= [];
8+
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['clearCachePostProc']['xima_typo3_internal_news_cache'] = DataHandler::class . '->clearCachePostProc';

0 commit comments

Comments
 (0)