Skip to content

Commit

Permalink
Small updates, CS cleaning (#11)
Browse files Browse the repository at this point in the history
- FileClient creates now 1-level subdirectories (down from 2)
- improves the error handling in Redis/Json clients
- uses psr-4 for autoloading
- upgraded phpunit version
- updates some unit tests
- CS cleanup
  • Loading branch information
kodeart authored Sep 23, 2018
1 parent e7a52f1 commit 0272fa8
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Koded\Caching;

use Koded\Stdlib\Interfaces\StringSerializable;
use Psr\SimpleCache\{ CacheInterface, InvalidArgumentException };
use Psr\SimpleCache\{CacheInterface, InvalidArgumentException};

interface Cache
{
Expand Down
2 changes: 1 addition & 1 deletion CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function forUnsupportedLogger(string $supported, string $given)
{
return new self(Cache::E_UNSUPPORTED_LOGGER, [':supported' => $supported, ':given' => $given]);
}

public static function forCreatingDirectory(string $directory)
{
return new static(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]);
Expand Down
14 changes: 7 additions & 7 deletions Client/FileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function get($key, $default = null)
{
$filename = $this->filename($key, false);

if (!is_file($filename)) {
if (false === is_file($filename)) {
return $default;
}

Expand Down Expand Up @@ -100,8 +100,8 @@ public function clear()
$this->logger->error($e->getMessage());

return false;
// @codeCoverageIgnoreEnd
}
// @codeCoverageIgnoreEnd
}

public function deleteMultiple($keys)
Expand Down Expand Up @@ -133,7 +133,7 @@ private function initialize(string $directory)
$dir = $directory ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cache';
$dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

if (!is_dir($dir) && false === mkdir($dir, 0775, true)) {
if (false === is_dir($dir) && false === mkdir($dir, 0775, true)) {
$e = FileCacheClientException::forCreatingDirectory($dir);
$this->logger->error($e->getMessage());
throw $e;
Expand All @@ -153,13 +153,13 @@ private function initialize(string $directory)
private function filename(string $key, bool $create = true): string
{
$filename = sha1($key);
$dir = $this->dir . substr($filename, 0, 2);
$dir = $this->dir . substr($filename, 0, 1);

if ($create && !is_dir($dir)) {
mkdir($dir, 0775, true);
if ($create && false === is_dir($dir)) {
mkdir($dir, 0775, true) || $this->logger->error('Failed to create cache directory: {dir}', ['dir' => $dir]);
}

$filename = $dir . DIRECTORY_SEPARATOR . substr($filename, 2) . '.php';
$filename = $dir . DIRECTORY_SEPARATOR . substr($filename, 1) . '.php';

if ($create && !is_file($filename)) {
touch($filename);
Expand Down
2 changes: 1 addition & 1 deletion Client/PredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Koded\Caching\Client;

use Exception;
use Koded\Caching\{ CacheException, CacheSerializer };
use Koded\Caching\{CacheException, CacheSerializer};
use Koded\Caching\Configuration\PredisConfiguration;
use Koded\Caching\Serializer\PhpSerializer;
use Predis\Client;
Expand Down
8 changes: 8 additions & 0 deletions Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use Exception;
use Koded\Caching\CacheException;
use Koded\Caching\Configuration\RedisConfiguration;
use function Koded\Stdlib\dump;
use Psr\SimpleCache\CacheInterface;
use Redis;
use RedisException;

/**
* Class RedisClient uses the Redis PHP extension.
Expand All @@ -35,7 +37,9 @@ public function __construct(Redis $client, RedisConfiguration $config)
try {
// Because connect() does not throw exception, but E_WARNING
if (false === @$this->client->connect(...$config->getConnectionParams())) {
// @codeCoverageIgnoreStart
throw CacheException::withConnectionErrorFor('Redis');
// @codeCoverageIgnoreEnd
}

$this->client->setOption(Redis::OPT_SERIALIZER, $config->getSerializerType());
Expand All @@ -45,6 +49,10 @@ public function __construct(Redis $client, RedisConfiguration $config)
if ($auth = $config->get('auth')) {
$this->client->auth($auth);
}

} catch (RedisException $e) {
error_log($e->getMessage());
throw CacheException::withConnectionErrorFor('Redis');
} catch (CacheException $e) {
throw $e;
} catch (Exception $e) {
Expand Down
9 changes: 9 additions & 0 deletions Client/RedisJsonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Koded\Caching\Serializer\{ JsonSerializer, PhpSerializer };
use Psr\SimpleCache\CacheInterface;
use Redis;
use RedisException;

/**
* Class RedisJsonClient uses the Redis PHP extension to save the cache item as JSON.
Expand Down Expand Up @@ -62,7 +63,9 @@ public function __construct(
try {
// Because connect() does not throw exception, but E_WARNING
if (false === @$this->client->connect(...$config->getConnectionParams())) {
// @codeCoverageIgnoreStart
throw CacheException::withConnectionErrorFor('Redis');
// @codeCoverageIgnoreEnd
}

$this->client->setOption(Redis::OPT_SERIALIZER, $config->getSerializerType());
Expand All @@ -72,8 +75,14 @@ public function __construct(
if ($auth = $config->get('auth')) {
$this->client->auth($auth);
}

} catch (RedisException $e) {
error_log($e->getMessage());
throw CacheException::withConnectionErrorFor('Redis');
} catch (CacheException $e) {
// @codeCoverageIgnoreStart
throw $e;
// @codeCoverageIgnoreEnd
} catch (Exception $e) {
throw CacheException::generic($e->getMessage(), $e);
}
Expand Down
12 changes: 9 additions & 3 deletions ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@

namespace Koded\Caching;

use Koded\Caching\Client\{ FileClient, MemcachedClient, MemoryClient, NullClient, PredisClient, RedisClient, RedisJsonClient };
use Koded\Caching\Serializer\{ JsonSerializer, PhpSerializer };
use Koded\Caching\Client\{FileClient,
MemcachedClient,
MemoryClient,
NullClient,
PredisClient,
RedisClient,
RedisJsonClient};
use Koded\Caching\Serializer\{JsonSerializer, PhpSerializer};
use Koded\Stdlib\Interfaces\ConfigurationFactory;
use Psr\Log\{ LoggerInterface, NullLogger };
use Psr\Log\{LoggerInterface, NullLogger};
use Psr\SimpleCache\CacheInterface;

class ClientFactory
Expand Down
5 changes: 3 additions & 2 deletions Configuration/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

namespace Koded\Caching\Configuration;

use Koded\Stdlib\{ Arguments, Config, Interfaces\Configuration };
use Koded\Stdlib\{Arguments, Config};
use Koded\Stdlib\Interfaces\Configuration;

/**
* Class ConfigFactory
*
* @property int $ttl Default Time-To-Live seconds for the cached items
* @property int $ttl Default Time-To-Live seconds for the cached items
* @property string $keyRegex Regex for validating the cache item key
*
*/
Expand Down
2 changes: 1 addition & 1 deletion Configuration/PredisConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Koded\Stdlib\Immutable;
use Koded\Stdlib\Interfaces\Configuration;

final class PredisConfiguration extends Immutable implements Configuration
final class PredisConfiguration extends Immutable implements Configuration
{

public function getConnectionParams(): array
Expand Down
2 changes: 1 addition & 1 deletion Configuration/RedisConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Koded\Caching\Configuration;

use Koded\Caching\{ Cache, CacheException };
use Koded\Caching\{Cache, CacheException};
use Koded\Stdlib\Immutable;
use Koded\Stdlib\Interfaces\Configuration;
use Redis;
Expand Down
2 changes: 1 addition & 1 deletion Serializer/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Koded\Caching\Serializer;

use Koded\Caching\{ CacheException, CacheSerializer };
use Koded\Caching\{CacheException, CacheSerializer};

final class JsonSerializer implements CacheSerializer
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Client/FileClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_cache_content()
$client = new FileClient(new FileConfiguration(['dir' => $this->dir->url()]), new NullLogger);
$client->set('foo', new Arguments(['foo' => 'bar']));

$raw = $this->dir->getChild('0b/eec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33.php')->getContent();
$raw = $this->dir->getChild('0/beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33.php')->getContent();
$this->assertContains(var_export([
'timestamp' => 32503593600,
'key' => 'foo',
Expand Down
7 changes: 7 additions & 0 deletions Tests/InvalidSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public function test_should_fail_on_invalid_serializer_in_configuration()
'host' => getenv('REDIS_SERVER_HOST'),
])))->build());
}

protected function setUp()
{
if (false === extension_loaded('redis')) {
$this->markTestSkipped('Redis extension is not loaded.');
}
}
}
11 changes: 7 additions & 4 deletions Tests/RedisJsonConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ public function test_should_throw_exception_on_connection_error()

putenv('CACHE_CLIENT=redis');

if (false === extension_loaded('redis')) {
$this->markTestSkipped('Redis extension is not loaded.');
}

new SimpleCache((new ClientFactory(new ConfigFactory([
'host' => 'invalid-redis-host',
'serializer' => Cache::SERIALIZER_JSON
])))->build());
}

protected function setUp()
{
if (false === extension_loaded('redis')) {
$this->markTestSkipped('Redis extension is not loaded.');
}
}
}
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.0
1.9.1
9 changes: 2 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"psr/simple-cache": "~1"
},
"autoload": {
"psr-0": {
"psr-4": {
"Koded\\Caching\\": ""
},
"files": [
Expand All @@ -36,14 +36,9 @@
]
},
"require-dev": {
"phpunit/phpunit": "~6",
"phpunit/phpunit": "~7",
"mikey179/vfsStream": "~1",
"satooshi/php-coveralls": "~1",
"predis/predis": "dev-master"
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
}
}
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function simple_cache_factory(string $client = '', array $arguments = []): Simpl
* Differs from PSR-16 "reserved" characters by adding the ":" to the list.
* The colon is a wide accepted convention for Redis to separate the key.
*
* @param string $key The cache key
* @param string $key The cache key
*
* @throws CacheException
*/
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.4/phpunit.xsd"
bootstrap="Tests/autoload.php"
stopOnFailure="true"
verbose="true"
colors="true">
<testsuites>
Expand Down

0 comments on commit 0272fa8

Please sign in to comment.