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
1 changed files with 18 additions and 11 deletions

View File

@ -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);
}
}
}