New mysql cache cleanup
This commit is contained in:
parent
2aab5e8cc8
commit
ca03bc662a
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace danog\MadelineProto\Db;
|
namespace danog\MadelineProto\Db;
|
||||||
|
|
||||||
|
use Amp\Loop;
|
||||||
|
use danog\MadelineProto\Logger;
|
||||||
|
|
||||||
trait ArrayCacheTrait
|
trait ArrayCacheTrait
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -18,7 +21,6 @@ trait ArrayCacheTrait
|
|||||||
protected array $cache = [];
|
protected array $cache = [];
|
||||||
protected string $ttl = '+5 minutes';
|
protected string $ttl = '+5 minutes';
|
||||||
private string $ttlCheckInterval = '+1 minute';
|
private string $ttlCheckInterval = '+1 minute';
|
||||||
private int $nextTtlCheckTs = 0;
|
|
||||||
|
|
||||||
protected function getCache(string $key, $default = null)
|
protected function getCache(string $key, $default = null)
|
||||||
{
|
{
|
||||||
@ -30,8 +32,6 @@ trait ArrayCacheTrait
|
|||||||
$this->cache[$key]['ttl'] = strtotime($this->ttl);
|
$this->cache[$key]['ttl'] = strtotime($this->ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->cleanupCache();
|
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,8 +47,6 @@ trait ArrayCacheTrait
|
|||||||
'value' => $value,
|
'value' => $value,
|
||||||
'ttl' => strtotime($this->ttl),
|
'ttl' => strtotime($this->ttl),
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->cleanupCache();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,12 +59,9 @@ trait ArrayCacheTrait
|
|||||||
unset($this->cache[$key]);
|
unset($this->cache[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function startCacheCleanupLoop(): void
|
||||||
* Remove all keys from cache
|
|
||||||
*/
|
|
||||||
protected function clearCache(): void
|
|
||||||
{
|
{
|
||||||
$this->cache = [];
|
Loop::repeat(strtotime($this->ttlCheckInterval, 0) * 1000, fn() => $this->cleanupCache());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,11 +70,6 @@ trait ArrayCacheTrait
|
|||||||
protected function cleanupCache(): void
|
protected function cleanupCache(): void
|
||||||
{
|
{
|
||||||
$now = time();
|
$now = time();
|
||||||
if ($this->nextTtlCheckTs > $now) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->nextTtlCheckTs = strtotime($this->ttlCheckInterval, $now);
|
|
||||||
$oldKeys = [];
|
$oldKeys = [];
|
||||||
foreach ($this->cache as $cacheKey => $cacheValue) {
|
foreach ($this->cache as $cacheKey => $cacheValue) {
|
||||||
if ($cacheValue['ttl'] < $now) {
|
if ($cacheValue['ttl'] < $now) {
|
||||||
@ -89,6 +79,14 @@ trait ArrayCacheTrait
|
|||||||
foreach ($oldKeys as $oldKey) {
|
foreach ($oldKeys as $oldKey) {
|
||||||
$this->unsetCache($oldKey);
|
$this->unsetCache($oldKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logger::log(
|
||||||
|
sprintf(
|
||||||
|
"cache for table:%s; keys left: %s; keys removed: %s",
|
||||||
|
$this->table, \count($this->cache), \count($oldKeys)
|
||||||
|
),
|
||||||
|
Logger::VERBOSE
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -57,6 +57,8 @@ class MysqlArray implements DbArray
|
|||||||
$instance->db = static::getDbConnection($settings);
|
$instance->db = static::getDbConnection($settings);
|
||||||
$instance->ttl = $settings['cache_ttl'] ?? $instance->ttl;
|
$instance->ttl = $settings['cache_ttl'] ?? $instance->ttl;
|
||||||
|
|
||||||
|
$instance->startCacheCleanupLoop();
|
||||||
|
|
||||||
return call(static function() use($instance, $value) {
|
return call(static function() use($instance, $value) {
|
||||||
yield from static::renameTmpTable($instance, $value);
|
yield from static::renameTmpTable($instance, $value);
|
||||||
yield from $instance->prepareTable();
|
yield from $instance->prepareTable();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user