Always fill usernames cache. Cache fixes.
This commit is contained in:
parent
dedf20ea1b
commit
6cf1ef504f
|
@ -17,12 +17,15 @@ trait ArrayCacheTrait
|
||||||
*/
|
*/
|
||||||
protected array $cache = [];
|
protected array $cache = [];
|
||||||
protected string $ttl = '+1 day';
|
protected string $ttl = '+1 day';
|
||||||
private string $ttlCheckInterval = '+1 second';
|
private string $ttlCheckInterval = '+1 minute';
|
||||||
private int $nextTtlCheckTs = 0;
|
private int $nextTtlCheckTs = 0;
|
||||||
|
|
||||||
protected function getCache(string $key, $default = null)
|
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);
|
$this->cache[$key]['ttl'] = strtotime($this->ttl);
|
||||||
} else {
|
} else {
|
||||||
return $default;
|
return $default;
|
||||||
|
@ -39,20 +42,12 @@ trait ArrayCacheTrait
|
||||||
*/
|
*/
|
||||||
protected function setCache(string $key, $value): void
|
protected function setCache(string $key, $value): void
|
||||||
{
|
{
|
||||||
$now = time();
|
|
||||||
$this->cache[$key] = [
|
$this->cache[$key] = [
|
||||||
'value' => $value,
|
'value' => $value,
|
||||||
'ttl' => $now,
|
'ttl' => strtotime($this->ttl),
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($this->nextTtlCheckTs < $now) {
|
$this->cleanupCache();
|
||||||
$this->nextTtlCheckTs = strtotime($this->ttlCheckInterval, $now);
|
|
||||||
foreach ($this->cache as $cacheKey => $cacheValue) {
|
|
||||||
if ($cacheValue['ttl'] < $now) {
|
|
||||||
$this->unsetCache($cacheKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,4 +68,20 @@ trait ArrayCacheTrait
|
||||||
$this->cache = [];
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -142,6 +142,9 @@ class MysqlArray implements DbArray
|
||||||
|
|
||||||
public function offsetSet($index, $value): void
|
public function offsetSet($index, $value): void
|
||||||
{
|
{
|
||||||
|
if ($this->getCache($index) === $value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$this->setCache($index, $value);
|
$this->setCache($index, $value);
|
||||||
|
|
||||||
$this->request("
|
$this->request("
|
||||||
|
|
|
@ -95,6 +95,9 @@ trait PeerHandler
|
||||||
public function addUser(array $user): \Generator
|
public function addUser(array $user): \Generator
|
||||||
{
|
{
|
||||||
$existingChat = yield $this->chats[$user['id']];
|
$existingChat = yield $this->chats[$user['id']];
|
||||||
|
if ($existingChat) {
|
||||||
|
$this->cacheChatUsername($user['id'], $user);
|
||||||
|
}
|
||||||
if (!isset($user['access_hash']) && !($user['min'] ?? false)) {
|
if (!isset($user['access_hash']) && !($user['min'] ?? false)) {
|
||||||
if (!empty($existingChat['access_hash'])) {
|
if (!empty($existingChat['access_hash'])) {
|
||||||
$this->logger->logger("No access hash with user {$user['id']}, using backup");
|
$this->logger->logger("No access hash with user {$user['id']}, using backup");
|
||||||
|
@ -125,9 +128,9 @@ trait PeerHandler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->chats[$user['id']] = $user;
|
$this->chats[$user['id']] = $user;
|
||||||
$this->cacheChatUsername($user['id'], $user);
|
|
||||||
$this->cachePwrChat($user['id'], false, true);
|
$this->cachePwrChat($user['id'], false, true);
|
||||||
}
|
}
|
||||||
|
$this->cacheChatUsername($user['id'], $user);
|
||||||
break;
|
break;
|
||||||
case 'userEmpty':
|
case 'userEmpty':
|
||||||
break;
|
break;
|
||||||
|
@ -154,9 +157,9 @@ trait PeerHandler
|
||||||
if (!$existingChat || $existingChat !== $chat) {
|
if (!$existingChat || $existingChat !== $chat) {
|
||||||
$this->logger->logger("Updated chat -{$chat['id']}", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
|
$this->logger->logger("Updated chat -{$chat['id']}", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
|
||||||
$this->chats[-$chat['id']] = $chat;
|
$this->chats[-$chat['id']] = $chat;
|
||||||
$this->cacheChatUsername(-$chat['id'], $chat);
|
|
||||||
$this->cachePwrChat(-$chat['id'], $this->settings['peer']['full_fetch'], true);
|
$this->cachePwrChat(-$chat['id'], $this->settings['peer']['full_fetch'], true);
|
||||||
}
|
}
|
||||||
|
$this->cacheChatUsername(-$chat['id'], $chat);
|
||||||
break;
|
break;
|
||||||
case 'channelEmpty':
|
case 'channelEmpty':
|
||||||
break;
|
break;
|
||||||
|
@ -192,19 +195,19 @@ trait PeerHandler
|
||||||
$chat = $newchat;
|
$chat = $newchat;
|
||||||
}
|
}
|
||||||
$this->chats[$bot_api_id] = $chat;
|
$this->chats[$bot_api_id] = $chat;
|
||||||
$this->cacheChatUsername($bot_api_id, $chat);
|
|
||||||
$fullChat = yield $this->full_chats[$bot_api_id];
|
$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'])) {
|
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->cachePwrChat($bot_api_id, $this->settings['peer']['full_fetch'], true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->cacheChatUsername($bot_api_id, $chat);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function cacheChatUsername(int $id, array $chat)
|
private function cacheChatUsername(int $id, array $chat)
|
||||||
{
|
{
|
||||||
if (!empty($chat['username'])) {
|
if ($id && !empty($chat['username'])) {
|
||||||
$this->usernames[strtolower($chat['username'])] = $id;
|
$this->usernames[strtolower($chat['username'])] = $id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user