]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/ProfilerCache.php
Merge branch 'bug/phpinfo-accessible-hotfix' into develop
[friendica.git] / src / Core / Cache / ProfilerCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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;
23
24 use Friendica\Core\System;
25 use Friendica\Util\Profiler;
26
27 /**
28  * This class wraps cache driver so they can get profiled - in case the profiler is enabled
29  *
30  * It is using the decorator pattern (@see
31  */
32 class ProfilerCache implements ICache, IMemoryCache
33 {
34         /**
35          * @var ICache The original cache driver
36          */
37         private $cache;
38
39         /**
40          * @var Profiler The profiler of Friendica
41          */
42         private $profiler;
43
44         public function __construct(ICache $cache, Profiler $profiler)
45         {
46                 $this->cache    = $cache;
47                 $this->profiler = $profiler;
48         }
49
50         /**
51          * {@inheritDoc}
52          */
53         public function getAllKeys($prefix = null)
54         {
55                 $time = microtime(true);
56
57                 $return = $this->cache->getAllKeys($prefix);
58
59                 $this->profiler->saveTimestamp($time, 'cache');
60
61                 return $return;
62         }
63
64         /**
65          * {@inheritDoc}
66          */
67         public function get($key)
68         {
69                 $time = microtime(true);
70
71                 $return = $this->cache->get($key);
72
73                 $this->profiler->saveTimestamp($time, 'cache');
74
75                 return $return;
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
82         {
83                 $time = microtime(true);
84
85                 $return = $this->cache->set($key, $value, $ttl);
86
87                 $this->profiler->saveTimestamp($time, 'cache');
88
89                 return $return;
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         public function delete($key)
96         {
97                 $time = microtime(true);
98
99                 $return = $this->cache->delete($key);
100
101                 $this->profiler->saveTimestamp($time, 'cache');
102
103                 return $return;
104         }
105
106         /**
107          * {@inheritDoc}
108          */
109         public function clear($outdated = true)
110         {
111                 $time = microtime(true);
112
113                 $return = $this->cache->clear($outdated);
114
115                 $this->profiler->saveTimestamp($time, 'cache');
116
117                 return $return;
118         }
119
120         /**
121          * {@inheritDoc}
122          */
123         public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
124         {
125                 if ($this->cache instanceof IMemoryCache) {
126                         $time = microtime(true);
127
128                         $return = $this->cache->add($key, $value, $ttl);
129
130                         $this->profiler->saveTimestamp($time, 'cache');
131
132                         return $return;
133                 } else {
134                         return false;
135                 }
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
142         {
143                 if ($this->cache instanceof IMemoryCache) {
144                         $time = microtime(true);
145
146                         $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
147
148                         $this->profiler->saveTimestamp($time, 'cache');
149
150                         return $return;
151                 } else {
152                         return false;
153                 }
154         }
155
156         /**
157          * {@inheritDoc}
158          */
159         public function compareDelete($key, $value)
160         {
161                 if ($this->cache instanceof IMemoryCache) {
162                         $time = microtime(true);
163
164                         $return = $this->cache->compareDelete($key, $value);
165
166                         $this->profiler->saveTimestamp($time, 'cache');
167
168                         return $return;
169                 } else {
170                         return false;
171                 }
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         public function GetName()
178         {
179                 return $this->cache->getName() . ' (with profiler)';
180         }
181 }