]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/ProfilerCache.php
Merge pull request #7945 from MrPetovan/bug/fatal-errors
[friendica.git] / src / Core / Cache / ProfilerCache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 use Friendica\Core\System;
6 use Friendica\Util\Profiler;
7
8 /**
9  * This class wraps cache driver so they can get profiled - in case the profiler is enabled
10  *
11  * It is using the decorator pattern (@see
12  */
13 class ProfilerCache implements ICache, IMemoryCache
14 {
15         /**
16          * @var ICache The original cache driver
17          */
18         private $cache;
19
20         /**
21          * @var Profiler The profiler of Friendica
22          */
23         private $profiler;
24
25         public function __construct(ICache $cache, Profiler $profiler)
26         {
27                 $this->cache    = $cache;
28                 $this->profiler = $profiler;
29         }
30
31         /**
32          * {@inheritDoc}
33          */
34         public function getAllKeys($prefix = null)
35         {
36                 $time = microtime(true);
37
38                 $return = $this->cache->getAllKeys($prefix);
39
40                 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
41
42                 return $return;
43         }
44
45         /**
46          * {@inheritDoc}
47          */
48         public function get($key)
49         {
50                 $time = microtime(true);
51
52                 $return = $this->cache->get($key);
53
54                 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
55
56                 return $return;
57         }
58
59         /**
60          * {@inheritDoc}
61          */
62         public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
63         {
64                 $time = microtime(true);
65
66                 $return = $this->cache->set($key, $value, $ttl);
67
68                 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
69
70                 return $return;
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         public function delete($key)
77         {
78                 $time = microtime(true);
79
80                 $return = $this->cache->delete($key);
81
82                 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
83
84                 return $return;
85         }
86
87         /**
88          * {@inheritDoc}
89          */
90         public function clear($outdated = true)
91         {
92                 $time = microtime(true);
93
94                 $return = $this->cache->clear($outdated);
95
96                 $this->profiler->saveTimestamp($time, 'cache', System::callstack());
97
98                 return $return;
99         }
100
101         /**
102          * {@inheritDoc}
103          */
104         public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
105         {
106                 if ($this->cache instanceof IMemoryCache) {
107                         $time = microtime(true);
108
109                         $return = $this->cache->add($key, $value, $ttl);
110
111                         $this->profiler->saveTimestamp($time, 'cache', System::callstack());
112
113                         return $return;
114                 } else {
115                         return false;
116                 }
117         }
118
119         /**
120          * {@inheritDoc}
121          */
122         public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
123         {
124                 if ($this->cache instanceof IMemoryCache) {
125                         $time = microtime(true);
126
127                         $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
128
129                         $this->profiler->saveTimestamp($time, 'cache', System::callstack());
130
131                         return $return;
132                 } else {
133                         return false;
134                 }
135         }
136
137         /**
138          * {@inheritDoc}
139          */
140         public function compareDelete($key, $value)
141         {
142                 if ($this->cache instanceof IMemoryCache) {
143                         $time = microtime(true);
144
145                         $return = $this->cache->compareDelete($key, $value);
146
147                         $this->profiler->saveTimestamp($time, 'cache', System::callstack());
148
149                         return $return;
150                 } else {
151                         return false;
152                 }
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         public function GetName()
159         {
160                 return $this->cache->getName() . ' (with profiler)';
161         }
162 }