diff --git a/.gitignore b/.gitignore index 0dd68ca..aa0e9a3 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,7 @@ pip-log.txt .mr.developer.cfg /.project /.pydevproject + +.venv/ +.idea/ +.ipynb_checkpoints/ diff --git a/README.md b/README.md index 8e2f1f1..9fcae7e 100644 --- a/README.md +++ b/README.md @@ -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( + , + host=, + 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`: diff --git a/leaderboard/leaderboard.py b/leaderboard/leaderboard.py index fd2a2ca..9412bc9 100644 --- a/leaderboard/leaderboard.py +++ b/leaderboard/leaderboard.py @@ -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: @@ -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): @@ -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): ''' diff --git a/requirements.pip b/requirements.pip index 7800f0f..613757e 100644 --- a/requirements.pip +++ b/requirements.pip @@ -1 +1,2 @@ redis +redis-py-cluster \ No newline at end of file