changeset 508:6f5fff924877

Created a centralized spot to get a Redis connection so that settings can be managed in one place.
author Brian Neal <bgneal@gmail.com>
date Sun, 04 Dec 2011 19:53:27 +0000
parents 8631d32e6b16
children 248dd8dd67f8
files gpp/accounts/management/commands/rate_limit_clear.py gpp/antispam/rate_limit.py gpp/antispam/tests/rate_limit_tests.py gpp/core/services.py gpp/core/whos_online.py
diffstat 5 files changed, 34 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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-*')
--- 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
--- 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):
--- /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)
--- 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)