https://github.com/redis-developer/basic-redis-leaderboard-demo-python
A Basic Redis leaderboard demo app written in Python
https://github.com/redis-developer/basic-redis-leaderboard-demo-python
Last synced: 11 months ago
JSON representation
A Basic Redis leaderboard demo app written in Python
- Host: GitHub
- URL: https://github.com/redis-developer/basic-redis-leaderboard-demo-python
- Owner: redis-developer
- Created: 2021-02-11T14:01:43.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-27T07:42:46.000Z (over 2 years ago)
- Last Synced: 2025-04-12T03:52:34.870Z (11 months ago)
- Language: Python
- Size: 6.29 MB
- Stars: 11
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Basic Redis Leaderboard Demo Python (Django)
Show how the redis works with Python (Django).

# Overview video
Here's a short video that explains the project and how it uses Redis:
[](https://www.youtube.com/watch?v=zzinHxdZ34I)
## Try it out
(See notes: How to run on Google Cloud)
## How to run on Google Cloud
If you don't have redis yet, plug it in (https://spring-gcp.saturnism.me/app-dev/cloud-services/cache/memorystore-redis).
After successful deployment, you need to manually enable the vpc connector as shown in the pictures:
1. Open link google cloud console.

2. Click "Edit and deploy new revision" button.

3. Add environment.

4. Select vpc-connector and deploy application.

Problem with unsupported flags when deploying google cloud run button
# How it works?
## 1. How the data is stored:
- The AAPL's details - market cap of 2,6 triillions and USA origin - are stored in a hash like below:
HSET "company:AAPL" symbol "AAPL" market_cap "2600000000000" country USA
- The Ranks of AAPL of 2,6 trillions are stored in a ZSET.
ZADD companyLeaderboard 2600000000000 company:AAPL
## 2. How the data is accessed:
- Top 10 companies:
ZREVRANGE companyLeaderboard 0 9 WITHSCORES
- All companies:
ZREVRANGE companyLeaderboard 0 -1 WITHSCORES
- Bottom 10 companies:
ZRANGE companyLeaderboard 0 9 WITHSCORES
- Between rank 10 and 15:
ZREVRANGE companyLeaderboard 9 14 WITHSCORES
- Show ranks of AAPL, FB and TSLA:
ZREVRANGE companyLeaderBoard company:AAPL company:FB company:TSLA
- Adding 1 billion to market cap of FB company:
ZINCRBY companyLeaderBoard 1000000000 "company:FB"
- Reducing 1 billion of market cap of FB company:
ZINCRBY companyLeaderBoard -1000000000 "company:FB"
- Companies between 500 billion and 1 trillion:
ZCOUNT companyLeaderBoard 500000000000 1000000000000
- Companies over a Trillion:
ZCOUNT companyLeaderBoard 1000000000000 +inf
## How to run it locally?
### Development
```
git clone https://github.com/redis-developer/basic-redis-leaderboard-demo-python.git
```
### Run docker compose or install redis manually
Install docker (on mac: https://docs.docker.com/docker-for-mac/install/)
```sh
docker network create global
docker-compose up -d --build
```
#### Open directory server (cd server/configuration): copy .env.example to create .env (copy .env.example .env or cp .env.example .env). And provide the values for environment variables (if needed)
- DJANGO_DEBUG: Django debug mode
- DJANGO_ALLOWED_HOSTS: Allowed hosts
- REDIS_URL: Redis server url
- REDIS_HOST: Redis server host
- REDIS_PORT: Redis server port
- REDIS_DB: Redis server db index
- REDIS_PASSWORD: Redis server password
#### Run backend
Install python, pip and venv (on mac: https://installpython3.com/mac/)
Use python version: 3.9.1
``` sh
python3 -m venv venv
source ./venv/bin/activate
pip3 install -r requirements.txt
python3 server/manage.py collectstatic
python3 server/manage.py runserver
```
#### Run frontend
Static сontent runs automatically with the backend part. In case you need to run it separately, please see README in the [client](client) folder.