diff --git a/README.md b/README.md index 2a5bcf9..f4506a9 100644 --- a/README.md +++ b/README.md @@ -17,24 +17,24 @@ $cache = new RobiNN\Cache\Cache([ // Available config options 'storage' => 'file', // apcu|file|memcached|redis 'file' => [ - 'path' => __DIR__.'/cache', - //'secret' => 'secret_key', // Optional, for securing file names - //'remove_expired' => true, // Optional, automatically remove all expired keys (it can affect performance) + 'path' => __DIR__.'/cache', // The path to the folder containing the cached content. + //'secret' => 'secret_key', // For securing file names (optional). + //'remove_expired' => true, // Automatically remove all expired keys (it can affect performance) (optional). ], 'redis' => [ - 'host' => '127.0.0.1', // Optional, when a path is specified - 'port' => 6379, // Optional, when the default port is used - //'database' => 0, // Optional - //'username' => '', // Optional, requires Redis >= 6.0 - //'password' => '', // Optional - //'path' => '/var/run/redis/redis-server.sock', // Optional + 'host' => '127.0.0.1', // Optional when a path is specified. + 'port' => 6379, // Optional when the default port is used. + //'database' => 0, // Default database (optional). + //'username' => '', // ACL - requires Redis >= 6.0 (optional). + //'password' => '', // Optional. + //'path' => '/var/run/redis/redis-server.sock', // Unix domain socket (optional). ], 'memcached' => [ - 'host' => '127.0.0.1', // Optional, when a path is specified - 'port' => 11211, // Optional, when the default port is used - //'path' => '/var/run/memcached/memcached.sock', // Optional - //'sasl_username' => '', // Optional, when not using SASL - //'sasl_password' => '', // Optional, when not using SASL + 'host' => '127.0.0.1', // Optional when a path is specified. + 'port' => 11211, // Optional when the default port is used. + //'path' => '/var/run/memcached/memcached.sock', // Unix domain socket (optional). + //'sasl_username' => '', // SASL auth (optional). + //'sasl_password' => '', // SASL auth (optional). ], ]); @@ -52,14 +52,14 @@ print_r($data); // item-value ## Methods -| Name | Type | Description | -|-------------|-------|-----------------------------| -| isConnected | bool | Check connection | -| exists | bool | Check if the data is cached | -| set | void | Save data to cache | -| get | mixed | Get data by key | -| delete | bool | Delete data by key | -| flush | void | Delete all data from cache | +| Name | Return | Description | +|-------------|--------|------------------------------| +| isConnected | bool | Check connection. | +| exists | bool | Check if the data is cached. | +| set | bool | Save data to cache. | +| get | mixed | Get data by key. | +| delete | bool | Delete data by key. | +| flush | bool | Delete all data from cache. | ## Requirements diff --git a/src/Cache.php b/src/Cache.php index 8ab5dbc..ca20367 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -32,9 +32,10 @@ public function __construct(array $config = [], array $custom_storages = []) { ], $custom_storages); $storage = isset($storages[$config['storage']]) ? $config['storage'] : 'file'; - $server_info = $config[$config['storage']] ?? []; - $cache_class = new ($storages[$storage])($server_info); - $this->cache = $cache_class instanceof CacheInterface ? $cache_class : new Storages\FileStorage($server_info); + $server_info = $config[$storage] ?? []; + $this->cache = is_subclass_of($storages[$storage], CacheInterface::class) ? + new ($storages[$storage])($server_info) : + new Storages\FileStorage($server_info); } /** @@ -54,8 +55,8 @@ public function exists(string $key): bool { /** * Save data to cache. */ - public function set(string $key, mixed $data, int $seconds = 0): void { - $this->cache->set($key, $data, $seconds); + public function set(string $key, mixed $data, int $seconds = 0): bool { + return $this->cache->set($key, $data, $seconds); } /** diff --git a/src/CacheInterface.php b/src/CacheInterface.php index 77d39fd..5401098 100644 --- a/src/CacheInterface.php +++ b/src/CacheInterface.php @@ -17,7 +17,7 @@ public function isConnected(): bool; public function exists(string $key): bool; - public function set(string $key, mixed $data, int $seconds = 0): void; + public function set(string $key, mixed $data, int $seconds = 0): bool; public function get(string $key): mixed; diff --git a/src/Storages/APCuStorage.php b/src/Storages/APCuStorage.php index 8607065..24ff6b2 100644 --- a/src/Storages/APCuStorage.php +++ b/src/Storages/APCuStorage.php @@ -33,8 +33,8 @@ public function exists(string $key): bool { return apcu_exists($key); } - public function set(string $key, mixed $data, int $seconds = 0): void { - apcu_store($key, serialize($data), $seconds); + public function set(string $key, mixed $data, int $seconds = 0): bool { + return apcu_store($key, serialize($data), $seconds); } public function get(string $key): mixed { diff --git a/src/Storages/FileStorage.php b/src/Storages/FileStorage.php index 06ad34f..5b39647 100644 --- a/src/Storages/FileStorage.php +++ b/src/Storages/FileStorage.php @@ -52,7 +52,7 @@ public function exists(string $key): bool { return is_file($this->getFileName($key)) && !$this->isExpired($key); } - public function set(string $key, mixed $data, int $seconds = 0): void { + public function set(string $key, mixed $data, int $seconds = 0): bool { $file = $this->getFileName($key); try { @@ -64,9 +64,13 @@ public function set(string $key, mixed $data, int $seconds = 0): void { if (@file_put_contents($file, $json, LOCK_EX) === strlen((string) $json)) { @chmod($file, 0777); + + return true; } - } catch (JsonException $e) { - echo $e->getMessage(); + + return false; + } catch (JsonException) { + return false; } } diff --git a/src/Storages/MemcachedStorage.php b/src/Storages/MemcachedStorage.php index 96a2bf3..b1cbe19 100644 --- a/src/Storages/MemcachedStorage.php +++ b/src/Storages/MemcachedStorage.php @@ -43,14 +43,9 @@ public function __construct(array $server) { } if (isset($server['path'])) { - $memcached_server = $server['path']; - $this->memcached->addServer($server['path'], 0); } else { $server['port'] ??= 11211; - - $memcached_server = $server['host'].':'.$server['port']; - $this->memcached->addServer($server['host'], (int) $server['port']); } @@ -71,7 +66,8 @@ public function __construct(array $server) { } if (!$this->connection) { - throw new CacheException(sprintf('Failed to connect to Memcached server (%s).', $memcached_server)); + $connection = $server['path'] ?? $server['host'].':'.$server['port']; + throw new CacheException(sprintf('Failed to connect to Memcached server %s.', $connection)); } } @@ -83,12 +79,12 @@ public function exists(string $key): bool { return (bool) $this->memcached->get($key); } - public function set(string $key, mixed $data, int $seconds = 0): void { + public function set(string $key, mixed $data, int $seconds = 0): bool { if ($this->is_memcached) { - $this->memcached->set($key, serialize($data), $seconds); - } else { - $this->memcached->set($key, serialize($data), 0, $seconds); + return $this->memcached->set($key, serialize($data), $seconds); } + + return $this->memcached->set($key, serialize($data), 0, $seconds); } public function get(string $key): mixed { diff --git a/src/Storages/RedisStorage.php b/src/Storages/RedisStorage.php index 57df98b..3c4b9a7 100644 --- a/src/Storages/RedisStorage.php +++ b/src/Storages/RedisStorage.php @@ -34,13 +34,7 @@ public function __construct(array $server) { throw new CacheException('Redis extension is not installed.'); } - if (isset($server['path'])) { - $redis_server = $server['path']; - } else { - $server['port'] ??= 6379; - - $redis_server = $server['host'].':'.$server['port']; - } + $server['port'] ??= 6379; try { if (isset($server['path'])) { @@ -48,14 +42,7 @@ public function __construct(array $server) { } else { $this->redis->connect($server['host'], (int) $server['port'], 3); } - } catch (RedisException $e) { - $this->connection = false; - throw new CacheException( - sprintf('Failed to connect to Redis server (%s). Error: %s', $redis_server, $e->getMessage()) - ); - } - try { if (isset($server['password'])) { if (isset($server['username'])) { $credentials = [$server['username'], $server['password']]; @@ -65,20 +52,11 @@ public function __construct(array $server) { $this->redis->auth($credentials); } - } catch (RedisException $e) { - throw new CacheException( - sprintf('Could not authenticate with Redis server (%s). Error: %s', $redis_server, $e->getMessage()) - ); - } - - try { - $server['database'] ??= 0; - $this->redis->select($server['database']); + $this->redis->select($server['database'] ?? 0); } catch (RedisException $e) { - throw new CacheException( - sprintf('Could not select Redis database (%s). Error: %s', $redis_server, $e->getMessage()) - ); + $connection = $server['path'] ?? $server['host'].':'.$server['port']; + throw new CacheException($e->getMessage().' ['.$connection.']'); } } @@ -94,14 +72,15 @@ public function exists(string $key): bool { } } - public function set(string $key, mixed $data, int $seconds = 0): void { + public function set(string $key, mixed $data, int $seconds = 0): bool { try { if ($seconds > 0) { - $this->redis->setex($key, $seconds, serialize($data)); - } else { - $this->redis->set($key, serialize($data)); + return $this->redis->setex($key, $seconds, serialize($data)); } + + return $this->redis->set($key, serialize($data)); } catch (RedisException) { + return false; } } diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 3739f95..7a23452 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -26,7 +26,7 @@ public function testSetterGetter(): void { $key = 'cache-test-setter-getter'; $data = 'itemvalue'; - $this->cache->set($key, $data); + $this->assertTrue($this->cache->set($key, $data)); $this->assertTrue($this->cache->exists($key));