Last active
April 30, 2017 14:19
-
-
Save angyvolin/435e5934e78f1f6ae921736f236c3dca to your computer and use it in GitHub Desktop.
Redis autocomplete using ZRANGEBYLEX
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
<?php | |
/** | |
* Redis autocomplete using ZRANGEBYLEX command. | |
* Based on https://gist.github.com/antirez/11126283 | |
* | |
* @see https://github.com/angyvolin/redis-autocomplete-demo for online demo. | |
*/ | |
require __DIR__.'/vendor/autoload.php'; | |
$client = new Predis\Client([ | |
'host' => '127.0.0.1', | |
'port' => 6379, | |
'database' => 15 | |
]); | |
$term = $_GET['term']; | |
$items = $client->zrangebylex('kernel', "[$term", "[$term\xff", ['LIMIT', '0', '10']); | |
$id = 0; | |
$result = []; | |
foreach ($items as $item) { | |
$result[] = ['id' => $id++, 'label' => $item]; | |
} | |
echo json_encode($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See redis-autocomplete-demo for online demo app deployed to Heroku