This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -4,5 +4,21 @@ namespace Illuminate\Redis\Connections;
class PhpRedisClusterConnection extends PhpRedisConnection
{
//
/**
* Flush the selected Redis database on all master nodes.
*
* @return mixed
*/
public function flushdb()
{
$arguments = func_get_args();
$async = strtoupper((string) ($arguments[0] ?? null)) === 'ASYNC';
foreach ($this->client->_masters() as $master) {
$async
? $this->command('rawCommand', [$master, 'flushdb', 'async'])
: $this->command('flushdb', [$master]);
}
}
}

View File

@@ -7,7 +7,6 @@ use Illuminate\Contracts\Redis\Connection as ConnectionContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Redis;
use RedisCluster;
use RedisException;
/**
@@ -15,6 +14,8 @@ use RedisException;
*/
class PhpRedisConnection extends Connection implements ConnectionContract
{
use PacksPhpRedisValues;
/**
* The connection creation callback.
*
@@ -71,7 +72,7 @@ class PhpRedisConnection extends Connection implements ConnectionContract
}
/**
* Set the string value in argument as value of the key.
* Set the string value in the argument as the value of the key.
*
* @param string $key
* @param mixed $value
@@ -220,7 +221,7 @@ class PhpRedisConnection extends Connection implements ConnectionContract
$options = [];
foreach (array_slice($dictionary, 0, 3) as $i => $value) {
if (in_array($value, ['nx', 'xx', 'ch', 'incr', 'NX', 'XX', 'CH', 'INCR'], true)) {
if (in_array($value, ['nx', 'xx', 'ch', 'incr', 'gt', 'lt', 'NX', 'XX', 'CH', 'INCR', 'GT', 'LT'], true)) {
$options[] = $value;
unset($dictionary[$i]);
@@ -493,23 +494,17 @@ class PhpRedisConnection extends Connection implements ConnectionContract
/**
* Flush the selected Redis database.
*
* @return void
* @return mixed
*/
public function flushdb()
{
if (! $this->client instanceof RedisCluster) {
return $this->command('flushdb');
$arguments = func_get_args();
if (strtoupper((string) ($arguments[0] ?? null)) === 'ASYNC') {
return $this->command('flushdb', [true]);
}
foreach ($this->client->_masters() as [$host, $port]) {
$redis = tap(new Redis)->connect($host, $port);
if (isset($this->config['password']) && ! empty($this->config['password'])) {
$redis->auth($this->config['password']);
}
$redis->flushDb();
}
return $this->command('flushdb');
}
/**
@@ -556,7 +551,7 @@ class PhpRedisConnection extends Connection implements ConnectionContract
}
/**
* Apply prefix to the given key if necessary.
* Apply a prefix to the given key if necessary.
*
* @param string $key
* @return string

View File

@@ -2,7 +2,19 @@
namespace Illuminate\Redis\Connections;
use Predis\Command\ServerFlushDatabase;
class PredisClusterConnection extends PredisConnection
{
//
/**
* Flush the selected Redis database on all cluster nodes.
*
* @return void
*/
public function flushdb()
{
$this->client->executeCommandOnNodes(
tap(new ServerFlushDatabase)->setArguments(func_get_args())
);
}
}

View File

@@ -4,8 +4,6 @@ namespace Illuminate\Redis\Connections;
use Closure;
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
use Predis\Command\ServerFlushDatabase;
use Predis\Connection\Aggregate\ClusterInterface;
/**
* @mixin \Predis\Client
@@ -52,20 +50,4 @@ class PredisConnection extends Connection implements ConnectionContract
unset($loop);
}
/**
* Flush the selected Redis database.
*
* @return void
*/
public function flushdb()
{
if (! $this->client->getConnection() instanceof ClusterInterface) {
return $this->command('flushdb');
}
foreach ($this->getConnection() as $node) {
$node->executeCommand(new ServerFlushDatabase);
}
}
}

View File

@@ -50,7 +50,7 @@ class PhpRedisConnector implements Connector
}
/**
* Build a single cluster seed string from array.
* Build a single cluster seed string from an array.
*
* @param array $server
* @return string
@@ -102,6 +102,22 @@ class PhpRedisConnector implements Connector
if (! empty($config['scan'])) {
$client->setOption(Redis::OPT_SCAN, $config['scan']);
}
if (! empty($config['name'])) {
$client->client('SETNAME', $config['name']);
}
if (array_key_exists('serializer', $config)) {
$client->setOption(Redis::OPT_SERIALIZER, $config['serializer']);
}
if (array_key_exists('compression', $config)) {
$client->setOption(Redis::OPT_COMPRESSION, $config['compression']);
}
if (array_key_exists('compression_level', $config)) {
$client->setOption(Redis::OPT_COMPRESSION_LEVEL, $config['compression_level']);
}
});
}
@@ -176,6 +192,22 @@ class PhpRedisConnector implements Connector
if (! empty($options['failover'])) {
$client->setOption(RedisCluster::OPT_SLAVE_FAILOVER, $options['failover']);
}
if (! empty($options['name'])) {
$client->client('SETNAME', $options['name']);
}
if (array_key_exists('serializer', $options)) {
$client->setOption(RedisCluster::OPT_SERIALIZER, $options['serializer']);
}
if (array_key_exists('compression', $options)) {
$client->setOption(RedisCluster::OPT_COMPRESSION, $options['compression']);
}
if (array_key_exists('compression_level', $options)) {
$client->setOption(RedisCluster::OPT_COMPRESSION_LEVEL, $options['compression_level']);
}
});
}

View File

@@ -23,6 +23,10 @@ class PredisConnector implements Connector
['timeout' => 10.0], $options, Arr::pull($config, 'options', [])
);
if (isset($config['prefix'])) {
$formattedOptions['prefix'] = $config['prefix'];
}
return new PredisConnection(new Client($config, $formattedOptions));
}
@@ -38,6 +42,10 @@ class PredisConnector implements Connector
{
$clusterSpecificOptions = Arr::pull($config, 'options', []);
if (isset($config['prefix'])) {
$clusterSpecificOptions['prefix'] = $config['prefix'];
}
return new PredisClusterConnection(new Client(array_values($config), array_merge(
$options, $clusterOptions, $clusterSpecificOptions
)));

View File

@@ -2,9 +2,9 @@
namespace Illuminate\Redis\Limiters;
use Exception;
use Illuminate\Contracts\Redis\LimiterTimeoutException;
use Illuminate\Support\Str;
use Throwable;
class ConcurrencyLimiter
{
@@ -61,7 +61,7 @@ class ConcurrencyLimiter
* @return bool
*
* @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
* @throws \Exception
* @throws \Throwable
*/
public function block($timeout, $callback = null)
{
@@ -82,7 +82,7 @@ class ConcurrencyLimiter
return tap($callback(), function () use ($slot, $id) {
$this->release($slot, $id);
});
} catch (Exception $exception) {
} catch (Throwable $exception) {
$this->release($slot, $id);
throw $exception;

View File

@@ -58,7 +58,7 @@ class ConcurrencyLimiterBuilder
}
/**
* Set the maximum number of locks that can obtained per time window.
* Set the maximum number of locks that can be obtained per time window.
*
* @param int $maxLocks
* @return $this

View File

@@ -111,6 +111,30 @@ class DurationLimiter
return (bool) $results[0];
}
/**
* Determine if the key has been "accessed" too many times.
*
* @return bool
*/
public function tooManyAttempts()
{
[$this->decaysAt, $this->remaining] = $this->redis->eval(
$this->tooManyAttemptsLuaScript(), 1, $this->name, microtime(true), time(), $this->decay, $this->maxLocks
);
return $this->remaining <= 0;
}
/**
* Clear the limiter.
*
* @return void
*/
public function clear()
{
$this->redis->del($this->name);
}
/**
* Get the Lua script for acquiring a lock.
*
@@ -143,6 +167,36 @@ if ARGV[1] >= redis.call('HGET', KEYS[1], 'start') and ARGV[1] <= redis.call('HG
end
return {reset(), ARGV[2] + ARGV[3], ARGV[4] - 1}
LUA;
}
/**
* Get the Lua script to determine if the key has been "accessed" too many times.
*
* KEYS[1] - The limiter name
* ARGV[1] - Current time in microseconds
* ARGV[2] - Current time in seconds
* ARGV[3] - Duration of the bucket
* ARGV[4] - Allowed number of tasks
*
* @return string
*/
protected function tooManyAttemptsLuaScript()
{
return <<<'LUA'
if redis.call('EXISTS', KEYS[1]) == 0 then
return {0, ARGV[2] + ARGV[3]}
end
if ARGV[1] >= redis.call('HGET', KEYS[1], 'start') and ARGV[1] <= redis.call('HGET', KEYS[1], 'end') then
return {
redis.call('HGET', KEYS[1], 'end'),
ARGV[4] - redis.call('HGET', KEYS[1], 'count')
}
end
return {0, ARGV[2] + ARGV[3]}
LUA;
}
}

View File

@@ -24,7 +24,7 @@ class DurationLimiterBuilder
public $name;
/**
* The maximum number of locks that can obtained per time window.
* The maximum number of locks that can be obtained per time window.
*
* @var int
*/
@@ -58,7 +58,7 @@ class DurationLimiterBuilder
}
/**
* Set the maximum number of locks that can obtained per time window.
* Set the maximum number of locks that can be obtained per time window.
*
* @param int $maxLocks
* @return $this

View File

@@ -7,6 +7,7 @@ use Illuminate\Contracts\Redis\Factory;
use Illuminate\Redis\Connections\Connection;
use Illuminate\Redis\Connectors\PhpRedisConnector;
use Illuminate\Redis\Connectors\PredisConnector;
use Illuminate\Support\Arr;
use Illuminate\Support\ConfigurationUrlParser;
use InvalidArgumentException;
@@ -108,7 +109,7 @@ class RedisManager implements Factory
if (isset($this->config[$name])) {
return $this->connector()->connect(
$this->parseConnectionConfiguration($this->config[$name]),
$options
array_merge(Arr::except($options, 'parameters'), ['parameters' => Arr::get($options, 'parameters.'.$name, Arr::get($options, 'parameters', []))])
);
}
@@ -192,7 +193,7 @@ class RedisManager implements Factory
}
return array_filter($parsed, function ($key) {
return ! in_array($key, ['driver', 'username'], true);
return ! in_array($key, ['driver'], true);
}, ARRAY_FILTER_USE_KEY);
}
@@ -237,6 +238,19 @@ class RedisManager implements Factory
$this->driver = $driver;
}
/**
* Disconnect the given connection and remove from local cache.
*
* @param string|null $name
* @return void
*/
public function purge($name = null)
{
$name = $name ?: 'default';
unset($this->connections[$name]);
}
/**
* Register a custom driver creator Closure.
*

View File

12
vendor/laravel/framework/src/Illuminate/Redis/composer.json vendored Normal file → Executable file
View File

@@ -14,9 +14,11 @@
}
],
"require": {
"php": "^7.2.5|^8.0",
"illuminate/contracts": "^7.0",
"illuminate/support": "^7.0"
"php": "^7.3|^8.0",
"illuminate/collections": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/macroable": "^8.0",
"illuminate/support": "^8.0"
},
"autoload": {
"psr-4": {
@@ -25,11 +27,11 @@
},
"suggest": {
"ext-redis": "Required to use the phpredis connector (^4.0|^5.0).",
"predis/predis": "Required to use the predis connector (^1.1.2)."
"predis/predis": "Required to use the predis connector (^1.1.9)."
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
"dev-master": "8.x-dev"
}
},
"config": {