1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 05:34:42 +01:00

Allow arbitrary database TTL

This commit is contained in:
Daniil Gentili 2021-05-04 15:21:38 +02:00
parent 0fab07e246
commit 5693fc0bd8
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 13 additions and 9 deletions

View File

@ -3,6 +3,7 @@
namespace danog\MadelineProto\Db;
use Amp\Loop;
use Closure;
use danog\MadelineProto\Logger;
/**
@ -23,10 +24,6 @@ trait ArrayCacheTrait
* TTL interval.
*/
protected int $ttl = 5 * 60;
/**
* TTL cleanup interval.
*/
private int $ttlCheckInterval = 60;
/**
* Cache cleanup watcher ID.
@ -66,7 +63,10 @@ trait ArrayCacheTrait
protected function startCacheCleanupLoop(): void
{
$this->cacheCleanupId = Loop::repeat($this->ttlCheckInterval * 1000, fn () => $this->cleanupCache());
$this->cacheCleanupId = Loop::repeat(
\max(1000, ($this->ttl * 1000) / 5),
Closure::fromCallable([$this, 'cleanupCache']),
);
}
protected function stopCacheCleanupLoop(): void
{

View File

@ -3,6 +3,7 @@
namespace danog\MadelineProto\Db;
use Amp\Promise;
use danog\MadelineProto\Settings\Database\DatabaseAbstract as DatabaseDatabaseAbstract;
use danog\MadelineProto\Settings\Database\Memory;
use danog\MadelineProto\Settings\Database\Mysql;
use danog\MadelineProto\Settings\Database\Postgres;
@ -40,7 +41,7 @@ abstract class DbPropertiesFactory
$config = $propertyType['config'] ?? [];
$propertyType = \is_array($propertyType) ? $propertyType['type'] : $propertyType;
$propertyType = \strtolower($propertyType);
$class = !($config['enableCache'] ?? true) && !$dbSettings instanceof Memory
$class = $dbSettings instanceof DatabaseDatabaseAbstract && (!($config['enableCache'] ?? true) || !$dbSettings->getCacheTtl())
? __NAMESPACE__.'\\NullCache'
: __NAMESPACE__;

View File

@ -84,7 +84,7 @@ abstract class DriverArray implements DbArray
*/
public static function getInstance(string $table, $previous, $settings): Promise
{
if ($previous instanceof static && $previous->getTable() === $table) {
if ($previous && \get_class($previous) === static::class && $previous->getTable() === $table) {
$instance = &$previous;
} else {
$instance = new static();
@ -125,7 +125,7 @@ abstract class DriverArray implements DbArray
*/
protected static function renameTmpTable(self $new, $old): \Generator
{
if ($old instanceof static && $old->getTable()) {
if ($old && \str_replace('NullCache\\', '', \get_class($old)) === \str_replace('NullCache\\', '', \get_class($new)) && $old->getTable()) {
if (
$old->getTable() !== $new->getTable() &&
\mb_strpos($new->getTable(), 'tmp') !== 0
@ -147,7 +147,10 @@ abstract class DriverArray implements DbArray
protected static function migrateDataToDb(self $new, $old): \Generator
{
if (!empty($old) && !$old instanceof static) {
Logger::log('Converting database to '.\get_class($new), Logger::ERROR);
if (\str_replace('NullCache\\', '', \get_class($old)) === \str_replace('NullCache\\', '', \get_class($new))) {
return;
}
Logger::log('Converting '.\get_class($old).' to '.\get_class($new), Logger::ERROR);
if (!$old instanceof DbArray) {
$old = yield MemoryArray::getInstance('', $old, new Memory);