vladimir50/redis-search

简单的redis搜索库

dev-main 2022-01-16 21:05 UTC

This package is auto-updated.

Last update: 2024-09-17 02:52:24 UTC


README

RedisSearch 使用 predis/predis 库。

安装

composer require vladimir50/redis-search

您有两种初始化服务的方式

  1. 您可以通过配置文件初始化服务
    use RedisSearch\RedisSearch\RedisSearch;

    $redisSearch = new RedisSearch([
        'scheme'        => 'tcp',
        'host'          => '127.0.0.1',
        'port'          => 6379,
        'tables_prefix' => 'search_cache' // Redis search tables prefix
    ]);
  1. 或者通过您的 Predis 客户端初始化
    use RedisSearch\RedisSearch\RedisSearch;
    
    $client = new Predis\Client([
        'scheme' => 'tcp',
        'host'   => '10.0.0.1',
        'port'   => 6379,
    ]);

    $redisSearch = new RedisSearch(['tables_prefix' => 'search_cache'], $client);

方法指南

在表的特定列中搜索一个值,如果传递了第4个参数,则将通过传递的值搜索完整的条目

    $value = 'red';
    $key = 'color';
    
    $redisSearch->search('products', $value, $key);

在表的特定列中搜索范围的值

    $values = [
        10,
        100
    ];
    $key = 'price';
    
    $redisSearch->searchRange('products', $values, $key);

返回表中的记录数

    $redisSearch->totalCount('products');

通过ID从表中删除一行

    $redisSearch->delete('products', $id);

通过ID添加或更新表中的一条记录

    $fieldsData = [
        'color' => 'red',
        'type' => 'car',
        'categories' => [
            'cars',
            'red_color'
        ]
    ];
    
    $redisSearch->addOrUpdate('products', $id, $fieldsData);

为了通过ID更新特定的字段

    $redisSearch->updateField('products', $id, $filedName, $fildValue);

为了通过ID删除特定的字段

    $redisSearch->deleteField('products', $id, $filedName);

用于清除或Redis表

    $redisSearch->clearAll();

用于获取所有Redis行,如果传递了参数,则从特定的表中获取所有行

    $rows = $redisSearch->getAll();
    // or
    $rows = $redisSearch->getAll('products');

您可以在初始化服务时指定表前缀,或者通过方法指定它

    $redisSearch->setPrefix('products');

并且您还可以选择使用标准的 Predis 客户端

    /** @var Predis\Client $predisClient */
    $predisClient = $redisSearch->getClient();