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