Skip to content

Instantly share code, notes, and snippets.

@angyvolin
Last active April 30, 2017 14:19
Show Gist options
  • Save angyvolin/435e5934e78f1f6ae921736f236c3dca to your computer and use it in GitHub Desktop.
Save angyvolin/435e5934e78f1f6ae921736f236c3dca to your computer and use it in GitHub Desktop.
Redis autocomplete using ZRANGEBYLEX
<?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);
@angyvolin
Copy link
Author

angyvolin commented Apr 30, 2017

See redis-autocomplete-demo for online demo app deployed to Heroku

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment