-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support predis * add test for bulk generate id with predis resolver * Update composer.json Co-authored-by: Bilge <[email protected]> * add new phpcsfix rule "heredoc_indentation" * chore: rename PHPREDIS -> Predis * chore: remove predis from composer * chore: update readme * chore: fix test * fix unit test ensure all cache are overdue --------- Co-authored-by: Bilge <[email protected]>
- Loading branch information
Showing
13 changed files
with
219 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the godruoyi/php-snowflake. | ||
* | ||
* (c) Godruoyi <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Godruoyi\Snowflake; | ||
|
||
use Predis\Client as PredisClient; | ||
|
||
class PredisSequenceResolver implements SequenceResolver | ||
{ | ||
/** | ||
* The cache prefix. | ||
*/ | ||
protected string $prefix = ''; | ||
|
||
/** | ||
* The default redis lua script | ||
*/ | ||
protected static string $script = <<<'LUA' | ||
if redis.call('set', KEYS[1], ARGV[1], "EX", ARGV[2], "NX") then | ||
return 0 | ||
else | ||
return redis.call('incr', KEYS[1]) | ||
end | ||
LUA; | ||
|
||
public function __construct(protected PredisClient $predisClient) | ||
{ | ||
} | ||
|
||
public function sequence(int $currentTime): int | ||
{ | ||
return $this->predisClient->eval(self::$script, 1, $this->prefix.$currentTime, '0', '10') | 0; | ||
} | ||
|
||
/** | ||
* Set cache prefix. | ||
*/ | ||
public function setCachePrefix(string $prefix): self | ||
{ | ||
$this->prefix = $prefix; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the godruoyi/php-snowflake. | ||
* | ||
* (c) Godruoyi <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
namespace Tests; | ||
|
||
use Godruoyi\Snowflake\PredisSequenceResolver; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use Predis\Client; | ||
use ReflectionException; | ||
|
||
class PredisSequenceResolverTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
if (! class_exists('Predis\\Client')) { | ||
$this->markTestSkipped('Predis extension is not installed'); | ||
} | ||
} | ||
|
||
/** | ||
* @throws ReflectionException | ||
* @throws Exception | ||
*/ | ||
public function test_set_cache_prefix(): void | ||
{ | ||
$redis = $this->createMock(Client::class); | ||
$snowflake = new PredisSequenceResolver($redis); | ||
$snowflake->setCachePrefix('foo'); | ||
|
||
$this->assertEquals('foo', $this->invokeProperty($snowflake, 'prefix')); | ||
} | ||
|
||
/** | ||
* Test order sequence | ||
* | ||
* @throws Exception | ||
*/ | ||
public function test_predis_sequence(): void | ||
{ | ||
$redis = $this->createMock(Client::class); | ||
$redis->expects($this->exactly(4)) | ||
->method('__call') | ||
->withAnyParameters() | ||
->willReturn(1, 2, 3, 4); | ||
|
||
$snowflake = new PredisSequenceResolver($redis); | ||
|
||
$this->assertEquals(1, $snowflake->sequence(1)); | ||
$this->assertEquals(2, $snowflake->sequence(1)); | ||
$this->assertEquals(3, $snowflake->sequence(1)); | ||
$this->assertEquals(4, $snowflake->sequence(1)); | ||
} | ||
|
||
public function test_real_redis_connect(): void | ||
{ | ||
if (! ($host = getenv('REDIS_HOST')) || ! ($port = getenv('REDIS_PORT'))) { | ||
$this->markTestSkipped('Redis host or port is not set, skip real redis test.'); | ||
} | ||
|
||
$client = new Client([ | ||
'scheme' => 'tcp', | ||
'host' => $host, | ||
'port' => $port | 0, | ||
]); | ||
|
||
$client->ping(); | ||
|
||
$randomKey = random_int(0, 99999); | ||
|
||
$redisResolver = new PredisSequenceResolver($client); | ||
|
||
$this->assertEquals(0, $redisResolver->sequence($randomKey)); | ||
$this->assertEquals(1, $redisResolver->sequence($randomKey)); | ||
$this->assertEquals(2, $redisResolver->sequence($randomKey)); | ||
$this->assertEquals(3, $redisResolver->sequence($randomKey)); | ||
|
||
sleep(11); | ||
|
||
$this->assertEquals(0, $redisResolver->sequence($randomKey)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters