# HG changeset patch # User Brian Neal # Date 1323028407 0 # Node ID 6f5fff9248775ee9b8faef02253479d8ab6e933a # Parent 8631d32e6b1697415c4fc2927a8f9d5830d290c6 Created a centralized spot to get a Redis connection so that settings can be managed in one place. diff -r 8631d32e6b16 -r 6f5fff924877 gpp/accounts/management/commands/rate_limit_clear.py --- a/gpp/accounts/management/commands/rate_limit_clear.py Sun Dec 04 03:05:21 2011 +0000 +++ b/gpp/accounts/management/commands/rate_limit_clear.py Sun Dec 04 19:53:27 2011 +0000 @@ -9,6 +9,9 @@ from django.core.management.base import BaseCommand import redis +from core.services import get_redis_connection + + IP_RE = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') @@ -21,7 +24,7 @@ def handle(self, *args, **kwargs): try: - con = redis.Redis() + con = get_redis_connection() # get all rate-limit keys keys = con.keys('rate-limit-*') diff -r 8631d32e6b16 -r 6f5fff924877 gpp/antispam/rate_limit.py --- a/gpp/antispam/rate_limit.py Sun Dec 04 03:05:21 2011 +0000 +++ b/gpp/antispam/rate_limit.py Sun Dec 04 19:53:27 2011 +0000 @@ -8,14 +8,11 @@ import redis from django.conf import settings +from core.services import get_redis_connection + logger = logging.getLogger(__name__) -# Redis connection and database settings -HOST = getattr(settings, 'RATE_LIMIT_REDIS_HOST', 'localhost') -PORT = getattr(settings, 'RATE_LIMIT_REDIS_PORT', 6379) -DB = getattr(settings, 'RATE_LIMIT_REDIS_DB', 0) - # This exception is thrown upon any Redis error. This insulates client code from # knowing that we are using Redis and will allow us to use something else in the @@ -37,7 +34,7 @@ Create and return a Redis connection. Returns None on failure. """ try: - conn = redis.Redis(host=HOST, port=PORT, db=DB) + conn = get_redis_connection() except redis.RedisError, e: logger.error("rate limit: %s" % e) raise RateLimiterUnavailable diff -r 8631d32e6b16 -r 6f5fff924877 gpp/antispam/tests/rate_limit_tests.py --- a/gpp/antispam/tests/rate_limit_tests.py Sun Dec 04 03:05:21 2011 +0000 +++ b/gpp/antispam/tests/rate_limit_tests.py Sun Dec 04 19:53:27 2011 +0000 @@ -2,18 +2,18 @@ Tests for the rate limiting function in the antispam application. """ -import redis from django.test import TestCase from django.core.urlresolvers import reverse from antispam.rate_limit import _make_key +from core.services import get_redis_connection class RateLimitTestCase(TestCase): KEY = _make_key('127.0.0.1') def setUp(self): - self.conn = redis.Redis(host='localhost', port=6379, db=0) + self.conn = get_redis_connection() self.conn.delete(self.KEY) def tearDown(self): diff -r 8631d32e6b16 -r 6f5fff924877 gpp/core/services.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/core/services.py Sun Dec 04 19:53:27 2011 +0000 @@ -0,0 +1,21 @@ +""" +This module provides a common way for the various apps to integrate with services +that are installed at this site. + +""" +from django.conf import settings +import redis + +# Redis connection and database settings + +REDIS_HOST = getattr(settings, 'REDIS_HOST', 'localhost') +REDIS_PORT = getattr(settings, 'REDIS_PORT', 6379) +REDIS_DB = getattr(settings, 'REDIS_DB', 0) + + +def get_redis_connection(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB): + """ + Create and return a Redis connection using the supplied parameters. + + """ + return redis.Redis(host=host, port=port, db=db) diff -r 8631d32e6b16 -r 6f5fff924877 gpp/core/whos_online.py --- a/gpp/core/whos_online.py Sun Dec 04 03:05:21 2011 +0000 +++ b/gpp/core/whos_online.py Sun Dec 04 19:53:27 2011 +0000 @@ -4,21 +4,17 @@ """ import logging -from django.conf import settings import redis +from core.services import get_redis_connection + + # Users and visitors each have 2 sets that we store in a Redis database: # a current set and an old set. Whenever a user or visitor is seen, the # current set is updated. At some interval, the current set is renamed # to the old set, thus destroying it. At any given time, the union of the # current and old sets is the "who's online" set. -# Redis connection and database settings - -HOST = getattr(settings, 'WHOS_ONLINE_REDIS_HOST', 'localhost') -PORT = getattr(settings, 'WHOS_ONLINE_REDIS_PORT', 6379) -DB = getattr(settings, 'WHOS_ONLINE_REDIS_DB', 0) - # Redis key names: USER_CURRENT_KEY = "wo_user_current" USER_OLD_KEY = "wo_user_old" @@ -39,7 +35,7 @@ Create and return a Redis connection. Returns None on failure. """ try: - conn = redis.Redis(host=HOST, port=PORT, db=DB) + conn = get_redis_connection() return conn except redis.RedisError, e: logger.error(e)