3 * @file src/Core/Cache.php
5 namespace Friendica\Core;
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Util\DateTimeFormat;
13 require_once 'include/dba.php';
16 * @brief Class for storing data for a short time
21 * @brief Check for Memcache and open a connection if configured
23 * @return Memcache|boolean The Memcache object - or "false" if not successful
25 public static function memcache()
27 if (!class_exists('Memcache', false)) {
31 if (!Config::get('system', 'memcache')) {
35 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
36 $memcache_port = Config::get('system', 'memcache_port', 11211);
38 $memcache = new Memcache();
40 if (!$memcache->connect($memcache_host, $memcache_port)) {
48 * @brief Return the duration for a given cache level
50 * @param integer $level Cache level
52 * @return integer The cache duration in seconds
54 private static function duration($level)
72 case CACHE_QUARTER_HOUR:
75 case CACHE_FIVE_MINUTES:
87 * @brief Fetch cached data according to the key
89 * @param string $key The key to the cached data
91 * @return mixed Cached $value or "null" if not found
93 public static function get($key)
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);
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) {
111 // Frequently clear cache
114 $cache = dba::selectFirst('cache', ['v'], ['k' => $key]);
116 if (DBM::is_result($cache)) {
117 $cached = $cache['v'];
118 $value = @unserialize($cached);
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) {
132 * @brief Put data in the cache according to the key
134 * The input $value can have multiple formats.
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
142 public static function set($key, $value, $duration = CACHE_MONTH)
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));
151 $fields = ['v' => serialize($value), 'expire_mode' => $duration, 'updated' => DateTimeFormat::utcNow()];
152 $condition = ['k' => $key];
153 dba::update('cache', $fields, $condition, true);
157 * @brief Remove outdated data from the cache
159 * @param integer $max_level The maximum cache level that is to be cleared
163 public static function clear($max_level = CACHE_MONTH)
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"),
171 dba::delete('cache', $condition);
174 if ($max_level <= CACHE_WEEK) {
175 $condition = ["`updated` < ? AND `expire_mode` = ?",
176 DateTimeFormat::utc("now - 7 days"),
178 dba::delete('cache', $condition);
181 if ($max_level <= CACHE_DAY) {
182 $condition = ["`updated` < ? AND `expire_mode` = ?",
183 DateTimeFormat::utc("now - 1 days"),
185 dba::delete('cache', $condition);
187 Config::set("system", "cache_cleared_day", time());
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"),
194 dba::delete('cache', $condition);
196 Config::set("system", "cache_cleared_hour", time());
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"),
203 dba::delete('cache', $condition);
205 Config::set("system", "cache_cleared_half_hour", time());
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"),
212 dba::delete('cache', $condition);
214 Config::set("system", "cache_cleared_quarter_hour", time());
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"),
221 dba::delete('cache', $condition);
223 Config::set("system", "cache_cleared_five_minute", time());
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"),
230 dba::delete('cache', $condition);
232 Config::set("system", "cache_cleared_minute", time());