Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create a Laravel Paginator or LengthAwarePaginator from a Redis sorted set

License

NotificationsYou must be signed in to change notification settings

ge-tracker/laravel-redis-paginator

Repository files navigation

Latest Version on PackagistGitHub Tests Action StatusTotal 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 LaravelLengthAwarePaginator 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 providedRedisPaginator facade. The example below will interact with theleaderboard 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:

publicfunctionindex(LaravelRedisPaginator$redisPaginator){$users =$redisPaginator->perPage(25)->paginate('leaderboard');returnview('leaderboard',compact('users'));}

Sorting

ThesortAsc andsortDesc 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 therank() method. AMemberRank object will be returned which contains thescore,rank, andpage properties:

publicfunctionshow(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:

publicfunctionindex(){$users = RedisPaginator::perPage(25)->paginate('leaderboard');returnview('leaderboard',compact('users'));}

Selecting a page to view

The current page can be set by using thepage() method, or by using the method parameters. Under the hood, the package uses Laravel's defaultPaginator'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:

memberscoreEloquent ID
user:11001
user:22002
user:33003

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

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

<?phpnamespaceApp\RedisResolvers;useApp\User;useGeTracker\LaravelRedisPaginator\Resolvers\AbstractResolver;class UserResolverextends AbstractResolver{// Defaults shown below, can be omittedprotected$modelKey ='id';protected$scoreField ='score';/**     * Load Eloquent models     */protectedfunctionresolveModels(array$keys)    {return User::whereIn('id',$keys)->get();    }/**     * Resolve a key from Redis to an Eloquent incrementing ID or UUID     */protectedfunctionresolveKey($key)    {return (int)str_replace('user:','',$key);    }}

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

You can then defineresolveModels() 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

composertest

Changelog

Please seeCHANGELOG for more information on what has changed recently.

Contributing

Please seeCONTRIBUTING for details.

Security

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

Credits

License

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

About

Create a Laravel Paginator or LengthAwarePaginator from a Redis sorted set

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp