From 7c1b602779328877a6f988167f298370e0260f81 Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Mon, 18 May 2020 18:50:47 +0300 Subject: [PATCH] Fix Undefined index: value in ArrayCacheTrait.php:34 --- .../MadelineProto/Db/ArrayCacheTrait.php | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/danog/MadelineProto/Db/ArrayCacheTrait.php b/src/danog/MadelineProto/Db/ArrayCacheTrait.php index 66ed3ad7..f94a90eb 100644 --- a/src/danog/MadelineProto/Db/ArrayCacheTrait.php +++ b/src/danog/MadelineProto/Db/ArrayCacheTrait.php @@ -23,15 +23,16 @@ trait ArrayCacheTrait protected function getCache(string $key, $default = null) { $cacheItem = $this->cache[$key] ?? null; - $this->cleanupCache(); + $result = $default; - if ($cacheItem) { + if (\is_array($cacheItem)) { + $result = $cacheItem['value']; $this->cache[$key]['ttl'] = strtotime($this->ttl); - } else { - return $default; } - return $cacheItem['value']; + $this->cleanupCache(); + + return $result; } /** @@ -74,14 +75,20 @@ trait ArrayCacheTrait 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); - } + if ($this->nextTtlCheckTs > $now) { + return; + } + + $this->nextTtlCheckTs = strtotime($this->ttlCheckInterval, $now); + $oldKeys = []; + foreach ($this->cache as $cacheKey => $cacheValue) { + if ($cacheValue['ttl'] < $now) { + $oldKeys[] = $cacheKey; } } + foreach ($oldKeys as $oldKey) { + $this->unsetCache($oldKey); + } } } \ No newline at end of file