Skip to content

Commit

Permalink
v2.4.5, fixed return type in set()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNN1 committed Dec 19, 2022
1 parent 0bc3863 commit e79f1f4
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 74 deletions.
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
],
]);

Expand All @@ -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

Expand Down
11 changes: 6 additions & 5 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Storages/APCuStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions src/Storages/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/Storages/MemcachedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand All @@ -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));
}
}

Expand All @@ -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 {
Expand Down
39 changes: 9 additions & 30 deletions src/Storages/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,15 @@ 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'])) {
$this->redis->connect($server['path']);
} 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']];
Expand All @@ -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.']');
}
}

Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit e79f1f4

Please sign in to comment.