Skip to content

Commit

Permalink
Merge pull request #23 from flownative/fix-73-compat
Browse files Browse the repository at this point in the history
Fix compatibility with PHP 7.4
  • Loading branch information
robertlemke authored Nov 23, 2022
2 parents b872b76 + eb0a6d1 commit 53450ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
20 changes: 12 additions & 8 deletions Classes/Command/RedisSentinelCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
use Predis\Command\Redis\INFO;
use Predis\Connection\Replication\SentinelReplication;

#[Flow\Scope("singleton")]
/**
* @Flow\Scope("singleton")
*/
class RedisSentinelCommandController extends CommandController
{
#[Flow\Inject]
protected CacheManager $cacheManager;
/**
* @Flow\Inject
*/
protected ?CacheManager $cacheManager;

/**
* List Redis Sentinel caches
Expand Down Expand Up @@ -175,13 +179,13 @@ public function connectCommand(string $cacheIdentifier): void
$this->outputLine('<error>X</error>');
$this->outputLine('<error>%s</error>', [$throwable->getMessage()]);

if (str_contains($throwable->getMessage(), 'NOAUTH')) {
if (str_contains($throwable->getMessage(), 'AUTH')) {
$usesPassword = empty(!isset($sentinelConnection) || $sentinelConnection->getParameters()->password);
if ($usesPassword) {
$this->outputLine('Note: There was <u>no Sentinel password</u> defined in the backend options of this cache backend');
$this->outputLine();
} else {
$this->outputLine('The connection failed even though there was a password defined in the backend options');
$this->outputLine('The connection failed even though there was a password defined in the backend options.');
}
}
exit(1);
Expand All @@ -196,13 +200,13 @@ public function connectCommand(string $cacheIdentifier): void
$this->outputLine('<error>X</error>');
$this->outputLine('<error>%s</error>', [$throwable->getMessage()]);

if (str_contains($throwable->getMessage(), 'NOAUTH')) {
if (str_contains($throwable->getMessage(), 'AUTH')) {
$usesPassword = (empty($backendConfiguration['backendOptions']['password']));
if ($usesPassword) {
$this->outputLine('Note: There was <u>no password</u> defined in the backend options of this cache backend');
$this->outputLine();
} else {
$this->outputLine('The connection failed even though there was a password defined in the backend options');
$this->outputLine('The connection failed even though there was a password defined in the backend options.');
}
}

Expand Down Expand Up @@ -297,7 +301,7 @@ private function getRedisClient(array $sentinels, string $password, string $serv
$options['replication'] = 'sentinel';
$options['service'] = $service;
} else {
$connectionParameters = 'tcp://' . $hostname . ':' . $port;
$connectionParameters = 'redis://' . $hostname . ':' . $port;
}
return new Client($connectionParameters, $options);
}
Expand Down
41 changes: 21 additions & 20 deletions Classes/RedisBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Predis\Collection\Iterator;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Throwable;

class RedisBackend extends IndependentAbstractBackend implements TaggableBackendInterface, IterableBackendInterface, FreezableBackendInterface, PhpCapableBackendInterface, WithStatusInterface
{
Expand Down Expand Up @@ -82,9 +83,9 @@ public function __construct(EnvironmentConfiguration $environmentConfiguration,
* @param string $entryIdentifier An identifier for this specific cache entry
* @param string $data The data to be stored
* @param array $tags Tags to associate with this cache entry. If the backend does not support tags, this option can be ignored.
* @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
* @param int|null $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
* @return void
* @throws RuntimeException
* @throws Throwable
* @api
*/
public function set(string $entryIdentifier, string $data, array $tags = [], int $lifetime = null): void
Expand All @@ -110,7 +111,7 @@ public function set(string $entryIdentifier, string $data, array $tags = [], int
$this->client->sAdd($this->getPrefixedIdentifier('tags:' . $entryIdentifier), [$tag]);
}
$this->client->exec();
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -126,7 +127,7 @@ public function get(string $entryIdentifier)
{
try {
return $this->decompress($this->client->get($this->getPrefixedIdentifier('entry:' . $entryIdentifier)));
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -142,7 +143,7 @@ public function has(string $entryIdentifier): bool
{
try {
return (bool)$this->client->exists($this->getPrefixedIdentifier('entry:' . $entryIdentifier));
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand Down Expand Up @@ -178,7 +179,7 @@ public function remove(string $entryIdentifier): bool
} while ($result === false);

return true;
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand Down Expand Up @@ -218,7 +219,7 @@ public function flush(): void
$this->getPrefixedIdentifier('')
);
$this->frozen = null;
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand Down Expand Up @@ -268,7 +269,7 @@ public function flushByTag(string $tag): int
$this->getPrefixedIdentifier('tag:' . $tag),
$this->getPrefixedIdentifier('')
);
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand Down Expand Up @@ -298,7 +299,7 @@ public function findIdentifiersByTag(string $tag): array
{
try {
return $this->client->sMembers($this->getPrefixedIdentifier('tag:' . $tag));
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -308,7 +309,7 @@ public function current()
{
try {
return $this->get(substr($this->getEntryKeyspaceIterator()->current(), $this->entryKeyspaceIteratorKeyPrefixLength));
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -318,7 +319,7 @@ public function next()
{
try {
$this->getEntryKeyspaceIterator()->next();
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -328,7 +329,7 @@ public function key()
{
try {
return substr($this->getEntryKeyspaceIterator()->current(), $this->entryKeyspaceIteratorKeyPrefixLength);
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -337,7 +338,7 @@ public function valid(): bool
{
try {
return $this->getEntryKeyspaceIterator()->valid();
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -347,7 +348,7 @@ public function rewind()
{
try {
$this->getEntryKeyspaceIterator()->rewind();
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand Down Expand Up @@ -383,7 +384,7 @@ public function freeze(): void
$result = $this->client->exec();
} while ($result === false);
$this->frozen = true;
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -400,7 +401,7 @@ public function isFrozen(): bool
$this->frozen = (bool)$this->client->exists($this->getPrefixedIdentifier('frozen'));
}
return $this->frozen;
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand Down Expand Up @@ -596,10 +597,10 @@ private function getRedisClient(): \Predis\Client
$options['replication'] = 'sentinel';
$options['service'] = $this->service;
} else {
$connectionParameters = 'tcp://' . $this->hostname . ':' . $this->port;
$connectionParameters = 'redis://' . $this->hostname . ':' . $this->port;
}
return new Predis\Client($connectionParameters, $options);
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
$this->handleThrowable($throwable);
}
}
Expand All @@ -614,9 +615,9 @@ private function getEntryKeyspaceIterator()
}

/**
* @throws \Throwable
* @throws Throwable
*/
private function handleThrowable(\Throwable $throwable): void
private function handleThrowable(Throwable $throwable): void
{
$messageHash = md5($throwable->getMessage());
if (!$this->deduplicateErrors || !array_key_exists($messageHash, static::$loggedErrors)) {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ This cache backend will log errors, such as connection timeouts or other
problems while communicating with the Redis servers.

If a connection error occurs during a request, it is likely, that more errors of
the same type will happen. Therfore, those messages will, by default, be
the same type will happen. Therefore, those messages will, by default, be
de-duplicated: If the messages of an error is identical with one which already
has been logged during the current CLI / web request, it will not be logged
another time.
Expand Down

0 comments on commit 53450ed

Please sign in to comment.