From 10192d6c34b8043c7915a9bb9d98dccf28d926c1 Mon Sep 17 00:00:00 2001 From: rzvme Date: Fri, 7 Mar 2025 14:50:58 +0200 Subject: [PATCH 1/4] replace withoutSerializationOrCompression() with redisSafeIncrement() that uses a LUA script for Redis --- .../CacheSequenceResolver.php | 71 +++++-------------- 1 file changed, 19 insertions(+), 52 deletions(-) diff --git a/src/SequenceResolvers/CacheSequenceResolver.php b/src/SequenceResolvers/CacheSequenceResolver.php index 8b5d676..a2d5de9 100644 --- a/src/SequenceResolvers/CacheSequenceResolver.php +++ b/src/SequenceResolvers/CacheSequenceResolver.php @@ -5,8 +5,7 @@ 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 { @@ -19,67 +18,35 @@ 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(); + $key = $store->getPrefix().$key; - if (! $store instanceof RedisStore) { - return $callback(); - } + $script = <<connection(); - if (! $connection instanceof PhpRedisConnection) { - return $callback(); - } - - $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); - } + if ($connection instanceof PredisConnection) { + return (int) $connection->client()->eval($script, 1, $key, $ttl); } - } - 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); } } From 9789f2f11a7609b25e1a3908491a04927d4f851d Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 7 Mar 2025 09:26:47 -0500 Subject: [PATCH 2/4] Code style --- .../CacheSequenceResolver.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/SequenceResolvers/CacheSequenceResolver.php b/src/SequenceResolvers/CacheSequenceResolver.php index a2d5de9..d7344af 100644 --- a/src/SequenceResolvers/CacheSequenceResolver.php +++ b/src/SequenceResolvers/CacheSequenceResolver.php @@ -9,30 +9,30 @@ class CacheSequenceResolver implements ResolvesSequences { - public function __construct( - protected Repository $cache, - ) { - } + public function __construct( + protected Repository $cache, + ) { + } - public function next(int $timestamp): int - { - $key = "glhd-bits-seq:{$timestamp}"; + public function next(int $timestamp): int + { + $key = "glhd-bits-seq:{$timestamp}"; - if ($this->cache->getStore() instanceof RedisStore) { - return $this->redisSafeIncrement($key, ttl: 10); - } + if ($this->cache->getStore() instanceof RedisStore) { + return $this->redisSafeIncrement($key, ttl: 10); + } - $this->cache->add($key, 0, now()->addSeconds(10)); + $this->cache->add($key, 0, now()->addSeconds(10)); - return $this->cache->increment($key) - 1; - } + return $this->cache->increment($key) - 1; + } - protected function redisSafeIncrement(string $key, int $ttl): int - { - $store = $this->cache->getStore(); - $key = $store->getPrefix().$key; + protected function redisSafeIncrement(string $key, int $ttl): int + { + $store = $this->cache->getStore(); + $key = $store->getPrefix().$key; - $script = <<connection(); + $connection = $store->connection(); - if ($connection instanceof PredisConnection) { - return (int) $connection->client()->eval($script, 1, $key, $ttl); - } + if ($connection instanceof PredisConnection) { + return (int) $connection->client()->eval($script, 1, $key, $ttl); + } - return (int) $connection->client()->eval($script, [$key, $ttl], 1); - } + return (int) $connection->client()->eval($script, [$key, $ttl], 1); + } } From f1e3eb5ef91b78b6d6c0c92d0090947f14530571 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 7 Mar 2025 09:27:24 -0500 Subject: [PATCH 3/4] Whoops, wrong preset --- .../CacheSequenceResolver.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/SequenceResolvers/CacheSequenceResolver.php b/src/SequenceResolvers/CacheSequenceResolver.php index d7344af..a2d5de9 100644 --- a/src/SequenceResolvers/CacheSequenceResolver.php +++ b/src/SequenceResolvers/CacheSequenceResolver.php @@ -9,30 +9,30 @@ class CacheSequenceResolver implements ResolvesSequences { - public function __construct( - protected Repository $cache, - ) { - } + public function __construct( + protected Repository $cache, + ) { + } - public function next(int $timestamp): int - { - $key = "glhd-bits-seq:{$timestamp}"; + public function next(int $timestamp): int + { + $key = "glhd-bits-seq:{$timestamp}"; - if ($this->cache->getStore() instanceof RedisStore) { - return $this->redisSafeIncrement($key, ttl: 10); - } + if ($this->cache->getStore() instanceof RedisStore) { + return $this->redisSafeIncrement($key, ttl: 10); + } - $this->cache->add($key, 0, now()->addSeconds(10)); + $this->cache->add($key, 0, now()->addSeconds(10)); - return $this->cache->increment($key) - 1; - } + return $this->cache->increment($key) - 1; + } - protected function redisSafeIncrement(string $key, int $ttl): int - { - $store = $this->cache->getStore(); - $key = $store->getPrefix().$key; + protected function redisSafeIncrement(string $key, int $ttl): int + { + $store = $this->cache->getStore(); + $key = $store->getPrefix().$key; - $script = <<connection(); + $connection = $store->connection(); - if ($connection instanceof PredisConnection) { - return (int) $connection->client()->eval($script, 1, $key, $ttl); - } + if ($connection instanceof PredisConnection) { + return (int) $connection->client()->eval($script, 1, $key, $ttl); + } - return (int) $connection->client()->eval($script, [$key, $ttl], 1); - } + return (int) $connection->client()->eval($script, [$key, $ttl], 1); + } } From 44693a1b19564d74169adb04242a67a20fe39e4c Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 7 Mar 2025 09:29:34 -0500 Subject: [PATCH 4/4] Mess with indent --- .../CacheSequenceResolver.php | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/SequenceResolvers/CacheSequenceResolver.php b/src/SequenceResolvers/CacheSequenceResolver.php index a2d5de9..eff6f41 100644 --- a/src/SequenceResolvers/CacheSequenceResolver.php +++ b/src/SequenceResolvers/CacheSequenceResolver.php @@ -13,40 +13,40 @@ public function __construct( protected Repository $cache, ) { } - + public function next(int $timestamp): int { $key = "glhd-bits-seq:{$timestamp}"; - + 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 redisSafeIncrement(string $key, int $ttl): int { $store = $this->cache->getStore(); $key = $store->getPrefix().$key; - + $script = <<connection(); - + if ($connection instanceof PredisConnection) { return (int) $connection->client()->eval($script, 1, $key, $ttl); } - + return (int) $connection->client()->eval($script, [$key, $ttl], 1); } }