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.
- 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
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
Install Go Redis Leaderboard using the "go get" command:
go get github.com/croatiangrn/go-redis-leaderboard
- Go language distribution
- Redis client for Golang (github.com/go-redis/redis/v8)
- Contributions are welcome.
- Take care to maintain the existing coding style.
- Open a pull request
Released under the Apache License.