3 * @file src/Core/Cache.php
5 namespace Friendica\Core;
7 use Friendica\Core\Config;
8 use Friendica\Core\PConfig;
9 use Friendica\Database\DBM;
12 * @brief Class for storing data for a short time
17 * @brief Check for memcache and open a connection if configured
19 * @return object|boolean The memcache object - or "false" if not successful
21 public static function memcache()
23 if (!function_exists('memcache_connect')) {
27 if (!Config::get('system', 'memcache')) {
31 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
32 $memcache_port = Config::get('system', 'memcache_port', 11211);
34 $memcache = new Memcache;
36 if (!$memcache->connect($memcache_host, $memcache_port)) {
44 * @brief Return the duration for a given cache level
46 * @param integer $level Cache level
48 * @return integer The cache duration in seconds
50 private static function duration($level)
68 case CACHE_QUARTER_HOUR:
71 case CACHE_FIVE_MINUTES:
82 * @brief Fetch cached data according to the key
84 * @param string $key The key to the cached data
86 * @return mixed Cached $value or "null" if not found
88 public static function get($key)
90 $memcache = self::memcache();
91 if (is_object($memcache)) {
92 // We fetch with the hostname as key to avoid problems with other applications
93 $cached = $memcache->get(get_app()->get_hostname().":".$key);
94 $value = @unserialize($cached);
96 // Only return a value if the serialized value is valid.
97 // We also check if the db entry is a serialized
98 // boolean 'false' value (which we want to return).
99 if ($cached === serialize(false) || $value !== false) {
106 // Frequently clear cache
107 self::clear($duration);
110 "SELECT `v` FROM `cache` WHERE `k`='%s' LIMIT 1",
114 if (DBM::is_result($r)) {
115 $cached = $r[0]['v'];
116 $value = @unserialize($cached);
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) {
130 * @brief Put data in the cache according to the key
132 * The input $value can have multiple formats.
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
138 public static function set($key, $value, $duration = CACHE_MONTH)
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));
148 /// @todo store the cache data in the same way like the config data
150 "REPLACE INTO `cache` (`k`,`v`,`expire_mode`,`updated`) VALUES ('%s','%s',%d,'%s')",
152 dbesc(serialize($value)),
154 dbesc(datetime_convert())
159 * @brief Remove outdated data from the cache
161 * @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) {
169 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
170 dbesc(datetime_convert('UTC', 'UTC', "now - 30 days")),
175 if ($max_level <= CACHE_WEEK) {
177 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
178 dbesc(datetime_convert('UTC', 'UTC', "now - 7 days")),
183 if ($max_level <= CACHE_DAY) {
185 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
186 dbesc(datetime_convert('UTC', 'UTC', "now - 1 days")),
190 Config::set("system", "cache_cleared_day", time());
193 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
195 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
196 dbesc(datetime_convert('UTC', 'UTC', "now - 1 hours")),
200 Config::set("system", "cache_cleared_hour", time());
203 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
205 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
206 dbesc(datetime_convert('UTC', 'UTC', "now - 30 minutes")),
207 intval(CACHE_HALF_HOUR)
210 Config::set("system", "cache_cleared_half_hour", time());
213 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
215 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
216 dbesc(datetime_convert('UTC', 'UTC', "now - 15 minutes")),
217 intval(CACHE_QUARTER_HOUR)
220 Config::set("system", "cache_cleared_quarter_hour", time());
223 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
225 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
226 dbesc(datetime_convert('UTC', 'UTC', "now - 5 minutes")),
227 intval(CACHE_FIVE_MINUTES)
230 Config::set("system", "cache_cleared_five_minute", time());
233 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
235 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
236 dbesc(datetime_convert('UTC', 'UTC', "now - 1 minutes")),
240 Config::set("system", "cache_cleared_minute", time());