]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache.php
Fewer Defaults
[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\DateTimeFormat;
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 Memcache|boolean The Memcache object - or "false" if not successful
24          */
25         public static function memcache()
26         {
27                 if (!class_exists('Memcache', false)) {
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                         default:
80                                 $seconds = 60;
81                                 break;
82                 }
83                 return $seconds;
84         }
85
86         /**
87          * @brief Fetch cached data according to the key
88          *
89          * @param string $key The key to the cached data
90          *
91          * @return mixed Cached $value or "null" if not found
92          */
93         public static function get($key)
94         {
95                 $memcache = self::memcache();
96                 if (is_object($memcache)) {
97                         // We fetch with the hostname as key to avoid problems with other applications
98                         $cached = $memcache->get(get_app()->get_hostname().":".$key);
99                         $value = @unserialize($cached);
100
101                         // Only return a value if the serialized value is valid.
102                         // We also check if the db entry is a serialized
103                         // boolean 'false' value (which we want to return).
104                         if ($cached === serialize(false) || $value !== false) {
105                                 return $value;
106                         }
107
108                         return null;
109                 }
110
111                 // Frequently clear cache
112                 self::clear();
113
114                 $cache = dba::selectFirst('cache', ['v'], ['k' => $key]);
115
116                 if (DBM::is_result($cache)) {
117                         $cached = $cache['v'];
118                         $value = @unserialize($cached);
119
120                         // Only return a value if the serialized value is valid.
121                         // We also check if the db entry is a serialized
122                         // boolean 'false' value (which we want to return).
123                         if ($cached === serialize(false) || $value !== false) {
124                                 return $value;
125                         }
126                 }
127
128                 return null;
129         }
130
131         /**
132          * @brief Put data in the cache according to the key
133          *
134          * The input $value can have multiple formats.
135          *
136          * @param string  $key      The key to the cached data
137          * @param mixed   $value    The value that is about to be stored
138          * @param integer $duration The cache lifespan
139          *
140          * @return void
141          */
142         public static function set($key, $value, $duration = CACHE_MONTH)
143         {
144                 // Do we have an installed memcache? Use it instead.
145                 $memcache = self::memcache();
146                 if (is_object($memcache)) {
147                         // We store with the hostname as key to avoid problems with other applications
148                         $memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration));
149                         return;
150                 }
151                 $fields = ['v' => serialize($value), 'expire_mode' => $duration, 'updated' => DateTimeFormat::utcNow()];
152                 $condition = ['k' => $key];
153                 dba::update('cache', $fields, $condition, true);
154         }
155
156         /**
157          * @brief Remove outdated data from the cache
158          *
159          * @param integer $max_level The maximum cache level that is to be cleared
160          *
161          * @return void
162          */
163         public static function clear($max_level = CACHE_MONTH)
164         {
165                 // Clear long lasting cache entries only once a day
166                 if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
167                         if ($max_level == CACHE_MONTH) {
168                                 $condition = ["`updated` < ? AND `expire_mode` = ?",
169                                                 DateTimeFormat::utc("now - 30 days"),
170                                                 CACHE_MONTH];
171                                 dba::delete('cache', $condition);
172                         }
173
174                         if ($max_level <= CACHE_WEEK) {
175                                 $condition = ["`updated` < ? AND `expire_mode` = ?",
176                                                 DateTimeFormat::utc("now - 7 days"),
177                                                 CACHE_WEEK];
178                                 dba::delete('cache', $condition);
179                         }
180
181                         if ($max_level <= CACHE_DAY) {
182                                 $condition = ["`updated` < ? AND `expire_mode` = ?",
183                                                 DateTimeFormat::utc("now - 1 days"),
184                                                 CACHE_DAY];
185                                 dba::delete('cache', $condition);
186                         }
187                         Config::set("system", "cache_cleared_day", time());
188                 }
189
190                 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
191                         $condition = ["`updated` < ? AND `expire_mode` = ?",
192                                         DateTimeFormat::utc("now - 1 hours"),
193                                         CACHE_HOUR];
194                         dba::delete('cache', $condition);
195
196                         Config::set("system", "cache_cleared_hour", time());
197                 }
198
199                 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
200                         $condition = ["`updated` < ? AND `expire_mode` = ?",
201                                         DateTimeFormat::utc("now - 30 minutes"),
202                                         CACHE_HALF_HOUR];
203                         dba::delete('cache', $condition);
204
205                         Config::set("system", "cache_cleared_half_hour", time());
206                 }
207
208                 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
209                         $condition = ["`updated` < ? AND `expire_mode` = ?",
210                                         DateTimeFormat::utc("now - 15 minutes"),
211                                         CACHE_QUARTER_HOUR];
212                         dba::delete('cache', $condition);
213
214                         Config::set("system", "cache_cleared_quarter_hour", time());
215                 }
216
217                 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
218                         $condition = ["`updated` < ? AND `expire_mode` = ?",
219                                         DateTimeFormat::utc("now - 5 minutes"),
220                                         CACHE_FIVE_MINUTES];
221                         dba::delete('cache', $condition);
222
223                         Config::set("system", "cache_cleared_five_minute", time());
224                 }
225
226                 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
227                         $condition = ["`updated` < ? AND `expire_mode` = ?",
228                                         DateTimeFormat::utc("now - 1 minutes"),
229                                         CACHE_MINUTE];
230                         dba::delete('cache', $condition);
231
232                         Config::set("system", "cache_cleared_minute", time());
233                 }
234         }
235 }