forked from mozilla/kitsune
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bug 679533] redis_client() now raises exception if name passed isn't
defined or is unable to connect to the instance.
- Loading branch information
Showing
14 changed files
with
114 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from django.conf import settings | ||
from django.core.cache import parse_backend_uri | ||
|
||
from redis import Redis, ConnectionError | ||
|
||
|
||
class RedisError(Exception): | ||
"""An error with the redis configuration or connnection.""" | ||
|
||
|
||
def redis_client(name): | ||
"""Get a Redis client. | ||
Uses the name argument to lookup the connection string in the | ||
settings.REDIS_BACKEND dict. | ||
""" | ||
if name not in settings.REDIS_BACKENDS: | ||
raise RedisError( | ||
'{k} is not defined in settings.REDIS_BACKENDS'.format(k=name)) | ||
|
||
uri = settings.REDIS_BACKENDS[name] | ||
_, server, params = parse_backend_uri(uri) | ||
db = params.pop('db', 1) | ||
try: | ||
db = int(db) | ||
except (ValueError, TypeError): | ||
db = 1 | ||
try: | ||
socket_timeout = float(params.pop('socket_timeout')) | ||
except (KeyError, ValueError): | ||
socket_timeout = None | ||
password = params.pop('password', None) | ||
if ':' in server: | ||
host, port = server.split(':') | ||
try: | ||
port = int(port) | ||
except (ValueError, TypeError): | ||
port = 6379 | ||
else: | ||
host = server | ||
port = 6379 | ||
redis = Redis(host=host, port=port, db=db, password=password, | ||
socket_timeout=socket_timeout) | ||
try: | ||
redis.info() | ||
except ConnectionError: | ||
raise RedisError( | ||
'Unable to connect to redis backend: {k}'.format(k=name)) | ||
return redis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.