|
| 1 | +import os |
| 2 | +import threading |
| 3 | +import time |
| 4 | +import pickle |
| 5 | + |
| 6 | +from beaker.container import NamespaceManager |
| 7 | + |
| 8 | +try: |
| 9 | + import redis |
| 10 | +except ImportError: |
| 11 | + redis = None |
| 12 | + |
| 13 | +from beaker.ext.redisnm import RedisNamespaceManager, RedisSynchronizer |
| 14 | +from beaker._compat import string_type |
| 15 | + |
| 16 | + |
| 17 | +class RedisClusterNamespaceManager(RedisNamespaceManager): |
| 18 | + """Provides the :class:`.NamespaceManager` API over Redis cluster. |
| 19 | +
|
| 20 | + Provided ``urls`` can be both multiple redis connection strings separated by a comma or |
| 21 | + an already existing RedisCluster instance. |
| 22 | +
|
| 23 | + Unlike a StrictRedis connection string, a RedisCluster one does not support |
| 24 | + database indicators, it is zero by default. |
| 25 | +
|
| 26 | + Example: `redis://node-1:7001,redis://node-2:7002` |
| 27 | +
|
| 28 | + Additional options can be passed in kwargs (e.g. `username="redis", password="secure_password"`). |
| 29 | +
|
| 30 | + The data will be stored into redis keys, with their name |
| 31 | + starting with ``beaker_cache:``. |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, namespace, urls, timeout=None, **kwargs): |
| 35 | + super(RedisNamespaceManager, self).__init__(namespace) |
| 36 | + self.lock_dir = None # Redis uses redis itself for locking. |
| 37 | + self.timeout = timeout |
| 38 | + self.nodes = [] |
| 39 | + self.options = kwargs |
| 40 | + |
| 41 | + if redis is None: |
| 42 | + raise RuntimeError('redis is not available') |
| 43 | + |
| 44 | + if isinstance(urls, string_type): |
| 45 | + for url in urls.split(','): |
| 46 | + url_options = redis.connection.parse_url(url) |
| 47 | + if 'db' in url_options: |
| 48 | + raise redis.exceptions.RedisClusterException( |
| 49 | + "A ``db`` querystring option can only be 0 in cluster mode" |
| 50 | + ) |
| 51 | + self.nodes.append(redis.cluster.ClusterNode( |
| 52 | + host=url_options.get('host'), |
| 53 | + port=url_options.get('port') |
| 54 | + )) |
| 55 | + self.client = RedisClusterNamespaceManager.clients.get( |
| 56 | + urls, redis.cluster.RedisCluster, startup_nodes=self.nodes, **kwargs |
| 57 | + ) |
| 58 | + else: |
| 59 | + self.client = urls |
| 60 | + |
| 61 | + def get_creation_lock(self, key): |
| 62 | + return RedisClusterSynchronizer(self._format_key(key), self.client, self.nodes, **self.options) |
| 63 | + |
| 64 | + |
| 65 | +class RedisClusterSynchronizer(RedisSynchronizer): |
| 66 | + """Synchronizer based on redis cluster. |
| 67 | +
|
| 68 | + Provided ``urls`` can be both multiple redis connection strings separated by a comma or |
| 69 | + an already existing RedisCluster instance. |
| 70 | +
|
| 71 | + Unlike a StrictRedis connection string, a RedisCluster one does not support |
| 72 | + database indicators, it is zero by default. |
| 73 | +
|
| 74 | + Example: ``redis://node-1:7001,redis://node-2:7002, |
| 75 | +
|
| 76 | + This Synchronizer only supports 1 reader or 1 writer at time, not concurrent readers. |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, identifier, urls, nodes=None, **kwargs): |
| 80 | + super(RedisSynchronizer, self).__init__() |
| 81 | + self.identifier = 'beaker_lock:%s' % identifier |
| 82 | + if isinstance(urls, string_type): |
| 83 | + self.client = RedisClusterNamespaceManager.clients.get( |
| 84 | + urls, redis.cluster.RedisCluster, startup_nodes=nodes, **kwargs |
| 85 | + ) |
| 86 | + else: |
| 87 | + self.client = urls |
0 commit comments