CacheService::getBackupCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Service;
19
20
use CuyZ\Notiz\Core\Support\NotizConstants;
21
use CuyZ\Notiz\Service\Traits\ExtendedSelfInstantiateTrait;
22
use TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend;
23
use TYPO3\CMS\Core\Cache\CacheManager;
24
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
25
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
26
use TYPO3\CMS\Core\Log\LogManager;
27
use TYPO3\CMS\Core\SingletonInterface;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
class CacheService implements SingletonInterface
31
{
32
    use ExtendedSelfInstantiateTrait {
33
        get as getInstance;
34
    }
35
36
    const BACKUP_CACHE_WARNING_MESSAGE = 'The cache instance for NotiZ core could not be loaded. If this message is written often in your logs, your TYPO3 instance may suffer from performance issues.';
37
38
    /**
39
     * @var FrontendInterface
40
     */
41
    protected $cacheInstance;
42
43
    /**
44
     * @var CacheManager
45
     */
46
    protected $cacheManager;
47
48
    /**
49
     * @var LogManager
50
     */
51
    protected $logManager;
52
53
    /**
54
     * @param CacheManager $cacheManager
55
     * @param LogManager $logManager
56
     */
57
    public function __construct(CacheManager $cacheManager, LogManager $logManager)
58
    {
59
        $this->cacheManager = $cacheManager;
60
        $this->logManager = $logManager;
61
    }
62
63
    /**
64
     * Returns a cache value.
65
     *
66
     * @param string $key
67
     * @return mixed
68
     */
69
    public function get(string $key)
70
    {
71
        return $this->getCacheInstance()->get($key);
72
    }
73
74
    /**
75
     * Stores a value in the cache.
76
     *
77
     * @param string $key
78
     * @param mixed $data
79
     * @param array $tags
80
     * @return $this
81
     */
82
    public function set(string $key, $data, array $tags = [])
83
    {
84
        $this->getCacheInstance()->set($key, $data, $tags);
85
86
        return $this;
87
    }
88
89
    /**
90
     * Checks if an entry exists.
91
     *
92
     * @param string $key
93
     * @return bool
94
     */
95
    public function has(string $key): bool
96
    {
97
        return $this->getCacheInstance()->has($key);
98
    }
99
100
    /**
101
     * Checks if the cache for this extension was registered in TYPO3 core
102
     * manager.
103
     *
104
     * @return bool
105
     */
106
    public function cacheIsRegistered(): bool
107
    {
108
        return $this->cacheManager->hasCache(NotizConstants::CACHE_ID);
109
    }
110
111
    /**
112
     * Returns the extension cache frontend.
113
     *
114
     * If something was wrong during the cache registering and it is not found
115
     * in the cache manager, a backup cache is created to allow the extension
116
     * to run still.
117
     *
118
     * @return FrontendInterface
119
     */
120
    protected function getCacheInstance(): FrontendInterface
121
    {
122
        if (null === $this->cacheInstance) {
123
            $this->cacheInstance = $this->cacheManager->hasCache(NotizConstants::CACHE_ID)
124
                ? $this->cacheManager->getCache(NotizConstants::CACHE_ID)
125
                : $this->getBackupCache();
126
        }
127
128
        return $this->cacheInstance;
129
    }
130
131
    /**
132
     * Creates a transient memory cache, that will not allow true cache storage,
133
     * but will allow the extension to still use this service.
134
     *
135
     * This should not (or rarely) happen, so a warning is written in the logs
136
     * when this method is called.
137
     *
138
     * @return VariableFrontend
139
     */
140
    protected function getBackupCache(): VariableFrontend
141
    {
142
        $logger = $this->logManager->getLogger(__CLASS__);
143
        $logger->warning(self::BACKUP_CACHE_WARNING_MESSAGE);
144
145
        /** @var TransientMemoryBackend $backend */
146
        $backend = GeneralUtility::makeInstance(TransientMemoryBackend::class, 'production');
147
148
        /** @var VariableFrontend $frontend */
149
        $frontend = GeneralUtility::makeInstance(VariableFrontend::class, NotizConstants::CACHE_ID, $backend);
150
151
        return $frontend;
152
    }
153
}
154