PHPackages                             juniora/laravel-redis-paginator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. juniora/laravel-redis-paginator

ActiveLibrary[Caching](/categories/caching)

juniora/laravel-redis-paginator
===============================

Create a Laravel Paginator or LengthAwarePaginator from a Redis sorted set

v2.4.1(4mo ago)01.6k↓66.7%MITPHPPHP ^8.0CI failing

Since Nov 11Pushed 4mo agoCompare

[ Source](https://github.com/JunioraTeam/laravel-redis-paginator)[ Packagist](https://packagist.org/packages/juniora/laravel-redis-paginator)[ Docs](https://github.com/ge-tracker/laravel-redis-paginator)[ RSS](/packages/juniora-laravel-redis-paginator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (7)Versions (9)Used By (0)

Laravel Redis Sorted Set Paginator
==================================

[](#laravel-redis-sorted-set-paginator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9cbb85e585bea8e664f39c71ea56f69c554a15bcfc0d0088dc2bd2336c2d9e1e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a756e696f72612f6c61726176656c2d72656469732d706167696e61746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juniora/laravel-redis-paginator)[![Total Downloads](https://camo.githubusercontent.com/c747a6f127b58cfc4c254f2de524ad2d3f90575ce7ac9349cfa2bb9d6ae7ad95/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a756e696f72612f6c61726176656c2d72656469732d706167696e61746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juniora/laravel-redis-paginator)

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

[](#installation)

You can install the package via composer:

```
composer require juniora/laravel-redis-paginator
```

Usage
-----

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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:

memberscoreEloquent IDuser:11001user:22002user:33003First, 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.

```
