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 object|boolean The memcache object - or "false" if not successful
25 public static function memcache()
27 if (!function_exists('memcache_connect')) {
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:
86 * @brief Fetch cached data according to the key
88 * @param string $key The key to the cached data
90 * @return mixed Cached $value or "null" if not found
92 public static function get($key)
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);
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) {
110 // Frequently clear cache
113 $cache = dba::selectFirst('cache', ['v'], ['k' => $key]);
115 if (DBM::is_result($cache)) {
116 $cached = $cache['v'];
117 $value = @unserialize($cached);
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) {
131 * @brief Put data in the cache according to the key
133 * The input $value can have multiple formats.
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
141 public static function set($key, $value, $duration = CACHE_MONTH)
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));
150 $fields = ['v' => serialize($value), 'expire_mode' => $duration, 'updated' => DateTimeFormat::utcNow()];
151 $condition = ['k' => $key];
152 dba::update('cache', $fields, $condition, true);
156 * @brief Remove outdated data from the cache
158 * @param integer $max_level The maximum cache level that is to be cleared
162 public static function clear($max_level = CACHE_MONTH)
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 DateTimeFormat::utc("now - 30 days"),
170 dba::delete('cache', $condition);
173 if ($max_level <= CACHE_WEEK) {
174 $condition = ["`updated` < ? AND `expire_mode` = ?",
175 DateTimeFormat::utc("now - 7 days"),
177 dba::delete('cache', $condition);
180 if ($max_level <= CACHE_DAY) {
181 $condition = ["`updated` < ? AND `expire_mode` = ?",
182 DateTimeFormat::utc("now - 1 days"),
184 dba::delete('cache', $condition);
186 Config::set("system", "cache_cleared_day", time());
189 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
190 $condition = ["`updated` < ? AND `expire_mode` = ?",
191 DateTimeFormat::utc("now - 1 hours"),
193 dba::delete('cache', $condition);
195 Config::set("system", "cache_cleared_hour", time());
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 DateTimeFormat::utc("now - 30 minutes"),
202 dba::delete('cache', $condition);
204 Config::set("system", "cache_cleared_half_hour", time());
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 DateTimeFormat::utc("now - 15 minutes"),
211 dba::delete('cache', $condition);
213 Config::set("system", "cache_cleared_quarter_hour", time());
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 DateTimeFormat::utc("now - 5 minutes"),
220 dba::delete('cache', $condition);
222 Config::set("system", "cache_cleared_five_minute", time());
225 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
226 $condition = ["`updated` < ? AND `expire_mode` = ?",
227 DateTimeFormat::utc("now - 1 minutes"),
229 dba::delete('cache', $condition);
231 Config::set("system", "cache_cleared_minute", time());