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;
13 * @brief Check for memcache and open a connection if configured
15 * @return object|boolean The memcache object - or "false" if not successful
17 public static function memcache() {
18 if (!function_exists('memcache_connect')) {
22 if (!Config::get('system', 'memcache')) {
26 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
27 $memcache_port = Config::get('system', 'memcache_port', 11211);
29 $memcache = new Memcache;
31 if (!$memcache->connect($memcache_host, $memcache_port)) {
39 * @brief Return the duration for a given cache level
41 * @param integer $level Cache level
43 * @return integer The cache duration in seconds
45 private function duration($level) {
62 case CACHE_QUARTER_HOUR;
65 case CACHE_FIVE_MINUTES;
76 * @brief Fetch cached data according to the key
78 * @param string $key The key to the cached data
80 * @return mixed Cached $value or "null" if not found
82 public static function get($key) {
84 $memcache = self::memcache();
85 if (is_object($memcache)) {
86 // We fetch with the hostname as key to avoid problems with other applications
87 $value = $memcache->get(get_app()->get_hostname().":".$key);
88 if (!is_bool($value)) {
89 return unserialize($value);
95 // Frequently clear cache
96 self::clear($duration);
98 $r = q("SELECT `v` FROM `cache` WHERE `k`='%s' LIMIT 1",
102 if (dbm::is_result($r)) {
103 return unserialize($r[0]['v']);
110 * @brief Put data in the cache according to the key
112 * The input $value can have multiple formats.
114 * @param string $key The key to the cached data
115 * @param mixed $valie The value that is about to be stored
116 * @param integer $duration The cache lifespan
118 public static function set($key, $value, $duration = CACHE_MONTH) {
120 // Do we have an installed memcache? Use it instead.
121 $memcache = self::memcache();
122 if (is_object($memcache)) {
123 // We store with the hostname as key to avoid problems with other applications
124 $memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration));
128 /// @todo store the cache data in the same way like the config data
129 q("REPLACE INTO `cache` (`k`,`v`,`expire_mode`,`updated`) VALUES ('%s','%s',%d,'%s')",
131 dbesc(serialize($value)),
133 dbesc(datetime_convert()));
137 * @brief Remove outdated data from the cache
139 * @param integer $maxlevel The maximum cache level that is to be cleared
141 public static function clear($max_level = CACHE_MONTH) {
143 // Clear long lasting cache entries only once a day
144 if (get_config("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
145 if ($max_level == CACHE_MONTH) {
146 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
147 dbesc(datetime_convert('UTC','UTC',"now - 30 days")), intval(CACHE_MONTH));
150 if ($max_level <= CACHE_WEEK) {
151 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
152 dbesc(datetime_convert('UTC','UTC',"now - 7 days")), intval(CACHE_WEEK));
155 if ($max_level <= CACHE_DAY) {
156 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
157 dbesc(datetime_convert('UTC','UTC',"now - 1 days")), intval(CACHE_DAY));
159 set_config("system", "cache_cleared_day", time());
162 if (($max_level <= CACHE_HOUR) AND (get_config("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
163 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
164 dbesc(datetime_convert('UTC','UTC',"now - 1 hours")), intval(CACHE_HOUR));
166 set_config("system", "cache_cleared_hour", time());
169 if (($max_level <= CACHE_HALF_HOUR) AND (get_config("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
170 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
171 dbesc(datetime_convert('UTC','UTC',"now - 30 minutes")), intval(CACHE_HALF_HOUR));
173 set_config("system", "cache_cleared_half_hour", time());
176 if (($max_level <= CACHE_QUARTER_HOUR) AND (get_config("system", "cache_cleared_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
177 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
178 dbesc(datetime_convert('UTC','UTC',"now - 15 minutes")), intval(CACHE_QUARTER_HOUR));
180 set_config("system", "cache_cleared_quarter_hour", time());
183 if (($max_level <= CACHE_FIVE_MINUTES) AND (get_config("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
184 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
185 dbesc(datetime_convert('UTC','UTC',"now - 5 minutes")), intval(CACHE_FIVE_MINUTES));
187 set_config("system", "cache_cleared_five_minute", time());
190 if (($max_level <= CACHE_MINUTE) AND (get_config("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
191 q("DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d",
192 dbesc(datetime_convert('UTC','UTC',"now - 1 minutes")), intval(CACHE_MINUTE));
194 set_config("system", "cache_cleared_minute", time());