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;
13 * @brief Class for storing data for a short time
18 * @brief Check for memcache and open a connection if configured
20 * @return object|boolean The memcache object - or "false" if not successful
22 public static function memcache()
24 if (!function_exists('memcache_connect')) {
28 if (!Config::get('system', 'memcache')) {
32 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
33 $memcache_port = Config::get('system', 'memcache_port', 11211);
35 $memcache = new Memcache;
37 if (!$memcache->connect($memcache_host, $memcache_port)) {
45 * @brief Return the duration for a given cache level
47 * @param integer $level Cache level
49 * @return integer The cache duration in seconds
51 private static function duration($level)
69 case CACHE_QUARTER_HOUR:
72 case CACHE_FIVE_MINUTES:
83 * @brief Fetch cached data according to the key
85 * @param string $key The key to the cached data
87 * @return mixed Cached $value or "null" if not found
89 public static function get($key)
91 $memcache = self::memcache();
92 if (is_object($memcache)) {
93 // We fetch with the hostname as key to avoid problems with other applications
94 $cached = $memcache->get(get_app()->get_hostname().":".$key);
95 $value = @unserialize($cached);
97 // Only return a value if the serialized value is valid.
98 // We also check if the db entry is a serialized
99 // boolean 'false' value (which we want to return).
100 if ($cached === serialize(false) || $value !== false) {
107 // Frequently clear cache
108 self::clear($duration);
110 $r = dba::select('cache', array('v'), array('k' => $key), array('limit' => 1));
112 if (DBM::is_result($r)) {
114 $value = @unserialize($cached);
116 // Only return a value if the serialized value is valid.
117 // We also check if the db entry is a serialized
118 // boolean 'false' value (which we want to return).
119 if ($cached === serialize(false) || $value !== false) {
128 * @brief Put data in the cache according to the key
130 * The input $value can have multiple formats.
132 * @param string $key The key to the cached data
133 * @param mixed $value The value that is about to be stored
134 * @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));
147 $fields = array('v' => serialize($value), 'expire_mode' => $duration, 'updated' => datetime_convert());
148 $condition = array('k' => $key);
149 dba::update('cache', $fields, $condition, true);
153 * @brief Remove outdated data from the cache
155 * @param integer $max_level The maximum cache level that is to be cleared
159 public static function clear($max_level = CACHE_MONTH)
161 // Clear long lasting cache entries only once a day
162 if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
163 if ($max_level == CACHE_MONTH) {
164 $condition = array("`updated` < ? AND `expire_mode` = ?",
165 datetime_convert('UTC', 'UTC', "now - 30 days"),
167 dba::delete('cache', $condition);
170 if ($max_level <= CACHE_WEEK) {
171 $condition = array("`updated` < ? AND `expire_mode` = ?",
172 datetime_convert('UTC', 'UTC', "now - 7 days"),
174 dba::delete('cache', $condition);
177 if ($max_level <= CACHE_DAY) {
178 $condition = array("`updated` < ? AND `expire_mode` = ?",
179 datetime_convert('UTC', 'UTC', "now - 1 days"),
181 dba::delete('cache', $condition);
183 Config::set("system", "cache_cleared_day", time());
186 if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
187 $condition = array("`updated` < ? AND `expire_mode` = ?",
188 datetime_convert('UTC', 'UTC', "now - 1 hours"),
190 dba::delete('cache', $condition);
192 Config::set("system", "cache_cleared_hour", time());
195 if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
196 $condition = array("`updated` < ? AND `expire_mode` = ?",
197 datetime_convert('UTC', 'UTC', "now - 30 minutes"),
199 dba::delete('cache', $condition);
201 Config::set("system", "cache_cleared_half_hour", time());
204 if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
205 $condition = array("`updated` < ? AND `expire_mode` = ?",
206 datetime_convert('UTC', 'UTC', "now - 15 minutes"),
208 dba::delete('cache', $condition);
210 Config::set("system", "cache_cleared_quarter_hour", time());
213 if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
214 $condition = array("`updated` < ? AND `expire_mode` = ?",
215 datetime_convert('UTC', 'UTC', "now - 5 minutes"),
217 dba::delete('cache', $condition);
219 Config::set("system", "cache_cleared_five_minute", time());
222 if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
223 $condition = array("`updated` < ? AND `expire_mode` = ?",
224 datetime_convert('UTC', 'UTC', "now - 1 minutes"),
226 dba::delete('cache', $condition);
228 Config::set("system", "cache_cleared_minute", time());