]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Add Temporal::utcNow()
[friendica.git] / src / Core / Cache.php
1 <?php
2 /**
3  * @file src/Core/Cache.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Util\Temporal;
10 use dba;
11 use Memcache;
12
13 require_once 'include/dba.php';
14
15 /**
16  * @brief Class for storing data for a short time
17  */
18 class Cache
19 {
20         /**
21          * @brief Check for memcache and open a connection if configured
22          *
23          * @return object|boolean The memcache object - or "false" if not successful
24          */
25         public static function memcache()
26         {
27                 if (!function_exists('memcache_connect')) {
28                         return false;
29                 }
30
31                 if (!Config::get('system', 'memcache')) {
32                         return false;
33                 }
34
35                 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
36                 $memcache_port = Config::get('system', 'memcache_port', 11211);
37
38                 $memcache = new Memcache;
39
40                 if (!$memcache->connect($memcache_host, $memcache_port)) {
41                         return false;
42                 }
43
44                 return $memcache;
45         }
46
47         /**
48          * @brief Return the duration for a given cache level
49          *
50          * @param integer $level Cache level
51          *
52          * @return integer The cache duration in seconds
53          */
54         private static function duration($level)
55         {
56                 switch ($level) {
57                         case CACHE_MONTH:
58                                 $seconds = 2592000;
59                                 break;
60                         case CACHE_WEEK:
61                                 $seconds = 604800;
62                                 break;
63                         case CACHE_DAY:
64                                 $seconds = 86400;
65                                 break;
66                         case CACHE_HOUR:
67                                 $seconds = 3600;
68                                 break;
69                         case CACHE_HALF_HOUR:
70                                 $seconds = 1800;
71                                 break;
72                         case CACHE_QUARTER_HOUR:
73                                 $seconds = 900;
74                                 break;
75                         case CACHE_FIVE_MINUTES:
76                                 $seconds = 300;
77                                 break;
78                         case CACHE_MINUTE:
79                                 $seconds = 60;
80                                 break;
81                 }
82                 return $seconds;
83         }
84
85         /**
86          * @brief Fetch cached data according to the key
87          *
88          * @param string $key The key to the cached data
89          *
90          * @return mixed Cached $value or "null" if not found
91          */
92         public static function get($key)
93         {
94                 $memcache = self::memcache();
95                 if (is_object($memcache)) {
96                         // We fetch with the hostname as key to avoid problems with other applications
97                         $cached = $memcache->get(get_app()->get_hostname().":".$key);
98                         $value = @unserialize($cached);
99
100                         // Only return a value if the serialized value is valid.
101                         // We also check if the db entry is a serialized
102                         // boolean 'false' value (which we want to return).
103                         if ($cached === serialize(false) || $value !== false) {
104                                 return $value;
105                         }
106
107                         return null;
108                 }
109
110                 // Frequently clear cache
111                 self::clear();
112
113                 $cache = dba::selectFirst('cache', ['v'], ['k' => $key]);
114
115                 if (DBM::is_result($cache)) {
116                         $cached = $cache['v'];
117                         $value = @unserialize($cached);
118
119                         // Only return a value if the serialized value is valid.
120                         // We also check if the db entry is a serialized
121                         // boolean 'false' value (which we want to return).
122                         if ($cached === serialize(false) || $value !== false) {
123                                 return $value;
124                         }
125                 }
126
127                 return null;
128         }
129
130         /**
131          * @brief Put data in the cache according to the key
132          *
133          * The input $value can have multiple formats.
134          *
135          * @param string  $key      The key to the cached data
136          * @param mixed   $value    The value that is about to be stored
137          * @param integer $duration The cache lifespan
138          *
139          * @return void
140          */
141         public static function set($key, $value, $duration = CACHE_MONTH)
142         {
143                 // Do we have an installed memcache? Use it instead.
144                 $memcache = self::memcache();
145                 if (is_object($memcache)) {
146                         // We store with the hostname as key to avoid problems with other applications
147                         $memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration));
148                         return;
149                 }
150                 $fields = ['v' => serialize($value), 'expire_mode' => $duration, 'updated' => Temporal::utcNow()];
151                 $condition = ['k' => $key];
152                 dba::update('cache', $fields, $condition, true);
153         }
154
155         /**
156          * @brief Remove outdated data from the cache
157          *
158          * @param integer $max_level The maximum cache level that is to be cleared
159          *
160          * @return void
161          */
162         public static function clear($max_level = CACHE_MONTH)
163         {
164                 // Clear long lasting cache entries only once a day
165                 if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
166                         if ($max_level == CACHE_MONTH) {
167                                 $condition = ["`updated` < ? AND `expire_mode` = ?",
168                                                 Temporal::convert("now - 30 days"),
169                                                 CACHE_MONTH];
170                                 dba::delete('cache', $condition);
171                         }
172
173                         if ($max_level <= CACHE_WEEK) {
174                                 $condition = ["`updated` < ? AND `expire_mode` = ?",
175                                                 Temporal::convert("now - 7 days"),
176                                                 CACHE_WEEK];
177                                 dba::delete('cache', $condition);
178                         }
179
180                         if ($max_level <= CACHE_DAY) {
181                                 $condition = ["`updated` < ? AND `expire_mode` = ?",
182                                                 Temporal::convert("now - 1 days"),
183                                                 CACHE_DAY];
184                                 dba::delete('cache', $condition);
185                         }
186                         Config::set("system", "cache_cleared_day", time());
187                 }
188
189                 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
190                         $condition = ["`updated` < ? AND `expire_mode` = ?",
191                                         Temporal::convert("now - 1 hours"),
192                                         CACHE_HOUR];
193                         dba::delete('cache', $condition);
194
195                         Config::set("system", "cache_cleared_hour", time());
196                 }
197
198                 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
199                         $condition = ["`updated` < ? AND `expire_mode` = ?",
200                                         Temporal::convert("now - 30 minutes"),
201                                         CACHE_HALF_HOUR];
202                         dba::delete('cache', $condition);
203
204                         Config::set("system", "cache_cleared_half_hour", time());
205                 }
206
207                 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
208                         $condition = ["`updated` < ? AND `expire_mode` = ?",
209                                         Temporal::convert("now - 15 minutes"),
210                                         CACHE_QUARTER_HOUR];
211                         dba::delete('cache', $condition);
212
213                         Config::set("system", "cache_cleared_quarter_hour", time());
214                 }
215
216                 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
217                         $condition = ["`updated` < ? AND `expire_mode` = ?",
218                                         Temporal::convert("now - 5 minutes"),
219                                         CACHE_FIVE_MINUTES];
220                         dba::delete('cache', $condition);
221
222                         Config::set("system", "cache_cleared_five_minute", time());
223                 }
224
225                 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
226                         $condition = ["`updated` < ? AND `expire_mode` = ?",
227                                         Temporal::convert("now - 1 minutes"),
228                                         CACHE_MINUTE];
229                         dba::delete('cache', $condition);
230
231                         Config::set("system", "cache_cleared_minute", time());
232                 }
233         }
234 }