Cache.php is now using dba methods

This commit is contained in:
Michael 2017-11-21 23:03:33 +00:00
parent d99a7efd90
commit d982c68d75
1 changed files with 38 additions and 54 deletions

View File

@ -7,6 +7,7 @@ namespace Friendica\Core;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Database\DBM; use Friendica\Database\DBM;
use dba;
/** /**
* @brief Class for storing data for a short time * @brief Class for storing data for a short time
@ -106,13 +107,10 @@ class Cache
// Frequently clear cache // Frequently clear cache
self::clear($duration); self::clear($duration);
$r = q( $r = dba::select('cache', array('v'), array('k' => $key), array('limit' => 1));
"SELECT `v` FROM `cache` WHERE `k`='%s' LIMIT 1",
dbesc($key)
);
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
$cached = $r[0]['v']; $cached = $r['v'];
$value = @unserialize($cached); $value = @unserialize($cached);
// Only return a value if the serialized value is valid. // Only return a value if the serialized value is valid.
@ -146,15 +144,9 @@ class Cache
$memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration)); $memcache->set(get_app()->get_hostname().":".$key, serialize($value), MEMCACHE_COMPRESSED, self::duration($duration));
return; return;
} }
$fields = array('v' => serialize($value), 'expire_mode' => $duration, 'updated' => datetime_convert());
/// @todo store the cache data in the same way like the config data $condition = array('k' => $key);
q( dba::update('cache', $fields, $condition, true);
"REPLACE INTO `cache` (`k`,`v`,`expire_mode`,`updated`) VALUES ('%s','%s',%d,'%s')",
dbesc($key),
dbesc(serialize($value)),
intval($duration),
dbesc(datetime_convert())
);
} }
/** /**
@ -169,77 +161,69 @@ class Cache
// Clear long lasting cache entries only once a day // Clear long lasting cache entries only once a day
if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) { if (Config::get("system", "cache_cleared_day") < time() - self::duration(CACHE_DAY)) {
if ($max_level == CACHE_MONTH) { if ($max_level == CACHE_MONTH) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 30 days"),
dbesc(datetime_convert('UTC', 'UTC', "now - 30 days")), CACHE_MONTH);
intval(CACHE_MONTH) dba::delete('cache', $condition);
);
} }
if ($max_level <= CACHE_WEEK) { if ($max_level <= CACHE_WEEK) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 7 days"),
dbesc(datetime_convert('UTC', 'UTC', "now - 7 days")), CACHE_WEEK);
intval(CACHE_WEEK) dba::delete('cache', $condition);
);
} }
if ($max_level <= CACHE_DAY) { if ($max_level <= CACHE_DAY) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 1 days"),
dbesc(datetime_convert('UTC', 'UTC', "now - 1 days")), CACHE_DAY);
intval(CACHE_DAY) dba::delete('cache', $condition);
);
} }
Config::set("system", "cache_cleared_day", time()); Config::set("system", "cache_cleared_day", time());
} }
if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) { if (($max_level <= CACHE_HOUR) && (Config::get("system", "cache_cleared_hour")) < time() - self::duration(CACHE_HOUR)) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 1 hours"),
dbesc(datetime_convert('UTC', 'UTC', "now - 1 hours")), CACHE_HOUR);
intval(CACHE_HOUR) dba::delete('cache', $condition);
);
Config::set("system", "cache_cleared_hour", time()); Config::set("system", "cache_cleared_hour", time());
} }
if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) { if (($max_level <= CACHE_HALF_HOUR) && (Config::get("system", "cache_cleared_half_hour")) < time() - self::duration(CACHE_HALF_HOUR)) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 30 minutes"),
dbesc(datetime_convert('UTC', 'UTC', "now - 30 minutes")), CACHE_HALF_HOUR);
intval(CACHE_HALF_HOUR) dba::delete('cache', $condition);
);
Config::set("system", "cache_cleared_half_hour", time()); Config::set("system", "cache_cleared_half_hour", time());
} }
if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) { if (($max_level <= CACHE_QUARTER_HOUR) && (Config::get("system", "cache_cleared_quarter_hour")) < time() - self::duration(CACHE_QUARTER_HOUR)) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 15 minutes"),
dbesc(datetime_convert('UTC', 'UTC', "now - 15 minutes")), CACHE_QUARTER_HOUR);
intval(CACHE_QUARTER_HOUR) dba::delete('cache', $condition);
);
Config::set("system", "cache_cleared_quarter_hour", time()); Config::set("system", "cache_cleared_quarter_hour", time());
} }
if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) { if (($max_level <= CACHE_FIVE_MINUTES) && (Config::get("system", "cache_cleared_five_minute")) < time() - self::duration(CACHE_FIVE_MINUTES)) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 5 minutes"),
dbesc(datetime_convert('UTC', 'UTC', "now - 5 minutes")), CACHE_FIVE_MINUTES);
intval(CACHE_FIVE_MINUTES) dba::delete('cache', $condition);
);
Config::set("system", "cache_cleared_five_minute", time()); Config::set("system", "cache_cleared_five_minute", time());
} }
if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) { if (($max_level <= CACHE_MINUTE) && (Config::get("system", "cache_cleared_minute")) < time() - self::duration(CACHE_MINUTE)) {
q( $condition = array("`updated` < ? AND `expire_mode` = ?",
"DELETE FROM `cache` WHERE `updated` < '%s' AND `expire_mode` = %d", datetime_convert('UTC', 'UTC', "now - 1 minutes"),
dbesc(datetime_convert('UTC', 'UTC', "now - 1 minutes")), CACHE_MINUTE);
intval(CACHE_MINUTE) dba::delete('cache', $condition);
);
Config::set("system", "cache_cleared_minute", time()); Config::set("system", "cache_cleared_minute", time());
} }