Skip to content

Commit

Permalink
Merge pull request #4 from iksaku/enhancement/support-connection-prefix
Browse files Browse the repository at this point in the history
enhancement: Support Redis Connection prefixes
  • Loading branch information
stevebauman authored Oct 8, 2024
2 parents 6af0a53 + 2fbaf53 commit bcc5375
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 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, predis ]
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ActiveRedis provides you simple and efficient way to interact with Redis hashes
## Requirements

- PHP >= 8.1
- Predis >= 2.0
- Redis >= 3.0
- Laravel >= 9.0

## Installation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"minimum-stability": "stable",
"require": {
"php": ">=8.1",
"predis/predis": "^2.0",
"illuminate/support": "^9.0|^10.0|^11.0",
"illuminate/contracts": "^9.0|^10.0|^11.0",
"illuminate/collections": "^9.0|^10.0|^11.0"
Expand All @@ -38,6 +37,7 @@
"brick/math": "For decimal cast support"
},
"require-dev": {
"predis/predis": "^2.0",
"laravel/pint": "^1.17",
"pestphp/pest": "^1.0|^2.0|^3.0",
"orchestra/testbench": "^7.0|^8.0|^9.0"
Expand Down
40 changes: 37 additions & 3 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 @@ -28,24 +31,55 @@ public function exists(string $hash): bool
*/
public function chunk(string $pattern, int $count): Generator
{
$cursor = null;
[$cursor, $prefix] = array_values($this->getScanParameters());

do {
[$cursor, $keys] = $this->redis->scan($cursor, [
'match' => $pattern,
'match' => $prefix.$pattern,
'count' => $count,
]);

if (is_null($keys)) {
return;
}

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

if (empty($prefix)) {
yield $keys;

continue;
}

yield array_map(function (string $key) use ($prefix) {
return Str::after($key, $prefix);
}, $keys);
} while ($cursor !== '0');
}

/**
* Get the scan parameters for the Redis connection.
*/
protected function getScanParameters(): array
{
return match (true) {
$this->redis instanceof PhpRedisConnection => [
'cursor' => null,
'prefix' => $this->redis->getOption($this->redis->client()::OPT_PREFIX),
],
$this->redis instanceof PredisConnection => [
'cursor' => 0,
'prefix' => $this->redis->getOptions()->prefix->getPrefix(),
],
default => [
'cursor' => null,
'prefix' => null,
]
};
}

/**
* Set the hash field's value.
*/
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 bcc5375

Please sign in to comment.