ge-tracker/laravel-redis-paginator 问题修复 & 功能扩展

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

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

ge-tracker/laravel-redis-paginator

Composer 安装命令:

composer require ge-tracker/laravel-redis-paginator

包简介

Create a Laravel Paginator or LengthAwarePaginator from a Redis sorted set

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Ever wanted to display paginated sorted sets at scale? A great example of this would be a leaderboard for a game, or for a website with a large userbase. This package will create a Laravel LengthAwarePaginator from a Redis sorted set. As a sorted set, by definition, is always sorted, it allows a large number of records to be paginated, with very little overhead.

Installation

You can install the package via composer:

composer require ge-tracker/laravel-redis-paginator

Usage

Initialise the paginator by using dependency injection or the provided RedisPaginator facade. The example below will interact with the leaderboard sorted set. We leverage Laravel's Redis interface, which will honour any prefixing and clustering options that you have configured on your application.

Here is an example of a paginated sorted set in action:

public function index(LaravelRedisPaginator $redisPaginator)
{
    $users = $redisPaginator->perPage(25)->paginate('leaderboard');

    return view('leaderboard', compact('users'));
}

Sorting

The sortAsc and sortDesc methods allow you to choose the order of the returned results. The default sorting is in ascending order.

$usersAsc = $redisPaginator->sortAsc()->paginate('leaderboard');

$usersDesc = $redisPaginator->sortDesc()->paginate('leaderboard');

Get the rank and page for a user

You may want to display the user's rank in the sorted set, as well as a link to jump to the page that contains their name. This can be achieved by using the rank() method. A MemberRank object will be returned which contains the score, rank, and page properties:

public function show(User $user, LaravelRedisPaginator $redisPaginator)
{
    $memberRank = $redisPaginator->rank('user:' . $user->id, 'leaderboard');

    dump($memberRank->score, $memberRank->rank, $memberRank->page);
}

Using the facade

For those of you who prefer facades over dependency injection, that option is also available:

public function index()
{
    $users = RedisPaginator::perPage(25)->paginate('leaderboard');

    return view('leaderboard', compact('users'));
}

Selecting a page to view

The current page can be set by using the page() method, or by using the method parameters. Under the hood, the package uses Laravel's default Paginator's page resolution, which means that the page can also be specified via the query string.

// Using the fluent interface
$users = $redisPaginator->page(5)->paginate('leaderboard');

// Using method parameters
$users = $redisPaginator->paginate('leaderboard', 'page', 5);

// https://www.example.com/leaderboard?page=5
$users = $redisPaginator->paginate('leaderboard');

Resolving Eloquent models

Given that Redis an in-memory data structure store, and not a relational database, it is very likely that the real data relating to your paginated data (leaderboard?) is not wholly stored in Redis. This data will need to be loaded once you have fetched your paginated results, and this package will handle that for you.

In this example, we assume that you have stored your data in the following format:

member score Eloquent ID
user:1 100 1
user:2 200 2
user:3 300 3

First, create a Redis resolver. This can be placed anywhere your application, such as app/RedisResolvers/UserResolver.php.

The $modelKey property should correspond to the key that you are using to generate your Redis members. This will generally be id or uuid. The $scoreField property defines the field that will be mapped onto your Eloquent model, or merged into your results array.

<?php

namespace App\RedisResolvers;

use App\User;
use GeTracker\LaravelRedisPaginator\Resolvers\AbstractResolver;

class UserResolver extends AbstractResolver
{
    // Defaults shown below, can be omitted
    protected $modelKey = 'id';
    protected $scoreField = 'score';

    /**
     * Load Eloquent models
     */
    protected function resolveModels(array $keys)
    {
        return User::whereIn('id', $keys)->get();
    }

    /**
     * Resolve a key from Redis to an Eloquent incrementing ID or UUID
     */
    protected function resolveKey($key)
    {
        return (int)str_replace('user:', '', $key);
    }
}

The resolveKey() method will take a single key (Redis member), and allow you to transform it. In the example above, we are stripping user: from the string, before casting it to an integer.

You can then define resolveModels() that accepts an array of resolved keys to be queried.

Finally, we must set our model resolver before running the query:

$users = $this->paginator
    ->setModelResolver(new \App\RedisResolvers\UserResolver())
    ->paginate('leaderboard');

We can now access our full User model, as well as the score that has been loaded from Redis:

echo $users[0]->name . ' -> ' . $users[0]->score;

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email james@ge-tracker.com instead of using the issue tracker.

Credits

License

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

ge-tracker/laravel-redis-paginator 适用场景与选型建议

ge-tracker/laravel-redis-paginator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.33k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「paginator」 「pagination」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ge-tracker/laravel-redis-paginator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ge-tracker/laravel-redis-paginator 我们能提供哪些服务?
定制开发 / 二次开发

基于 ge-tracker/laravel-redis-paginator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-21