Use CacheItem in FilesCache

This commit is contained in:
Giuseppe Criscione 2022-11-02 22:03:34 +01:00
parent b7d7e29f13
commit 6cdd584694

View File

@ -35,8 +35,8 @@ class FilesCache extends AbstractCache
public function fetch(string $key)
{
if ($this->has($key)) {
$data = PHP::parseFile($this->getFile($key));
return $data['value'];
$cacheItem = CacheItem::fromArray(PHP::parseFile($this->getFile($key)));
return $cacheItem->value();
}
if ($this->hasExpired($key)) {
FileSystem::delete($this->getFile($key));
@ -49,8 +49,8 @@ class FilesCache extends AbstractCache
*/
public function save(string $key, $value, int $ttl = null): void
{
$data = ['value' => $value, 'expires' => time() + ($ttl ?? $this->defaultTtl)];
PHP::encodeToFile($data, $this->getFile($key));
$cacheItem = new CacheItem($value, time() + ($ttl ?? $this->defaultTtl), time());
PHP::encodeToFile($cacheItem->toArray(), $this->getFile($key));
}
/**
@ -93,7 +93,7 @@ class FilesCache extends AbstractCache
*/
protected function hasExpired(string $key): bool
{
$data = PHP::parseFile($this->getFile($key));
return time() >= $data['expires'];
$cacheItem = CacheItem::fromArray(PHP::parseFile($this->getFile($key)));
return time() >= $cacheItem->expirationTime();
}
}