Skip to content

Commit

Permalink
enhancement: Support prefixes in Redis connections
Browse files Browse the repository at this point in the history
* Run tests targeting both phpredis and predis
  • Loading branch information
iksaku committed Oct 7, 2024
1 parent 6af0a53 commit 06060a3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ jobs:
os: [ ubuntu-latest ]
php: [ 8.3, 8.2, 8.1 ]
laravel: [ 11.*, 10.*, 9.* ]
redis-client: [ phpredis, redis ]
dependency-version: [ prefer-stable ]
include:
- redis-client: phpredis
php-extensions: redis
- laravel: 11.*
testbench: 9.*
- laravel: 10.*
Expand All @@ -36,7 +39,7 @@ jobs:
- 6379:6379
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3

name: ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
name: ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} (${{ matrix.redis-client }}) - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
Expand All @@ -52,6 +55,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ matrix.php-extensions }}

- name: Install dependencies
run: |
Expand All @@ -60,3 +64,5 @@ jobs:
- name: Execute tests
run: vendor/bin/pest
env:
REDIS_CLIENT: ${{ matrix.redis-client }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vendor
composer.lock
psalm.phar
.idea
.php_cs.cache
.phpunit.result.cache
.php-cs-fixer.cache
26 changes: 24 additions & 2 deletions src/Repositories/RedisRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Closure;
use Generator;
use Illuminate\Contracts\Redis\Connection;
use Illuminate\Redis\Connections\PhpRedisConnection;
use Illuminate\Redis\Connections\PredisConnection;
use Illuminate\Support\Str;

class RedisRepository implements Repository
{
Expand All @@ -30,6 +33,16 @@ public function chunk(string $pattern, int $count): Generator
{
$cursor = null;

$prefix = match (true) {
$this->redis instanceof PhpRedisConnection => $this->redis->getOption($this->redis->client()::OPT_PREFIX),
$this->redis instanceof PredisConnection => $this->redis->getOptions()->prefix->getPrefix(),
default => null
};

if (filled($prefix)) {
$pattern = "{$prefix}{$pattern}";
}

do {
[$cursor, $keys] = $this->redis->scan($cursor, [
'match' => $pattern,
Expand All @@ -40,9 +53,18 @@ public function chunk(string $pattern, int $count): Generator
return;
}

if (! empty($keys)) {
yield $keys;
if (empty($keys)) {
continue;
}

if (filled($prefix)) {
$keys = array_map(
fn (string $key) => Str::after($key, $prefix),
$keys
);
}

yield $keys;
} while ($cursor !== '0');
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ModelQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
expect($found->getKey())->toBe($model->getKey());
});

it('queries existing attributes', function () {
$model = ModelStub::create([
'name' => 'John Doe'
]);

$found = ModelStub::find($model->getKey());

expect($found)->is($model)->toBeTrue();
expect($found->getKey())->toBe($model->getKey());

expect($found->name)->toBe($model->name);
});

it('returns null when finding by null or empty key', function (?string $key) {
ModelStub::create();

Expand Down
8 changes: 0 additions & 8 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,4 @@ protected function getPackageProviders($app): array
{
return [ActiveRedisServiceProvider::class];
}

/**
* Define environment setup.
*/
protected function defineEnvironment($app): void
{
$app['config']->set('database.redis.options.prefix', null);
}
}

0 comments on commit 06060a3

Please sign in to comment.