3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Cache\Type;
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;
30 * This class wraps cache driver, so they can get profiled - in case the profiler is enabled
32 * It is using the decorator pattern (@see https://en.wikipedia.org/wiki/Decorator_pattern )
34 class ProfilerCacheDecorator implements ICanCache, ICanCacheInMemory
37 * @var ICanCache The original cache driver
42 * @var Profiler The profiler of Friendica
46 public function __construct(ICanCache $cache, Profiler $profiler)
48 $this->cache = $cache;
49 $this->profiler = $profiler;
55 public function getAllKeys(?string $prefix = null): array
57 $this->profiler->startRecording('cache');
59 $return = $this->cache->getAllKeys($prefix);
61 $this->profiler->stopRecording();
69 public function get(string $key)
71 $this->profiler->startRecording('cache');
73 $return = $this->cache->get($key);
75 $this->profiler->stopRecording();
83 public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
85 $this->profiler->startRecording('cache');
87 $return = $this->cache->set($key, $value, $ttl);
89 $this->profiler->stopRecording();
97 public function delete(string $key): bool
99 $this->profiler->startRecording('cache');
101 $return = $this->cache->delete($key);
103 $this->profiler->stopRecording();
111 public function clear(bool $outdated = true): bool
113 $this->profiler->startRecording('cache');
115 $return = $this->cache->clear($outdated);
117 $this->profiler->stopRecording();
125 public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
127 if ($this->cache instanceof ICanCacheInMemory) {
128 $this->profiler->startRecording('cache');
130 $return = $this->cache->add($key, $value, $ttl);
132 $this->profiler->stopRecording();
143 public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
145 if ($this->cache instanceof ICanCacheInMemory) {
146 $this->profiler->startRecording('cache');
148 $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
150 $this->profiler->stopRecording();
161 public function compareDelete(string $key, $value): bool
163 if ($this->cache instanceof ICanCacheInMemory) {
164 $this->profiler->startRecording('cache');
166 $return = $this->cache->compareDelete($key, $value);
168 $this->profiler->stopRecording();
179 public function GetName(): string
181 return $this->cache->getName() . ' (with profiler)';