3 * @file include/cache.php
5 * @brief Class for storing data for a short time
8 use Friendica\Core\Config;
9 use Friendica\Core\PConfig;
10 use Friendica\Database\DBM;
14 * @brief Check for memcache and open a connection if configured
16 * @return object|boolean The memcache object - or "false" if not successful
18 public static function memcache() {
19 if (!function_exists('memcache_connect')) {
23 if (!Config::get('system', 'memcache')) {
27 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
28 $memcache_port = Config::get('system', 'memcache_port', 11211);
30 $memcache = new Memcache;
32 if (!$memcache->connect($memcache_host, $memcache_port)) {
40 * @brief Return the duration for a given cache level
42 * @param integer $level Cache level
44 * @return integer The cache duration in seconds
46 private static function duration($level) {
63 case CACHE_QUARTER_HOUR;
66 case CACHE_FIVE_MINUTES;
77 * @brief Fetch cached data according to the key
79 * @param string $key The key to the cached data
81 * @return mixed Cached $value or "null" if not found
83 public static function get($key) {
85 $memcache = self::memcache();
86 if (is_object($memcache)) {
87 // We fetch with the hostname as key to avoid problems with other applications
88 $cached = $memcache->get(get_app()->get_hostname().":".$key);
89 $value = @unserialize($cached);
91 // Only return a value if the serialized value is valid.
92 // We also check if the db entry is a serialized
93 // boolean 'false' value (which we want to return).
94 if ($cached === serialize(false) || $value !== false) {
101 // Frequently clear cache
102 self::clear($duration);
104 $r = q("SELECT `v` FROM `cache` WHERE `k`='%s' LIMIT 1",
108 if (DBM::is_result($r)) {
109 $cached = $r[0]['v'];
110 $value = @unserialize($cached);
112 // Only return a value if the serialized value is valid.
113 // We also check if the db entry is a serialized
114 // boolean 'false' value (which we want to return).
115 if ($cached === serialize(false) || $value !== false) {
124 * @brief Put data in the cache according to the key
126 * The input $value can have multiple formats.
128 * @param string $key The key to the cached data
129 * @param mixed $valie The value that is about to be stored
130 * @param integer $duration The cache lifespan
132 public static function set($key, $value, $duration = CACHE_MONTH) {
134 // Do we have an installed memcache? Use it instead.
135 $memcache = self::memcache();
136 if (is_object($memcache)) {
137 // We store with the hostname as key to avoid problems with other applications
138 $memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration));
142 /// @todo store the cache data in the same way like the config data
143 q("REPLACE INTO `cache` (`k`,`v`,`expire_mode`,`updated`) VALUES ('%s','%s',%d,'%s')",
145 dbesc(serialize($value)),
147 dbesc(datetime_convert()));
151 * @brief Remove outdated data from the cache
153 * @param integer $maxlevel The maximum cache level that is to be cleared
155 public static function clear($max_level = CACHE_MONTH) {
157 // Clear long lasting cache entries only once a day
158 if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
159 if ($max_level == CACHE_MONTH) {
160 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
161 dbesc(datetime_convert('UTC','UTC',"now - 30 days")), intval(CACHE_MONTH));
164 if ($max_level <= CACHE_WEEK) {
165 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
166 dbesc(datetime_convert('UTC','UTC',"now - 7 days")), intval(CACHE_WEEK));
169 if ($max_level <= CACHE_DAY) {
170 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
171 dbesc(datetime_convert('UTC','UTC',"now - 1 days")), intval(CACHE_DAY));
173 Config::set("system", "cache_cleared_day", time());
176 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
177 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
178 dbesc(datetime_convert('UTC','UTC',"now - 1 hours")), intval(CACHE_HOUR));
180 Config::set("system", "cache_cleared_hour", time());
183 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
184 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
185 dbesc(datetime_convert('UTC','UTC',"now - 30 minutes")), intval(CACHE_HALF_HOUR));
187 Config::set("system", "cache_cleared_half_hour", time());
190 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
191 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
192 dbesc(datetime_convert('UTC','UTC',"now - 15 minutes")), intval(CACHE_QUARTER_HOUR));
194 Config::set("system", "cache_cleared_quarter_hour", time());
197 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
198 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
199 dbesc(datetime_convert('UTC','UTC',"now - 5 minutes")), intval(CACHE_FIVE_MINUTES));
201 Config::set("system", "cache_cleared_five_minute", time());
204 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
205 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
206 dbesc(datetime_convert('UTC','UTC',"now - 1 minutes")), intval(CACHE_MINUTE));
208 Config::set("system", "cache_cleared_minute", time());