Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static function make(string $type = 'file'): Cacheable
return new RedisStore();
case 'apcu':
return new ApcuStore();
case 'sentinel':
return new RedisSentinelStore();
}
return new FileStore();
}
Expand Down
105 changes: 105 additions & 0 deletions src/Cache/RedisSentinelStore.php
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
Copy link
Owner

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

{
/** @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;
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The 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

public function __construct(array $options = [])

}

/**
* 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() . '*');
}
}