Created
December 13, 2019 02:17
-
-
Save lesstif/99a65bc7058e0db1bdea85fd4c726d0e to your computer and use it in GitHub Desktop.
Laravel PHPRedis example for ordered set ranking
This file contains hidden or 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
<?php | |
namespace Tests\Feature; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Illuminate\Foundation\Testing\WithFaker; | |
use Tests\TestCase; | |
class RedisRankingTest extends TestCase | |
{ | |
private $users; | |
private $keyName = 'recently-keyword'; | |
private $redis; | |
private $userSize = 30; | |
protected function setUp(): void | |
{ | |
parent::setUp(); | |
$krFaker = \Faker\Factory::create('ko_KR'); | |
// 사용자 생성 | |
for($i = 0; $i < $this->userSize; $i++) { | |
$this->users[] = $krFaker->name; | |
} | |
$this->redis = new \Redis(); | |
$this->redis->connect(config('database.redis.default.host'), config('database.redis.default.port')); | |
// 기존 member 삭제 | |
$allMembers = $this->redis->zRange($this->keyName, 0, -1); | |
foreach ($allMembers as $member) { | |
$this->redis->zRem($this->keyName, $member); | |
} | |
} | |
protected function tearDown(): void | |
{ | |
for($i = 0; $i < $this->userSize; $i++) { | |
// test 데이타 삭제 | |
$this->redis->zRem($this->keyName, $this->users[$i]); | |
} | |
$this->redis->close(); | |
parent::tearDown(); | |
} | |
/** | |
* A basic feature test example. | |
* | |
* @return void | |
*/ | |
public function testExample() | |
{ | |
$key = $this->keyName; | |
for ($i = 0 ; $i < 10000; $i++) | |
{ | |
$n = mt_rand(0, count($this->users) - 1); | |
$e = $this->users[$n]; | |
$score = (int)mt_rand(1, 10); | |
$this->redis->zIncrBy($key, $score, $e); | |
} | |
$memeber = $this->users[0]; | |
$rank = $this->redis->zRank($key, $memeber); | |
$revRank = $this->redis->zRevRank($key, $memeber); | |
// with score | |
$range = $this->redis->zRange($key, 0, 15, true); | |
// 마지막 파라미터는 with score | |
// https://github.com/phpredis/phpredis#zrevrange | |
$revrange = $this->redis->zRevRange($key, 0, 15, true); | |
dump([ | |
'member' => $memeber, | |
'rank' => $rank, | |
'revRank' => $revRank, | |
'revrange' => $revrange, | |
'range' => $range, | |
]); | |
$this->assertTrue(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment