Skip to content

Commit

Permalink
feature: add Redis#getPersistentId
Browse files Browse the repository at this point in the history
Since we removed the magic which automagically set the persistent id to the options, this method is to provide the persistent id in case we connected to redis using the persistent flag.

If the persistent ID was provided via config, the ID from the config is returned. Otherwise, we return the persistent ID from `ext-redis`.
In case `update` flag is provided, the persistent ID is always returned from `ext-redis`.

Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Jun 21, 2024
1 parent 58c225e commit 640af62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ public function getOptions(): RedisOptions
return $this->options;
}

/**
* @psalm-api
* @return non-empty-string|null
*/
public function getPersistentId(bool $update = false): ?string
{
$options = $this->getOptions();

if (! $options->isPersistent()) {
return null;
}

if ($update === false) {
$persistentId = $options->getPersistentId();
if ($persistentId !== null) {
return $persistentId;
}
}

$persistentId = $this->getRedisResource()->getPersistentID();
if (! is_string($persistentId) || $persistentId === '') {
return null;
}

return $persistentId;
}

/**
* {@inheritDoc}
*/
Expand Down
4 changes: 4 additions & 0 deletions src/RedisOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ final class RedisOptions extends AdapterOptions
* The namespace separator
*/
protected string $namespaceSeparator = ':';
/** @var non-empty-string|null */
protected string|null $persistentId;

protected string $redisVersion = '';
Expand Down Expand Up @@ -128,6 +129,9 @@ public function getNamespaceSeparator(): string
return $this->namespaceSeparator;
}

/**
* @return non-empty-string|null
*/
public function getPersistentId(): string|null
{
return $this->persistentId;
Expand Down

0 comments on commit 640af62

Please sign in to comment.