natsumework/redis-autocomplete 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

natsumework/redis-autocomplete

最新稳定版本:1.1.0

Composer 安装命令:

composer require natsumework/redis-autocomplete

包简介

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

README 文档

README

Latest Version on Packagist Test Status Software License Total Downloads

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

Contents

Installation

Install this package via composer:

composer require natsumework/redis-autocomplete

Publish the configuration file to config/redis-autocomplete.php:

php artisan vendor:publish --provider="Natsumework\RedisAutocomplete\AutoCompleteServiceProvider"

Configuration

config/redis-autocomplete.php

return [
    /*
    |--------------------------------------------------------------------------
    | Redis Connection
    |--------------------------------------------------------------------------
    |
    | Your application's config/database.php configuration file allows you
    | to define multiple Redis connections / servers.
    | Here you may specify the connection you want to use.
    |
    | To specify an instance of the default Redis connection,
    | you may set the value to null.
    */
    'connection' => 'default',

    /*
    |--------------------------------------------------------------------------
    | Cache Key Prefix
    |--------------------------------------------------------------------------
    |
    | When utilizing a RAM based store such as APC or Memcached, there might
    | be other applications utilizing the same cache. So, we'll specify a
    | value to get prefixed to all our keys so we can avoid collisions.
    |
    */
    'prefix' => 'redis-autocomplete',

    /*
    |--------------------------------------------------------------------------
    | Storage time
    |--------------------------------------------------------------------------
    |
    | Specifies that phrases will expire in a few seconds.
    | If set to null, phrases will be stored indefinitely.
    |
    */
    'ttl' => 60 * 60 * 24
];

Usage

Add phrases

Note:

  • The phrase must contain id and name as attributes
  • phraseid must be unique
  • When searching, it will be searched by phrase name
  • You may specify the score attribute of phrase to sort the phrases, and the search results will be retrieved in descending order of score.
    If the score is not specified, the default will be 0
  • You can add any other attributes to the pharse
$phrases = [
    [
        'id' => 1, // required
        'name' => 'laravel' // required
    ],
    [
        'id' => 2,
        'name' => 'redis autocomplete',
        'score' => 10, // default is 0
        'custom_column_1' => 'column 1',
        'custom_column_2' => ['item 1', 'item 2'],
        ...
    ]
];

// addPhrases(string $name, $phrases)
Autocomplete::addPhrases('my-phrases', $phrases);

You may also use collections

$phrases = collect([
    [
        'id' => 1,
        'name' => 'laravel'
    ],
    [
        'id' => 2,
        'name' => 'redis autocomplete'
    ]
]);

Autocomplete::addPhrases('my-phrases-collection', $phrases);

or use Eloquent Collections

// users must contain `id` and `name` as attributes
$phrases = User::all();

Autocomplete::addPhrases('my-users', $phrases);

Search

// array search(string $name, string $keyword, int $limit = 10)
$result = Autocomplete::search('my-phrases', 'keyword', 10);

Remove phrase

Note that the id and name of the phrase must be the same as when it was added

// removePhrase(string $name, $phrase)

$phrase = [
    'id' => 1,
    'name' => 'laravel'
]

Autocomplete::removePhrase('my-phrases', $phrase)

Connection

You may obtain a connection to a specific Redis connection using the Autocomplete facade's connection method

Autocomplete::connection('autocomplete')
    ->addPhrases($name, $phrases);

Autocomplete::connection('autocomplete')
    ->search($name, $keyword);

Ttl

You may obtain a ttl to a specific expiration using the Autocomplete facade's ttl method

Autocomplete::ttl(60 * 60)
    ->addPhrases($name, $phrases);

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email natsumework0902@gmail.com instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 8.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固