Improve code, fix filesystem test failing after first run
This commit is contained in:
@@ -17,7 +17,7 @@ interface CacheAdapterInterface
|
||||
* @param int|null $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null);
|
||||
public function set(string $key, $value, int $ttl = null): void;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
|
||||
@@ -49,21 +49,21 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param SerializerInterface $serializer
|
||||
* @param int|null $chmod
|
||||
* @param int|null $ttl
|
||||
* @param string|null $directorySeperator
|
||||
* @param string|null $directorySeparator
|
||||
*/
|
||||
public function __construct(
|
||||
string $directory,
|
||||
SerializerInterface $serializer,
|
||||
int $chmod = null,
|
||||
int $ttl = null,
|
||||
string $directorySeperator = null
|
||||
string $directorySeparator = null
|
||||
)
|
||||
{
|
||||
$this->indexLocation = $directory . '/' . md5($directory) . 'index';
|
||||
$this->ttlLocation = $directory . '/' . md5($directory) . 'ttl';
|
||||
$this->directory = $directory;
|
||||
$this->serializer = $serializer;
|
||||
$this->directorySeparator = $directorySeperator;
|
||||
$this->directorySeparator = $directorySeparator;
|
||||
$this->chmod = $chmod;
|
||||
$this->ttl = $ttl;
|
||||
|
||||
@@ -106,7 +106,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param int|null $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null)
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
$file = $this->getFilename($key);
|
||||
file_put_contents($file, $this->serializer->serialize($value));
|
||||
@@ -136,8 +136,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
if ($this->exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
unset($this->index[$key]);
|
||||
unset($this->timeToLive[$key]);
|
||||
unset($this->index[$key], $this->timeToLive[$key]);
|
||||
$this->saveIndex();
|
||||
}
|
||||
}
|
||||
@@ -175,7 +174,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
/**
|
||||
* @param $pattern
|
||||
*/
|
||||
private function deleteGlob($pattern)
|
||||
private function deleteGlob($pattern): void
|
||||
{
|
||||
foreach ($this->index as $key => $value) {
|
||||
if (fnmatch($pattern, $key)) {
|
||||
@@ -188,7 +187,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param $file
|
||||
* @return bool
|
||||
*/
|
||||
private function exists(string $file)
|
||||
private function exists(string $file): bool
|
||||
{
|
||||
return file_exists($file);
|
||||
}
|
||||
@@ -197,9 +196,9 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param $key
|
||||
* @return string
|
||||
*/
|
||||
private function getFilename(string $key)
|
||||
private function getFilename(string $key): string
|
||||
{
|
||||
list($directory, $file) = $this->getDirectoryAndFile($key);
|
||||
[$directory, $file] = $this->getDirectoryAndFile($key);
|
||||
return $directory . '/' . md5($file);
|
||||
}
|
||||
|
||||
@@ -207,7 +206,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
private function getDirectoryAndFile(string $key)
|
||||
private function getDirectoryAndFile(string $key): array
|
||||
{
|
||||
$directory = $this->directory;
|
||||
if ($this->directorySeparator !== null) {
|
||||
@@ -224,17 +223,19 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
/**
|
||||
* @param $directory
|
||||
*/
|
||||
private function createSubDirectoryIfNotExists($directory)
|
||||
private function createSubDirectoryIfNotExists($directory): void
|
||||
{
|
||||
if (file_exists($directory) === false) {
|
||||
mkdir($directory);
|
||||
if (!mkdir($directory) && !is_dir($directory)) {
|
||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
|
||||
}
|
||||
if ($this->chmod !== null) {
|
||||
chmod($directory, $this->chmod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function saveIndex()
|
||||
private function saveIndex(): void
|
||||
{
|
||||
file_put_contents($this->indexLocation, json_encode($this->index));
|
||||
file_put_contents($this->ttlLocation, json_encode($this->timeToLive));
|
||||
|
||||
@@ -32,8 +32,7 @@ final class MemoryAdapter implements CacheAdapterInterface
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
unset($this->timeToLive[$key]);
|
||||
unset($this->data[$key]);
|
||||
unset($this->timeToLive[$key], $this->data[$key]);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -43,7 +42,7 @@ final class MemoryAdapter implements CacheAdapterInterface
|
||||
* @param int|null $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null)
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
if ($ttl !== null) {
|
||||
@@ -53,13 +52,11 @@ final class MemoryAdapter implements CacheAdapterInterface
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $key)
|
||||
public function delete(string $key): void
|
||||
{
|
||||
unset($this->data[$key]);
|
||||
unset($this->timeToLive[$key]);
|
||||
return 1;
|
||||
unset($this->data[$key], $this->timeToLive[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -48,7 +48,7 @@ final class NamespaceAdapter implements CacheAdapterInterface
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $key)
|
||||
public function delete(string $key): void
|
||||
{
|
||||
$this->cacheAdapter->delete($this->namespace . $key);
|
||||
}
|
||||
@@ -57,19 +57,20 @@ final class NamespaceAdapter implements CacheAdapterInterface
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
public function keys($key = '*')
|
||||
public function keys($key = '*'): array
|
||||
{
|
||||
if (substr($key, -1) !== '*') {
|
||||
$key = $key . '*';
|
||||
$key .= '*';
|
||||
}
|
||||
|
||||
/** @var string[] $keys */
|
||||
$keys = $this->cacheAdapter->keys($this->namespace . $key);
|
||||
$allowed = [];
|
||||
$len = strlen($this->namespace);
|
||||
$len = \strlen($this->namespace);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (substr($key, 0, $len) === $this->namespace) {
|
||||
$allowed[] = substr($key, $len);
|
||||
foreach ($keys as $cacheKey) {
|
||||
if (0 === strpos($cacheKey, $this->namespace)) {
|
||||
$allowed[] = substr($cacheKey, $len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ final class NullAdapter implements CacheAdapterInterface
|
||||
* @param int|null $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null)
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ final class NullAdapter implements CacheAdapterInterface
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $key)
|
||||
public function delete(string $key): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ final class PredisAdapter implements CacheAdapterInterface
|
||||
* @param $value
|
||||
* @param int|null $ttl TTL in seconds
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null)
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
if ($ttl !== null && $ttl !== 0) {
|
||||
$this->client->setex($key, $ttl ?? $this->defaultTtl, $this->serializer->serialize($value));
|
||||
|
||||
@@ -4,9 +4,9 @@ namespace IQParts\Cache\Serializer;
|
||||
|
||||
final class JsonSerializer implements SerializerInterface
|
||||
{
|
||||
public function serialize($input)
|
||||
public function serialize($input): string
|
||||
{
|
||||
if (is_array($input)) {
|
||||
if (\is_array($input)) {
|
||||
return json_encode($input);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user