Always fill usernames cache. Cache fixes.

This commit is contained in:
Alexander Pankratov 2020-05-09 01:47:12 +03:00
parent dedf20ea1b
commit 6cf1ef504f
3 changed files with 33 additions and 16 deletions

View File

@ -17,12 +17,15 @@ trait ArrayCacheTrait
*/
protected array $cache = [];
protected string $ttl = '+1 day';
private string $ttlCheckInterval = '+1 second';
private string $ttlCheckInterval = '+1 minute';
private int $nextTtlCheckTs = 0;
protected function getCache(string $key, $default = null)
{
if ($cacheItem = $this->cache[$key] ?? null) {
$cacheItem = $this->cache[$key] ?? null;
$this->cleanupCache();
if ($cacheItem) {
$this->cache[$key]['ttl'] = strtotime($this->ttl);
} else {
return $default;
@ -39,20 +42,12 @@ trait ArrayCacheTrait
*/
protected function setCache(string $key, $value): void
{
$now = time();
$this->cache[$key] = [
'value' => $value,
'ttl' => $now,
'ttl' => strtotime($this->ttl),
];
if ($this->nextTtlCheckTs < $now) {
$this->nextTtlCheckTs = strtotime($this->ttlCheckInterval, $now);
foreach ($this->cache as $cacheKey => $cacheValue) {
if ($cacheValue['ttl'] < $now) {
$this->unsetCache($cacheKey);
}
}
}
$this->cleanupCache();
}
/**
@ -73,4 +68,20 @@ trait ArrayCacheTrait
$this->cache = [];
}
/**
* Remove all keys from cache
*/
protected function cleanupCache(): void
{
$now = time();
if ($this->nextTtlCheckTs < $now) {
$this->nextTtlCheckTs = strtotime($this->ttlCheckInterval, $now);
foreach ($this->cache as $cacheKey => $cacheValue) {
if ($cacheValue['ttl'] < $now) {
$this->unsetCache($cacheKey);
}
}
}
}
}

View File

@ -142,6 +142,9 @@ class MysqlArray implements DbArray
public function offsetSet($index, $value): void
{
if ($this->getCache($index) === $value) {
return;
}
$this->setCache($index, $value);
$this->request("

View File

@ -95,6 +95,9 @@ trait PeerHandler
public function addUser(array $user): \Generator
{
$existingChat = yield $this->chats[$user['id']];
if ($existingChat) {
$this->cacheChatUsername($user['id'], $user);
}
if (!isset($user['access_hash']) && !($user['min'] ?? false)) {
if (!empty($existingChat['access_hash'])) {
$this->logger->logger("No access hash with user {$user['id']}, using backup");
@ -125,9 +128,9 @@ trait PeerHandler
}
}
$this->chats[$user['id']] = $user;
$this->cacheChatUsername($user['id'], $user);
$this->cachePwrChat($user['id'], false, true);
}
$this->cacheChatUsername($user['id'], $user);
break;
case 'userEmpty':
break;
@ -154,9 +157,9 @@ trait PeerHandler
if (!$existingChat || $existingChat !== $chat) {
$this->logger->logger("Updated chat -{$chat['id']}", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
$this->chats[-$chat['id']] = $chat;
$this->cacheChatUsername(-$chat['id'], $chat);
$this->cachePwrChat(-$chat['id'], $this->settings['peer']['full_fetch'], true);
}
$this->cacheChatUsername(-$chat['id'], $chat);
break;
case 'channelEmpty':
break;
@ -192,19 +195,19 @@ trait PeerHandler
$chat = $newchat;
}
$this->chats[$bot_api_id] = $chat;
$this->cacheChatUsername($bot_api_id, $chat);
$fullChat = yield $this->full_chats[$bot_api_id];
if ($this->settings['peer']['full_fetch'] && (!$fullChat || $fullChat['full']['participants_count'] !== (yield from $this->getFullInfo($bot_api_id))['full']['participants_count'])) {
$this->cachePwrChat($bot_api_id, $this->settings['peer']['full_fetch'], true);
}
}
$this->cacheChatUsername($bot_api_id, $chat);
break;
}
}
private function cacheChatUsername(int $id, array $chat)
{
if (!empty($chat['username'])) {
if ($id && !empty($chat['username'])) {
$this->usernames[strtolower($chat['username'])] = $id;
}
}