Skip to content

Commit

Permalink
Update: 适配 snowflake
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Mar 18, 2024
1 parent f69f72c commit 30af06f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/Components/snowflake/src/ImiRedisResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Imi\Snowflake;

use Godruoyi\Snowflake\SequenceResolver;
use Imi\Redis\Handler\IRedisHandler;
use Imi\Redis\RedisManager;

class ImiRedisResolver implements SequenceResolver
Expand Down Expand Up @@ -39,10 +40,7 @@ public function __construct(
public function sequence(int $currentTime)
{
$redis = RedisManager::getInstance($this->redisPool);
if (!$redis)
{
throw new \RuntimeException('Get redis instance failed');
}
/** @var $redis IRedisHandler */

return $redis->evalEx(static::SEQUENCE_LUA, [$this->prefix . $currentTime, 1, 1000], 1);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Components/snowflake/tests/Unit/SnowflakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public function testBasic(): void
$this->assertTrue(\strlen(SnowflakeUtil::id('testBasic')) <= 19);
}

public function testBasicByPredis(): void
{
$this->assertTrue(!empty(SnowflakeUtil::id('testBasicByPredis')));
$this->assertTrue(\strlen(SnowflakeUtil::id('testBasicByPredis')) <= 19);
}

public function testWorkIdAndDataCenterId(): void
{
$this->assertTrue(!empty(SnowflakeUtil::id('test1')));
Expand Down
41 changes: 24 additions & 17 deletions src/Components/snowflake/tests/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@
],
],
],
'predis_test' => [
'manager' => \Imi\ConnectionCenter\Handler\Singleton\SingletonConnectionManager::class,
'pool' => [
'maxResources' => 10,
'minResources' => 0,
],
'config' => [
'driver' => \Imi\Redis\Connector\RedisConnectionDriver::class,
'resources' => [
[
'host' => env('REDIS_SERVER_HOST', '127.0.0.1'),
'port' => env('REDIS_SERVER_PORT', 6379),
'password' => env('REDIS_SERVER_PASSWORD'),

'client' => 'predis',
'mode' => \Imi\Redis\Enum\RedisMode::Standalone,
],
],
],
],
],
// 连接池配置
'pools' => [
Expand All @@ -77,26 +97,13 @@
'defaultPool' => 'redis_test',
],
'beans' => [
'JWT' => [
'list' => [
'a' => [
'audience' => 'audience_a',
'subject' => 'subject_a',
'expires' => 86400,
'issuer' => 'issuer_a',
'headers' => [
'a' => '1',
'b' => '2',
],
'tokenHandler' => static fn () => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImEiOiIxIiwiYiI6IjIifQ.eyJhdWQiOiJhdWRpZW5jZV9hIiwic3ViIjoic3ViamVjdF9hIiwiZXhwIjoxODkzMjkxNTE1LCJpc3MiOiJpc3N1ZXJfYSIsIm5iZiI6MCwianRpIjoiIiwiaWF0IjoxNTc3OTMxNTE1LCJkYXRhIjp7Im1lbWJlcklkIjoxOTI2MDgxN319.-tXlyj1BcVD8GJIE2nQdTPULVpZFD0h5BIQdx_X943E',
'privateKey' => '123456',
'publicKey' => '123456',
],
],
],
'Snowflake' => [
'list' => [
'testBasic' => [
'redisPool' => 'redis_test',
],
'testBasicByPredis' => [
'redisPool' => 'predis_test',
],
'test1' => [
'datacenterId' => -1,
Expand Down

0 comments on commit 30af06f

Please sign in to comment.