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