New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

jsidorenko/redcard

Autocomplete implementation using PHP and Redis
22
Install
composer require jsidorenko/redcard
Latest Version:v1.1
PHP:>=5.3.0
Older releases:5
License:MIT
Last Updated:Apr 4, 2016
Links: GitHub  ·  Packagist
Maintainer: jsidorenkko

RedCard Autocomplete

Autocomplete implementation using PHP+Redis.

Inspired by https://github.com/seatgeek/soulmate

This library handles a basic implementation of autocomplete with sorted results (according to "scores") as well as arbitrary metadata for results. Also has the ability to separate different autocomplete databases in to "bins" (e.g. have separate bins "users" and another for "videos" so when querying against "users" it doesn't show results from "videos")

Installation

Add mochaka/redcard as a requirement to composer.json:

{
    "require": {
        "jsidorenko/redcard": "dev-master"
    }
}

Update your packages with composer update or install with composer install.

Integration

You will need to create a Predis Client Instance and provide it to the autocomplete class.

    $redis = new Predis\Client(array(
        'scheme' => 'tcp',
        'host'   => 'localhost',
        'port'   => 6379,
    ));

    $autocomplete = new JSidorenko\RedCard\RedisAutocomplete( $redis );
    or
    $autocomplete = new JSidorenko\RedCard\RedisAutocomplete( $redis, "yourDomainPrefix" );

    instead of "yourDomainPrefix" you can write something like "users" or "locations"

Basic Usage

To store data you must have a unique ID for an item and the phrase that should be searchable.


$autocomplete->store(2, "cat");
$autocomplete->store(3, "care");
$autocomplete->store("MYCRAZYID", "caress");
$autocomplete->store(55, "cars");
$autocomplete->store(6, "camera");

$results = $autocomplete->find("car");

var_dump($results)

Bins

Different types of data can be distinguished from one another through bins. Each bin has its own name and when searching and removing they will not conflict with one another.


$autocomplete->store(2, "Mary", "users");
$autocomplete->store(3, "Sally", "users");
$autocomplete->store(4, "Leo", "users" );

$autocomplete->store(5, "Mary Had A Litte Lamb", "blog-title");
$autocomplete->store(6, "Redis Rocks, A Life Story", "blog-title");

$results = $autocomplete->find("Mary", "users");

// Will only return Mary instead of "Mary Had A Litte Lamb"

Interface

The basic functions that you need to be aware of to utilize RedCard.

  • store: store a new item to autocomplete

        store($id, $phrase, $bin = '', $score = 1, $data = NULL)
    

    example

        $autocomplete->store('id123', "Clockwork Orange", "Books", 3, array('author'=>'Anthony Burgess'))
    
  • find: find an item. Searches are cached in a seperate hash.

        find($phrase, $bin = '', $count = 10, $isCaching = true)
    

    example

        $autocomplete->find("Clock", "Books", 1, true)
    
  • find by ID: find an item by provided ID. Returns null if nothing found.

        findByID($id, $bin = '')
    

    example

        $autocomplete->findByID('id123')
    
  • remove: remove an item from a bin. Searches are cached in a separate hash.

        remove($id, $bin = '')
    

    example

        $autocomplete->remove('id123', 'Books')
    
  • clear: clear all items.

        clear()
    

    example

        $autocomplete->clear()
    

License

RedCard Autocomplete is licensed under the MIT License. Original Copyright (c) 2011 Rishi Ishairzay, released under the MIT license

Related Packages

etrepat/laravel-compleet

Laravel integration for Compleet: A Redis-backed service for fast autocompleting

304 1
natsumework/redis-autocomplete

Provide an easy way for your laravel app to use redis to speed up autocomplete q...

9,149 3
adamcrampton/object-cache

Redis object cache for Laravel

1,287 0
byerikas/cache-tags

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag.

33,881 14
xtcat/laravel-redis-fallback

Laravel 5 redis cache fallback to file

23,721 5

Version History

Version Released PHP Laravel License
v1.1 >=5.3.0 MIT
v1.0 >=5.3.0 ~5 MIT