natsumework/redis-autocomplete

为您的laravel应用提供一种简单的方式来使用redis以加速自动完成查询。

1.1.0 2024-03-31 05:59 UTC

This package is auto-updated.

Last update: 2024-08-31 00:29:53 UTC


README

Latest Version on Packagist Test Status Software License Total Downloads

为您的laravel应用提供一种简单的方式来使用redis以加速自动完成查询。

内容

安装

使用composer安装此包

composer require natsumework/redis-autocomplete

将配置文件发布到config/redis-autocomplete.php

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

配置

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
];

使用

添加短语

注意

  • 短语必须包含idname作为属性
  • 短语id必须唯一
  • 搜索时,将按短语name进行搜索
  • 您可以为短语指定score属性以排序短语,搜索结果将按分数降序检索。
    如果没有指定分数,则默认为0
  • 您可以添加任何其他属性到短语
$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);

您还可以使用集合

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

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

或使用Eloquent集合

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

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

搜索

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

删除短语

注意,短语的idname必须与添加时相同

// removePhrase(string $name, $phrase)

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

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

连接

您可以使用Autocomplete外观的连接方法获取对特定Redis连接的连接

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

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

Ttl

您可以使用Autocomplete外观的ttl方法获取特定过期时间的Ttl

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

变更日志

有关最近更改的更多信息,请参阅变更日志

测试

$ composer test

安全

如果您发现任何安全相关的问题,请通过natsumework0902@gmail.com发送电子邮件,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅贡献指南

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件