Skip to content
Merged
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
87 changes: 27 additions & 60 deletions src/SequenceResolvers/CacheSequenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,48 @@
use Glhd\Bits\Contracts\ResolvesSequences;
use Illuminate\Cache\RedisStore;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Redis\Connections\PhpRedisConnection;
use Redis;
use Illuminate\Redis\Connections\PredisConnection;

class CacheSequenceResolver implements ResolvesSequences
{
public function __construct(
protected Repository $cache,
) {
}

public function next(int $timestamp): int
{
$key = "glhd-bits-seq:{$timestamp}";

$this->withoutSerializationOrCompression(
fn() => $this->cache->add($key, 0, now()->addSeconds(10)),
);


if ($this->cache->getStore() instanceof RedisStore) {
return $this->redisSafeIncrement($key, ttl: 10);
}

$this->cache->add($key, 0, now()->addSeconds(10));

return $this->cache->increment($key) - 1;
}

protected function withoutSerializationOrCompression(callable $callback)
protected function redisSafeIncrement(string $key, int $ttl): int
{
// This is a copied from `RateLimiter::withoutSerializationOrCompression` and
// `PacksPhpRedisValues::withoutSerializationOrCompression` for backwards-compatibility
// reasons (the feature wasn't added until Laravel 11.41.0).

$store = $this->cache->getStore();

if (! $store instanceof RedisStore) {
return $callback();
}

$key = $store->getPrefix().$key;

$script = <<<LUA
local ttl = tonumber(ARGV[1])
local value = redis.call('INCR', KEYS[1])
if value == 1 then
redis.call('EXPIRE', KEYS[1], ttl)
end
return value - 1
LUA;

$connection = $store->connection();

if (! $connection instanceof PhpRedisConnection) {
return $callback();
if ($connection instanceof PredisConnection) {
return (int) $connection->client()->eval($script, 1, $key, $ttl);
}

$client = $connection->client();

$old_serializer = null;
if ($this->serialized($client)) {
$old_serializer = $client->getOption($client::OPT_SERIALIZER);
$client->setOption($client::OPT_SERIALIZER, $client::SERIALIZER_NONE);
}

$old_compressor = null;
if ($this->compressed($client)) {
$old_compressor = $client->getOption($client::OPT_COMPRESSION);
$client->setOption($client::OPT_COMPRESSION, $client::COMPRESSION_NONE);
}

try {
return $callback();
} finally {
if (null !== $old_serializer) {
$client->setOption($client::OPT_SERIALIZER, $old_serializer);
}

if (null !== $old_compressor) {
$client->setOption($client::OPT_COMPRESSION, $old_compressor);
}
}
}

public function serialized(Redis $client): bool
{
return defined('Redis::OPT_SERIALIZER')
&& $client->getOption(Redis::OPT_SERIALIZER) !== Redis::SERIALIZER_NONE;
}

public function compressed(Redis $client): bool
{
return defined('Redis::OPT_COMPRESSION')
&& $client->getOption(Redis::OPT_COMPRESSION) !== Redis::COMPRESSION_NONE;

return (int) $connection->client()->eval($script, [$key, $ttl], 1);
}
}