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