Improve code, fix filesystem test failing after first run
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "iqparts/cache",
|
||||
"type": "library",
|
||||
"description": "Simple cache library with Redis and Filesystem adapters.",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"predis/predis": "^1.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.3",
|
||||
"phpstan/phpstan": "^0.9.1"
|
||||
"phpunit/phpunit": "^6.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,14 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class AbstractTestCase extends TestCase
|
||||
{
|
||||
public function getTmpDirectory()
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTmpDirectory(): string
|
||||
{
|
||||
$location = __DIR__ . '/../tmp';
|
||||
if (!file_exists($location)) {
|
||||
if (!mkdir($location)) {
|
||||
if (!mkdir($location) && !is_dir($location)) {
|
||||
throw new \RuntimeException('Could not create directory: ' . $location);
|
||||
}
|
||||
chmod($location, 0777);
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace IQParts\CacheTest\Unit\Adapter;
|
||||
use IQParts\Cache\Adapter\FilesystemAdapter;
|
||||
use IQParts\Cache\Serializer\JsonSerializer;
|
||||
use IQParts\CacheTest\AbstractTestCase;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
final class FilesystemAdapterTest extends AbstractTestCase
|
||||
{
|
||||
@@ -27,7 +29,7 @@ final class FilesystemAdapterTest extends AbstractTestCase
|
||||
|
||||
$adapter->set('ttl', 'b', 200);
|
||||
$this->assertTrue($adapter->ttl('ttl') > 0);
|
||||
$this->assertTrue($adapter->ttl('b') === $adapter::NO_TTL);
|
||||
$this->assertSame($adapter->ttl('b'), $adapter::NO_TTL);
|
||||
|
||||
$adapter->set('a-keys', 'a');
|
||||
$adapter->set('b-keys', 'a');
|
||||
@@ -118,4 +120,22 @@ final class FilesystemAdapterTest extends AbstractTestCase
|
||||
$this->assertEquals('a', $adapter->get('a/b'));
|
||||
$adapter->delete('a/b');
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$files = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($this->getTmpDirectory(), RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::CHILD_FIRST
|
||||
);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
rmdir($file->getRealPath());
|
||||
} else {
|
||||
unlink($file->getRealPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,9 @@ use IQParts\CacheTest\AbstractTestCase;
|
||||
|
||||
final class MemoryAdapterTest extends AbstractTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testAdapter()
|
||||
{
|
||||
$adapter = new MemoryAdapter();
|
||||
@@ -25,7 +28,7 @@ final class MemoryAdapterTest extends AbstractTestCase
|
||||
|
||||
$adapter->set('a', 'b', 200);
|
||||
$this->assertTrue($adapter->ttl('a') > 0);
|
||||
$this->assertTrue($adapter->ttl('c') === MemoryAdapter::NO_TTL);
|
||||
$this->assertSame($adapter->ttl('c'), MemoryAdapter::NO_TTL);
|
||||
}
|
||||
|
||||
public function testTtl()
|
||||
|
||||
@@ -10,10 +10,10 @@ final class JsonSerializerTest extends AbstractTestCase
|
||||
public function testSerialize()
|
||||
{
|
||||
$serializer = new JsonSerializer();
|
||||
$string = "myString";
|
||||
$string = 'myString';
|
||||
$array = [0 => '1', 1 => '2', 2 => '3'];
|
||||
|
||||
$this->assertTrue(is_string($serializer->serialize($array)));
|
||||
$this->assertInternalType('string', $serializer->serialize($array));
|
||||
$this->assertEquals($string, $serializer->serialize($string));
|
||||
|
||||
$this->assertEquals($array, $serializer->deserialize($serializer->serialize($array)));
|
||||
|
||||
Reference in New Issue
Block a user