Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis cluster endpoint support added #53

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ pip-log.txt
.mr.developer.cfg
/.project
/.pydevproject

.venv/
.idea/
.ipynb_checkpoints/
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ The default options are as follows:
MEMBER_DATA_KEY = 'member_data'
SCORE_KEY = 'score'
RANK_KEY = 'rank'
DEFAULT_CLUSTER_MODE = False
```

You would use the option, `order=Leaderboard.ASC`, if you wanted a leaderboard sorted from lowest-to-highest score. You may also set the `order` option on a leaderboard after you have created a new instance of a leaderboard. The various `..._KEY` options above control what data is returned in the hash of leaderboard data from calls such as `leaders` or `around_me`. Finally, the `global_member_data` option allows you to control whether optional member data is per-leaderboard (`False`) or global (`True`).

### Using cluster mode

```python
Leaderboard(
<leader_board_name>,
host=<cluster_endpoint>,
db=0, # only one db supported in cluster mode
cluster_mode=True
)
```

### Ranking members in the leaderboard

Add members to your leaderboard using `rank_member`:
Expand Down
23 changes: 19 additions & 4 deletions leaderboard/leaderboard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import division

from redis import StrictRedis, Redis, ConnectionPool
import math
import sys

from redis import StrictRedis, Redis, ConnectionPool
from rediscluster import StrictRedisCluster

if sys.version_info.major == 3:
from itertools import zip_longest
else:
Expand Down Expand Up @@ -30,6 +33,7 @@ class Leaderboard(object):
MEMBER_DATA_KEY = 'member_data'
SCORE_KEY = 'score'
RANK_KEY = 'rank'
DEFAULT_CLUSTER_MODE = False

@classmethod
def pool(self, host, port, db, pools={}, **options):
Expand Down Expand Up @@ -87,15 +91,26 @@ def __init__(self, leaderboard_name, **options):
connection = self.options.pop('connection', None)
if isinstance(connection, (StrictRedis, Redis)):
self.options['connection_pool'] = connection.connection_pool

host = self.options.pop('host', self.DEFAULT_REDIS_HOST)
port = self.options.pop('port', self.DEFAULT_REDIS_PORT)
if 'connection_pool' not in self.options:
self.options['connection_pool'] = self.pool(
self.options.pop('host', self.DEFAULT_REDIS_HOST),
self.options.pop('port', self.DEFAULT_REDIS_PORT),
host,
port,
self.options.pop('db', self.DEFAULT_REDIS_DB),
self.options.pop('pools', self.DEFAULT_POOLS),
**self.options
)
self.redis_connection = Redis(**self.options)
if not self.options.get("cluster_mode", self.DEFAULT_CLUSTER_MODE):
self.redis_connection = Redis(**self.options)
else:
startup_nodes = [{"host": host, "port": port}]
self.redis_connection = StrictRedisCluster(
startup_nodes=startup_nodes,
decode_responses=True,
skip_full_coverage_check=True
)

def delete_leaderboard(self):
'''
Expand Down
1 change: 1 addition & 0 deletions requirements.pip
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
redis
redis-py-cluster