Fix Undefined index: value in ArrayCacheTrait.php:34

This commit is contained in:
Alexander Pankratov 2020-05-18 18:50:47 +03:00
parent d0e57f2535
commit 7c1b602779

View File

@ -23,15 +23,16 @@ trait ArrayCacheTrait
protected function getCache(string $key, $default = null) protected function getCache(string $key, $default = null)
{ {
$cacheItem = $this->cache[$key] ?? 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); $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 protected function cleanupCache(): void
{ {
$now = time(); $now = time();
if ($this->nextTtlCheckTs < $now) { if ($this->nextTtlCheckTs > $now) {
$this->nextTtlCheckTs = strtotime($this->ttlCheckInterval, $now); return;
foreach ($this->cache as $cacheKey => $cacheValue) { }
if ($cacheValue['ttl'] < $now) {
$this->unsetCache($cacheKey); $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);
}
} }
} }