Merge pull request #9036 from annando/clear-cache

Cache clearing rearrangements
This commit is contained in:
Hypolite Petovan 2020-08-19 15:43:24 -04:00 committed by GitHub
commit 69aae82f5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 31 deletions

View File

@ -21,7 +21,6 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Photo; use Friendica\Model\Photo;
@ -35,21 +34,12 @@ class ClearCache
public static function execute() public static function execute()
{ {
$a = DI::app(); $a = DI::app();
$last = DI::config()->get('system', 'cache_last_cleared');
if ($last) {
$next = $last + (3600); // Once per hour
$clear_cache = ($next <= time());
} else {
$clear_cache = true;
}
if (!$clear_cache) {
return;
}
// clear old cache // clear old cache
DI::cache()->clear(); DI::cache()->clear();
if (DI::config()->get('system', 'optimize_tables')) {
DBA::e("OPTIMIZE TABLE `cache`");
}
// clear old item cache files // clear old item cache files
clear_cache(); clear_cache();
@ -76,25 +66,14 @@ class ClearCache
// Delete the cached OEmbed entries that are older than three month // Delete the cached OEmbed entries that are older than three month
DBA::delete('oembed', ["`created` < NOW() - INTERVAL 3 MONTH"]); DBA::delete('oembed', ["`created` < NOW() - INTERVAL 3 MONTH"]);
if (DI::config()->get('system', 'optimize_tables')) {
DBA::e("OPTIMIZE TABLE `oembed`");
}
// Delete the cached "parse_url" entries that are older than three month // Delete the cached "parse_url" entries that are older than three month
DBA::delete('parsed_url', ["`created` < NOW() - INTERVAL 3 MONTH"]); DBA::delete('parsed_url', ["`created` < NOW() - INTERVAL 3 MONTH"]);
if (DI::config()->get('system', 'optimize_tables')) { if (DI::config()->get('system', 'optimize_tables')) {
Logger::info('Optimize start');
DBA::e("OPTIMIZE TABLE `auth_codes`");
DBA::e("OPTIMIZE TABLE `cache`");
DBA::e("OPTIMIZE TABLE `challenge`");
DBA::e("OPTIMIZE TABLE `locks`");
DBA::e("OPTIMIZE TABLE `oembed`");
DBA::e("OPTIMIZE TABLE `parsed_url`"); DBA::e("OPTIMIZE TABLE `parsed_url`");
DBA::e("OPTIMIZE TABLE `profile_check`");
DBA::e("OPTIMIZE TABLE `session`");
DBA::e("OPTIMIZE TABLE `tokens`");
DBA::e("OPTIMIZE TABLE `process`");
Logger::info('Optimize finished');
} }
DI::config()->set('system', 'cache_last_cleared', time());
} }
} }

View File

@ -63,9 +63,6 @@ class Cron
// Call possible post update functions // Call possible post update functions
Worker::add(PRIORITY_LOW, 'PostUpdate'); Worker::add(PRIORITY_LOW, 'PostUpdate');
// Clear cache entries
Worker::add(PRIORITY_LOW, 'ClearCache');
// Repair entries in the database // Repair entries in the database
Worker::add(PRIORITY_LOW, 'RepairDatabase'); Worker::add(PRIORITY_LOW, 'RepairDatabase');
@ -94,6 +91,10 @@ class Cron
self::checkdeletedContacts(); self::checkdeletedContacts();
if (DI::config()->get('system', 'optimize_tables')) {
self::optimizeTables();
}
DI::config()->set('system', 'last_expire_day', $d2); DI::config()->set('system', 'last_expire_day', $d2);
} }
@ -110,9 +111,20 @@ class Cron
// Optimizing this table only last seconds // Optimizing this table only last seconds
if (DI::config()->get('system', 'optimize_tables')) { if (DI::config()->get('system', 'optimize_tables')) {
DBA::e("OPTIMIZE TABLE `workerqueue`"); // We are acquiring the two locks from the worker to avoid locking problems
if (DI::lock()->acquire(Worker::LOCK_PROCESS, 10)) {
if (DI::lock()->acquire(Worker::LOCK_WORKER, 10)) {
DBA::e("OPTIMIZE TABLE `workerqueue`");
DBA::e("OPTIMIZE TABLE `process`");
DI::lock()->release(Worker::LOCK_WORKER);
}
DI::lock()->release(Worker::LOCK_PROCESS);
}
} }
// Clear cache entries
Worker::add(PRIORITY_LOW, 'ClearCache');
DI::config()->set('system', 'last_cron_hourly', time()); DI::config()->set('system', 'last_cron_hourly', time());
} }
@ -136,6 +148,25 @@ class Cron
return; return;
} }
/**
* Optimize tables that are known to grow and shrink all the time
*
* @return void
*/
private static function optimizeTables()
{
Logger::info('Optimize start');
DBA::e("OPTIMIZE TABLE `auth_codes`");
DBA::e("OPTIMIZE TABLE `challenge`");
DBA::e("OPTIMIZE TABLE `locks`");
DBA::e("OPTIMIZE TABLE `profile_check`");
DBA::e("OPTIMIZE TABLE `session`");
DBA::e("OPTIMIZE TABLE `tokens`");
DI::lock()->release('optimize_tables');
}
/** /**
* Checks for contacts that are about to be deleted and ensures that they are removed. * Checks for contacts that are about to be deleted and ensures that they are removed.
* This should be done automatically in the "remove" function. This here is a cleanup job. * This should be done automatically in the "remove" function. This here is a cleanup job.