はじめに とあるサービスでランキング計算をする実装を組んでいた。ふと、アプリケーション側ではなく、Redisに任せることもできることを思い出したので、調査がてらサンプルコードを書いたので記事にまとめる。 実現したいこと スコア情報を持つデータがある。Redisを使用して同点順位によるランキングを作成したい 開発環境 Redis 4.0.11 Ruby 2.6.1p33 gem redis 4.1.2 実装方法 今回は動作検証のためRubyと redis gemを使用して実装をした。後ほどサンプルコード全文を共有する。 準備 まずは redis gemを利用してRedisに接続をする。Redisに接続したら、以降は REDIS オブジェクトを通じてRedisを操作する。 require 'redis' # Redisに接続 REDIS = Redis.new(host: 'localhost