In this guide, you will use ** Python ** and ** Redis ** to build a game leaderboard on ** Alibaba Cloud **.
The main idea of this demo is to use the cache layer (Redis) instead of hitting the database to generate a leaderboard in a gaming application. This approach is suitable for large databases that require real-time responses.
It's not required, but a basic understanding of Python is a plus (sample code is Python). I'm using Redis this time, so it's a good idea to read it to find out what Redis is like.
For information on creating a Redis cluster on Alibaba Cloud, read the separate step-by-step command at this guide (https://www.alibabacloud.com/help/en/doc-detail/26351.htm) and read it. I will omit it here because I can proceed along it. The settings are simple and easy to understand.
ssh -i root@
rm /usr/bin/python # Change python into version 3
ln -s /usr/bin/python3 /usr/bin/python
apt-get update # Update Ubuntu
export LC_ALL=C # Set Locale
apt-get install python3-pip # Install pip
pip install redis # Install python-redis
apt-get install apache2 # Install apache
mkdir /var/www/python # Set Environment
a2dismod mpm_event
a2enmod mpm_prefork cgi
4, replace etc / apache2 / sites-enabled / 000-default.conf.
-----
<VirtualHost *:80>
DocumentRoot /var/www/python
<Directory /var/www/python>
Options +ExecCGI
DirectoryIndex leaderboards.py
</Directory>
AddHandler cgi-script .py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
-----
chmod 755 /var/www/python/leaderboards.py
service apache2 restart
Below is a code sample in Python, but I'll explain this further.
Code description: 1, connect to Redis (8th line):
r = redis.StrictRedis(host='', port=6379, db=0, password='')
The host name and password must be updated for this to work properly. It can be obtained from "Connection Address" when creating ApsaraDB for Redis. It is also the password set at the time of creation.
2, add the score to the leaderboard (line 16)
r.zincrby(leaderboardName, gamer, random.randint(1,1000000))
LeaderboardName is the key you set for the leaderboard name, gamer is the gamer's username or ID, and the last parameter is where you put the score (in this case a random number).
r.zrange(leaderboardName, 0, 9, desc=True, withscores=True)
LeaderboardName is the key to set the name of the leaderboard, the second parameter is from which rank to start (0 starts) and the third parameter is where to stop (-1 to show to the end). The value desc = True sorts the leaderboards in descending order (False by default).
4, get the ranking of the current player (line 30).
r.zrevrank(leaderboardName, gamer)+1
LeaderboardName is the key you set for the leaderboard name, and gamer is the gamer's username or ID. In the database the rank starts at 0 instead of 1, so you need to add one (+1). 5. Get the score of the current player (or anyone) (line 34).
r.zscore(leaderboardName, gamer)
LeaderboardName is the key you set for the leaderboard name, and gamer is the gamer's username or ID.
Below is the expected response when running code on a web server.
Redis stores data in-memory and can reach the performance of millions of requests per second depending on the maturity of the product. This makes it the perfect database for this use case and other caching needs.
For more information on Redis on Alibaba Cloud, please visit the ApsaraDB for Redis (https://www.alibabacloud.com/en/product/apsaradb-for-redis) page.
Recommended Posts