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 require_once 'include/dba.php';
15 * @brief Class for storing data for a short time
20 * @brief Check for memcache and open a connection if configured
22 * @return object|boolean The memcache object - or "false" if not successful
24 public static function memcache()
26 if (!function_exists('memcache_connect')) {
30 if (!Config::get('system', 'memcache')) {
34 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
35 $memcache_port = Config::get('system', 'memcache_port', 11211);
37 $memcache = new \Memcache;
39 if (!$memcache->connect($memcache_host, $memcache_port)) {
47 * @brief Return the duration for a given cache level
49 * @param integer $level Cache level
51 * @return integer The cache duration in seconds
53 private static function duration($level)
71 case CACHE_QUARTER_HOUR:
74 case CACHE_FIVE_MINUTES:
85 * @brief Fetch cached data according to the key
87 * @param string $key The key to the cached data
89 * @return mixed Cached $value or "null" if not found
91 public static function get($key)
93 $memcache = self::memcache();
94 if (is_object($memcache)) {
95 // We fetch with the hostname as key to avoid problems with other applications
96 $cached = $memcache->get(get_app()->get_hostname().":".$key);
97 $value = @unserialize($cached);
99 // Only return a value if the serialized value is valid.
100 // We also check if the db entry is a serialized
101 // boolean 'false' value (which we want to return).
102 if ($cached === serialize(false) || $value !== false) {
109 // Frequently clear cache
112 $cache = dba::selectFirst('cache', ['v'], ['k' => $key]);
114 if (DBM::is_result($cache)) {
115 $cached = $cache['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));
149 $fields = ['v' => serialize($value), 'expire_mode' => $duration, 'updated' => datetime_convert()];
150 $condition = ['k' => $key];
151 dba::update('cache', $fields, $condition, true);
155 * @brief Remove outdated data from the cache
157 * @param integer $max_level The maximum cache level that is to be cleared
161 public static function clear($max_level = CACHE_MONTH)
163 // Clear long lasting cache entries only once a day
164 if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
165 if ($max_level == CACHE_MONTH) {
166 $condition = ["`updated` < ? AND `expire_mode` = ?",
167 datetime_convert('UTC', 'UTC', "now - 30 days"),
169 dba::delete('cache', $condition);
172 if ($max_level <= CACHE_WEEK) {
173 $condition = ["`updated` < ? AND `expire_mode` = ?",
174 datetime_convert('UTC', 'UTC', "now - 7 days"),
176 dba::delete('cache', $condition);
179 if ($max_level <= CACHE_DAY) {
180 $condition = ["`updated` < ? AND `expire_mode` = ?",
181 datetime_convert('UTC', 'UTC', "now - 1 days"),
183 dba::delete('cache', $condition);
185 Config::set("system", "cache_cleared_day", time());
188 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
189 $condition = ["`updated` < ? AND `expire_mode` = ?",
190 datetime_convert('UTC', 'UTC', "now - 1 hours"),
192 dba::delete('cache', $condition);
194 Config::set("system", "cache_cleared_hour", time());
197 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
198 $condition = ["`updated` < ? AND `expire_mode` = ?",
199 datetime_convert('UTC', 'UTC', "now - 30 minutes"),
201 dba::delete('cache', $condition);
203 Config::set("system", "cache_cleared_half_hour", time());
206 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
207 $condition = ["`updated` < ? AND `expire_mode` = ?",
208 datetime_convert('UTC', 'UTC', "now - 15 minutes"),
210 dba::delete('cache', $condition);
212 Config::set("system", "cache_cleared_quarter_hour", time());
215 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
216 $condition = ["`updated` < ? AND `expire_mode` = ?",
217 datetime_convert('UTC', 'UTC', "now - 5 minutes"),
219 dba::delete('cache', $condition);
221 Config::set("system", "cache_cleared_five_minute", time());
224 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
225 $condition = ["`updated` < ? AND `expire_mode` = ?",
226 datetime_convert('UTC', 'UTC', "now - 1 minutes"),
228 dba::delete('cache', $condition);
230 Config::set("system", "cache_cleared_minute", time());