-
-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add sentinel support #399
base: main
Are you sure you want to change the base?
add sentinel support #399
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,105 @@ | ||||
<?php | ||||
|
||||
namespace TusPhp\Cache; | ||||
|
||||
use Carbon\Carbon; | ||||
use TusPhp\Config; | ||||
use Predis\Client as RedisClient; | ||||
|
||||
class RedisSentinelStore extends AbstractCache | ||||
{ | ||||
/** @var RedisClient */ | ||||
protected $redis; | ||||
|
||||
/** | ||||
* RedisStore constructor. | ||||
* | ||||
* @param array $options | ||||
*/ | ||||
public function __construct(array $params = [], array $options = []) | ||||
{ | ||||
$options = empty($options) ? Config::get('sentinel.options') : $options; | ||||
$params = empty($params) ? Config::get('sentinel.params') : $params; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add an example of how sentinel options/params looks like, probably in the README? |
||||
|
||||
$this->redis = new RedisClient($params, $options); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we only need to pass missing option in RedisClient, then we can just update RedisStore to accept second parameter instead of writing a new implementation. This should not break backwards compatibility. public function __construct(array $options = [], array $params = []) instead of tus-php/src/Cache/RedisStore.php Line 19 in 2067142
|
||||
} | ||||
|
||||
/** | ||||
* Get redis. | ||||
* | ||||
* @return RedisClient | ||||
*/ | ||||
public function getRedis(): RedisClient | ||||
{ | ||||
return $this->redis; | ||||
} | ||||
|
||||
/** | ||||
* {@inheritDoc} | ||||
*/ | ||||
public function get(string $key, bool $withExpired = false) | ||||
{ | ||||
$prefix = $this->getPrefix(); | ||||
|
||||
if (false === strpos($key, $prefix)) { | ||||
$key = $prefix . $key; | ||||
} | ||||
|
||||
$contents = $this->redis->get($key); | ||||
if (null !== $contents) { | ||||
$contents = json_decode($contents, true); | ||||
} | ||||
|
||||
if ($withExpired) { | ||||
return $contents; | ||||
} | ||||
|
||||
if ( ! $contents) { | ||||
return null; | ||||
} | ||||
|
||||
$isExpired = Carbon::parse($contents['expires_at'])->lt(Carbon::now()); | ||||
|
||||
return $isExpired ? null : $contents; | ||||
} | ||||
|
||||
/** | ||||
* {@inheritDoc} | ||||
*/ | ||||
public function set(string $key, $value) | ||||
{ | ||||
$contents = $this->get($key) ?? []; | ||||
|
||||
if (\is_array($value)) { | ||||
$contents = $value + $contents; | ||||
} else { | ||||
$contents[] = $value; | ||||
} | ||||
|
||||
$status = $this->redis->set($this->getPrefix() . $key, json_encode($contents)); | ||||
|
||||
return 'OK' === $status->getPayload(); | ||||
} | ||||
|
||||
/** | ||||
* {@inheritDoc} | ||||
*/ | ||||
public function delete(string $key): bool | ||||
{ | ||||
$prefix = $this->getPrefix(); | ||||
|
||||
if (false === strpos($key, $prefix)) { | ||||
$key = $prefix . $key; | ||||
} | ||||
|
||||
return $this->redis->del([$key]) > 0; | ||||
} | ||||
|
||||
/** | ||||
* {@inheritDoc} | ||||
*/ | ||||
public function keys(): array | ||||
{ | ||||
return $this->redis->keys($this->getPrefix() . '*'); | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this could directly extend
RedisStore