|
| 1 | +// EXAMPLE: topk_tutorial |
| 2 | +<?php |
| 3 | + |
| 4 | +require 'vendor/autoload.php'; |
| 5 | + |
| 6 | +use Predis\Client as PredisClient; |
| 7 | + |
| 8 | +class DtTopkTest |
| 9 | +// REMOVE_START |
| 10 | +extends PredisTestCase |
| 11 | +// REMOVE_END |
| 12 | +{ |
| 13 | + public function testDtTopk() { |
| 14 | + $r = new PredisClient([ |
| 15 | + 'scheme' => 'tcp', |
| 16 | + 'host' => '127.0.0.1', |
| 17 | + 'port' => 6379, |
| 18 | + 'password' => '', |
| 19 | + 'database' => 0, |
| 20 | + ]); |
| 21 | + // REMOVE_START |
| 22 | + $r->flushall(); |
| 23 | + // REMOVE_END |
| 24 | + |
| 25 | + // STEP_START topk |
| 26 | + $res1 = $r->topkreserve('bikes:keywords', 5, 2000, 7, 0.925); |
| 27 | + echo $res1 . PHP_EOL; |
| 28 | + // >>> OK |
| 29 | + |
| 30 | + $res2 = $r->topkadd( |
| 31 | + 'bikes:keywords', |
| 32 | + 'store', |
| 33 | + 'seat', |
| 34 | + 'handlebars', |
| 35 | + 'handles', |
| 36 | + 'pedals', |
| 37 | + 'tires', |
| 38 | + 'store', |
| 39 | + 'seat', |
| 40 | + ); |
| 41 | + echo json_encode($res2) . PHP_EOL; |
| 42 | + // >>> [null,null,null,null,null,"handlebars",null,null] |
| 43 | + |
| 44 | + $res3 = $r->topklist('bikes:keywords'); |
| 45 | + echo json_encode($res3) . PHP_EOL; |
| 46 | + // >>> ["store","seat","pedals","tires","handles"] |
| 47 | + |
| 48 | + $res4 = $r->topkquery('bikes:keywords', 'store', 'handlebars'); |
| 49 | + echo json_encode($res4) . PHP_EOL; |
| 50 | + // >>> [1,0] |
| 51 | + // STEP_END |
| 52 | + // REMOVE_START |
| 53 | + $this->assertEquals('OK', $res1); |
| 54 | + $this->assertEquals([null, null, null, null, null, 'handlebars', null, null], $res2); |
| 55 | + $this->assertEquals(['store','seat','pedals','tires','handles'], $res3); |
| 56 | + $this->assertEquals([1,0], $res4); |
| 57 | + // REMOVE_END |
| 58 | + } |
| 59 | +} |
| 60 | + |
0 commit comments