Skip to content

croatiangrn/go-redis-leaderboard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Redis Leaderboard

A leaderboard written in Go using Redis database.

Base idea is taken from this project. But this project uses go-redis package and supports saving of generic user info to redis.

Features

  • Create multiple Leaderboards by name
  • You can rank a member the leaderboard will be updated automatically
  • Remove a member from a specific Leaderboard
  • Get total of users in a specific Leaderboard and also how many pages it has.
  • Get leaders on any page

How to use

Create a new leaderboard or attach to an existing leaderboard named 'awesome_leaderboard':

    const UserInfoBucket = "users_info_bucket"
    
    redisSettings := redisLeaderboard.RedisSettings{
        Host:     "127.0.0.1:6379",
        Password: "",
        DB:       0,
    }
    
    awesomeLeaderboard, err := redisLeaderboard.NewLeaderboard(redisSettings, redisLeaderboard.ProdMode, "awesome_leaderboard", UserInfoBucket, redisLeaderboard.DefaultPageSize)
    //return an awesomeLeaderboard

Adding or getting member from awesome_leaderboard using FirstOrInsertMember(userID, score):

    awesomeLeaderboard.FirstOrInsertMember("12345", 33)
    awesomeLeaderboard.FirstOrInsertMember("45678", 44)
    awesomeLeaderboard.FirstOrInsertMember("111", 12)

You can call IncrementMemberScore(userID, incrementBy) with the same member and the leaderboard will be updated automatically:

	awesomeLeaderboard.IncrementMemberScore("12345", 7481523)
	//return an user: User{UserID:"12345", Score:7481523, Rank:1}

Getting a total number of members on awesome_leaderboard using TotalMembers():

	awesomeLeaderboard.TotalMembers()
	//return an int: 3

Getting the member and his info using GetMember(userID, withInfo):

	awesomeLeaderboard.GetMember("12345", true)
	//return 
	    {
                 "user_id": "12345",
                 "score": 30,
                 "rank": 1,
                 "additional_info": {
                     "user_name": "Jane Doe | 2020",
                     "email": "[email protected]"
                 }
            }

Getting leaders using GetLeaders(page):

	awesomeLeaderboard.GetLeaders(1)
	//return an array of users with highest score in a first page (you can specify any page): [pageSize]User

Installation

Install Go Redis Leaderboard using the "go get" command:

go get github.com/croatiangrn/go-redis-leaderboard

Dependencies

  • Go language distribution
  • Redis client for Golang (github.com/go-redis/redis/v8)

Contributing

  • Contributions are welcome.
  • Take care to maintain the existing coding style.
  • Open a pull request

License

Released under the Apache License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages