3 namespace Friendica\Core\Cache;
5 use Friendica\Core\Cache;
6 use Friendica\Core\System;
7 use Friendica\Util\Profiler;
10 * This class wraps cache driver so they can get profiled - in case the profiler is enabled
12 * It is using the decorator pattern (@see
14 class ProfilerCache implements ICache, IMemoryCache
17 * @var ICache The original cache driver
22 * @var Profiler The profiler of Friendica
26 public function __construct(ICache $cache, Profiler $profiler)
28 $this->cache = $cache;
29 $this->profiler = $profiler;
35 public function getAllKeys($prefix = null)
37 $time = microtime(true);
39 $return = $this->cache->getAllKeys($prefix);
41 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
49 public function get($key)
51 $time = microtime(true);
53 $return = $this->cache->get($key);
55 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
63 public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
65 $time = microtime(true);
67 $return = $this->cache->set($key, $value, $ttl);
69 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
77 public function delete($key)
79 $time = microtime(true);
81 $return = $this->cache->delete($key);
83 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
91 public function clear($outdated = true)
93 $time = microtime(true);
95 $return = $this->cache->clear($outdated);
97 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
105 public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
107 if ($this->cache instanceof IMemoryCache) {
108 $time = microtime(true);
110 $return = $this->cache->add($key, $value, $ttl);
112 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
123 public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
125 if ($this->cache instanceof IMemoryCache) {
126 $time = microtime(true);
128 $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
130 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
141 public function compareDelete($key, $value)
143 if ($this->cache instanceof IMemoryCache) {
144 $time = microtime(true);
146 $return = $this->cache->compareDelete($key, $value);
148 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
156 public function __toString()
158 return (string)$this->cache . ' (with profiler)';