3 * @copyright Copyright (C) 2010-2021, 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;
24 use Friendica\Core\System;
25 use Friendica\Util\Profiler;
28 * This class wraps cache driver so they can get profiled - in case the profiler is enabled
30 * It is using the decorator pattern (@see
32 class ProfilerCache implements ICache, IMemoryCache
35 * @var ICache The original cache driver
40 * @var Profiler The profiler of Friendica
44 public function __construct(ICache $cache, Profiler $profiler)
46 $this->cache = $cache;
47 $this->profiler = $profiler;
53 public function getAllKeys($prefix = null)
55 $time = microtime(true);
57 $return = $this->cache->getAllKeys($prefix);
59 $this->profiler->saveTimestamp($time, 'cache');
67 public function get($key)
69 $time = microtime(true);
71 $return = $this->cache->get($key);
73 $this->profiler->saveTimestamp($time, 'cache');
81 public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
83 $time = microtime(true);
85 $return = $this->cache->set($key, $value, $ttl);
87 $this->profiler->saveTimestamp($time, 'cache');
95 public function delete($key)
97 $time = microtime(true);
99 $return = $this->cache->delete($key);
101 $this->profiler->saveTimestamp($time, 'cache');
109 public function clear($outdated = true)
111 $time = microtime(true);
113 $return = $this->cache->clear($outdated);
115 $this->profiler->saveTimestamp($time, 'cache');
123 public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
125 if ($this->cache instanceof IMemoryCache) {
126 $time = microtime(true);
128 $return = $this->cache->add($key, $value, $ttl);
130 $this->profiler->saveTimestamp($time, 'cache');
141 public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
143 if ($this->cache instanceof IMemoryCache) {
144 $time = microtime(true);
146 $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
148 $this->profiler->saveTimestamp($time, 'cache');
159 public function compareDelete($key, $value)
161 if ($this->cache instanceof IMemoryCache) {
162 $time = microtime(true);
164 $return = $this->cache->compareDelete($key, $value);
166 $this->profiler->saveTimestamp($time, 'cache');
177 public function GetName()
179 return $this->cache->getName() . ' (with profiler)';