Skip to content

Commit 0272fa8

Browse files
authored
Small updates, CS cleaning (#11)
- 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
1 parent e7a52f1 commit 0272fa8

18 files changed

Lines changed: 62 additions & 34 deletions

Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Koded\Caching;
1414

1515
use Koded\Stdlib\Interfaces\StringSerializable;
16-
use Psr\SimpleCache\{ CacheInterface, InvalidArgumentException };
16+
use Psr\SimpleCache\{CacheInterface, InvalidArgumentException};
1717

1818
interface Cache
1919
{

CacheException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static function forUnsupportedLogger(string $supported, string $given)
3636
{
3737
return new self(Cache::E_UNSUPPORTED_LOGGER, [':supported' => $supported, ':given' => $given]);
3838
}
39-
39+
4040
public static function forCreatingDirectory(string $directory)
4141
{
4242
return new static(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]);

Client/FileClient.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function get($key, $default = null)
5050
{
5151
$filename = $this->filename($key, false);
5252

53-
if (!is_file($filename)) {
53+
if (false === is_file($filename)) {
5454
return $default;
5555
}
5656

@@ -100,8 +100,8 @@ public function clear()
100100
$this->logger->error($e->getMessage());
101101

102102
return false;
103+
// @codeCoverageIgnoreEnd
103104
}
104-
// @codeCoverageIgnoreEnd
105105
}
106106

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

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

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

162-
$filename = $dir . DIRECTORY_SEPARATOR . substr($filename, 2) . '.php';
162+
$filename = $dir . DIRECTORY_SEPARATOR . substr($filename, 1) . '.php';
163163

164164
if ($create && !is_file($filename)) {
165165
touch($filename);

Client/PredisClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Koded\Caching\Client;
1414

1515
use Exception;
16-
use Koded\Caching\{ CacheException, CacheSerializer };
16+
use Koded\Caching\{CacheException, CacheSerializer};
1717
use Koded\Caching\Configuration\PredisConfiguration;
1818
use Koded\Caching\Serializer\PhpSerializer;
1919
use Predis\Client;

Client/RedisClient.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Exception;
1616
use Koded\Caching\CacheException;
1717
use Koded\Caching\Configuration\RedisConfiguration;
18+
use function Koded\Stdlib\dump;
1819
use Psr\SimpleCache\CacheInterface;
1920
use Redis;
21+
use RedisException;
2022

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

4145
$this->client->setOption(Redis::OPT_SERIALIZER, $config->getSerializerType());
@@ -45,6 +49,10 @@ public function __construct(Redis $client, RedisConfiguration $config)
4549
if ($auth = $config->get('auth')) {
4650
$this->client->auth($auth);
4751
}
52+
53+
} catch (RedisException $e) {
54+
error_log($e->getMessage());
55+
throw CacheException::withConnectionErrorFor('Redis');
4856
} catch (CacheException $e) {
4957
throw $e;
5058
} catch (Exception $e) {

Client/RedisJsonClient.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Koded\Caching\Serializer\{ JsonSerializer, PhpSerializer };
1919
use Psr\SimpleCache\CacheInterface;
2020
use Redis;
21+
use RedisException;
2122

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

6871
$this->client->setOption(Redis::OPT_SERIALIZER, $config->getSerializerType());
@@ -72,8 +75,14 @@ public function __construct(
7275
if ($auth = $config->get('auth')) {
7376
$this->client->auth($auth);
7477
}
78+
79+
} catch (RedisException $e) {
80+
error_log($e->getMessage());
81+
throw CacheException::withConnectionErrorFor('Redis');
7582
} catch (CacheException $e) {
83+
// @codeCoverageIgnoreStart
7684
throw $e;
85+
// @codeCoverageIgnoreEnd
7786
} catch (Exception $e) {
7887
throw CacheException::generic($e->getMessage(), $e);
7988
}

ClientFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212

1313
namespace Koded\Caching;
1414

15-
use Koded\Caching\Client\{ FileClient, MemcachedClient, MemoryClient, NullClient, PredisClient, RedisClient, RedisJsonClient };
16-
use Koded\Caching\Serializer\{ JsonSerializer, PhpSerializer };
15+
use Koded\Caching\Client\{FileClient,
16+
MemcachedClient,
17+
MemoryClient,
18+
NullClient,
19+
PredisClient,
20+
RedisClient,
21+
RedisJsonClient};
22+
use Koded\Caching\Serializer\{JsonSerializer, PhpSerializer};
1723
use Koded\Stdlib\Interfaces\ConfigurationFactory;
18-
use Psr\Log\{ LoggerInterface, NullLogger };
24+
use Psr\Log\{LoggerInterface, NullLogger};
1925
use Psr\SimpleCache\CacheInterface;
2026

2127
class ClientFactory

Configuration/ConfigFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
namespace Koded\Caching\Configuration;
1414

15-
use Koded\Stdlib\{ Arguments, Config, Interfaces\Configuration };
15+
use Koded\Stdlib\{Arguments, Config};
16+
use Koded\Stdlib\Interfaces\Configuration;
1617

1718
/**
1819
* Class ConfigFactory
1920
*
20-
* @property int $ttl Default Time-To-Live seconds for the cached items
21+
* @property int $ttl Default Time-To-Live seconds for the cached items
2122
* @property string $keyRegex Regex for validating the cache item key
2223
*
2324
*/

Configuration/PredisConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Koded\Stdlib\Immutable;
1616
use Koded\Stdlib\Interfaces\Configuration;
1717

18-
final class PredisConfiguration extends Immutable implements Configuration
18+
final class PredisConfiguration extends Immutable implements Configuration
1919
{
2020

2121
public function getConnectionParams(): array

Configuration/RedisConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Koded\Caching\Configuration;
1414

15-
use Koded\Caching\{ Cache, CacheException };
15+
use Koded\Caching\{Cache, CacheException};
1616
use Koded\Stdlib\Immutable;
1717
use Koded\Stdlib\Interfaces\Configuration;
1818
use Redis;

0 commit comments

Comments
 (0)