]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/ProfilerCacheDecorator.php
Merge branch 'friendica:develop' into 6606-k-alin-mysql-unix-socket
[friendica.git] / src / Core / Cache / Type / ProfilerCacheDecorator.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Cache\Type;
23
24 use Friendica\Core\Cache\Enum\Duration;
25 use Friendica\Core\Cache\Capability\ICanCache;
26 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
27 use Friendica\Util\Profiler;
28
29 /**
30  * This class wraps cache driver, so they can get profiled - in case the profiler is enabled
31  *
32  * It is using the decorator pattern (@see https://en.wikipedia.org/wiki/Decorator_pattern )
33  */
34 class ProfilerCacheDecorator implements ICanCache, ICanCacheInMemory
35 {
36         /**
37          * @var ICanCache The original cache driver
38          */
39         private $cache;
40
41         /**
42          * @var Profiler The profiler of Friendica
43          */
44         private $profiler;
45
46         public function __construct(ICanCache $cache, Profiler $profiler)
47         {
48                 $this->cache    = $cache;
49                 $this->profiler = $profiler;
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         public function getAllKeys(?string $prefix = null): array
56         {
57                 $this->profiler->startRecording('cache');
58
59                 $return = $this->cache->getAllKeys($prefix);
60
61                 $this->profiler->stopRecording();
62
63                 return $return;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         public function get(string $key)
70         {
71                 $this->profiler->startRecording('cache');
72
73                 $return = $this->cache->get($key);
74
75                 $this->profiler->stopRecording();
76
77                 return $return;
78         }
79
80         /**
81          * {@inheritDoc}
82          */
83         public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
84         {
85                 $this->profiler->startRecording('cache');
86
87                 $return = $this->cache->set($key, $value, $ttl);
88
89                 $this->profiler->stopRecording();
90
91                 return $return;
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         public function delete(string $key): bool
98         {
99                 $this->profiler->startRecording('cache');
100
101                 $return = $this->cache->delete($key);
102
103                 $this->profiler->stopRecording();
104
105                 return $return;
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         public function clear(bool $outdated = true): bool
112         {
113                 $this->profiler->startRecording('cache');
114
115                 $return = $this->cache->clear($outdated);
116
117                 $this->profiler->stopRecording();
118
119                 return $return;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
126         {
127                 if ($this->cache instanceof ICanCacheInMemory) {
128                         $this->profiler->startRecording('cache');
129
130                         $return = $this->cache->add($key, $value, $ttl);
131
132                         $this->profiler->stopRecording();
133
134                         return $return;
135                 } else {
136                         return false;
137                 }
138         }
139
140         /**
141          * {@inheritDoc}
142          */
143         public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
144         {
145                 if ($this->cache instanceof ICanCacheInMemory) {
146                         $this->profiler->startRecording('cache');
147
148                         $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
149
150                         $this->profiler->stopRecording();
151
152                         return $return;
153                 } else {
154                         return false;
155                 }
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         public function compareDelete(string $key, $value): bool
162         {
163                 if ($this->cache instanceof ICanCacheInMemory) {
164                         $this->profiler->startRecording('cache');
165
166                         $return = $this->cache->compareDelete($key, $value);
167
168                         $this->profiler->stopRecording();
169
170                         return $return;
171                 } else {
172                         return false;
173                 }
174         }
175
176         /**
177          * {@inheritDoc}
178          */
179         public function GetName(): string
180         {
181                 return $this->cache->getName() . ' (with profiler)';
182         }
183 }