refacor: improve cache interface (#3492)

* fix: proper typehint on setScope

* refactor: type hint setKey()

* typehint
This commit is contained in:
Dag 2023-07-06 15:10:30 +02:00 committed by GitHub
parent f8801d8cb3
commit caac7f572c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 56 additions and 220 deletions

View file

@ -2,8 +2,8 @@
class MemcachedCache implements CacheInterface
{
private $scope;
private $key;
private string $scope;
private string $key;
private $conn;
private $expiration = 0;
private $time = null;
@ -58,11 +58,11 @@ class MemcachedCache implements CacheInterface
return $result['data'];
}
public function saveData($datas)
public function saveData($data): void
{
$time = time();
$object_to_save = [
'data' => $datas,
'data' => $data,
'time' => $time,
];
$result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
@ -72,8 +72,6 @@ class MemcachedCache implements CacheInterface
}
$this->time = $time;
return $this;
}
public function getTime(): ?int
@ -84,32 +82,21 @@ class MemcachedCache implements CacheInterface
return $this->time;
}
public function purgeCache($duration)
public function purgeCache(int $seconds): void
{
// Note: does not purges cache right now
// Just sets cache expiration and leave cache purging for memcached itself
$this->expiration = $duration;
$this->expiration = $seconds;
}
public function setScope($scope)
public function setScope(string $scope): void
{
$this->scope = $scope;
return $this;
}
public function setKey($key)
public function setKey(array $key): void
{
if (!empty($key) && is_array($key)) {
$key = array_map('strtolower', $key);
}
$key = json_encode($key);
if (!is_string($key)) {
throw new \Exception('The given key is invalid!');
}
$this->key = $key;
return $this;
$this->key = json_encode($key);
}
private function getCacheKey()