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
140 public static function set($key, $value, $duration = CACHE_MONTH)
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));
150 /// @todo store the cache data in the same way like the config data
152 "REPLACE INTO `cache` (`k`,`v`,`expire_mode`,`updated`) VALUES ('%s','%s',%d,'%s')",
154 dbesc(serialize($value)),
156 dbesc(datetime_convert())
161 * @brief Remove outdated data from the cache
163 * @param integer $max_level The maximum cache level that is to be cleared
167 public static function clear($max_level = CACHE_MONTH)
169 // Clear long lasting cache entries only once a day
170 if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
171 if ($max_level == CACHE_MONTH) {
173 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
174 dbesc(datetime_convert('UTC', 'UTC', "now - 30 days")),
179 if ($max_level <= CACHE_WEEK) {
181 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
182 dbesc(datetime_convert('UTC', 'UTC', "now - 7 days")),
187 if ($max_level <= CACHE_DAY) {
189 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
190 dbesc(datetime_convert('UTC', 'UTC', "now - 1 days")),
194 Config::set("system", "cache_cleared_day", time());
197 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
199 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
200 dbesc(datetime_convert('UTC', 'UTC', "now - 1 hours")),
204 Config::set("system", "cache_cleared_hour", time());
207 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
209 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
210 dbesc(datetime_convert('UTC', 'UTC', "now - 30 minutes")),
211 intval(CACHE_HALF_HOUR)
214 Config::set("system", "cache_cleared_half_hour", time());
217 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
219 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
220 dbesc(datetime_convert('UTC', 'UTC', "now - 15 minutes")),
221 intval(CACHE_QUARTER_HOUR)
224 Config::set("system", "cache_cleared_quarter_hour", time());
227 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
229 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
230 dbesc(datetime_convert('UTC', 'UTC', "now - 5 minutes")),
231 intval(CACHE_FIVE_MINUTES)
234 Config::set("system", "cache_cleared_five_minute", time());
237 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
239 "DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
240 dbesc(datetime_convert('UTC', 'UTC', "now - 1 minutes")),
244 Config::set("system", "cache_cleared_minute", time());