Mercurial > public > sg101
comparison gpp/core/whos_online.py @ 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 | 3fe60148f75c |
children | f72ace06658a |
comparison
equal
deleted
inserted
replaced
507:8631d32e6b16 | 508:6f5fff924877 |
---|---|
2 This module keeps track of who is online. We maintain records for both | 2 This module keeps track of who is online. We maintain records for both |
3 authenticated users ("users") and non-authenticated visitors ("visitors"). | 3 authenticated users ("users") and non-authenticated visitors ("visitors"). |
4 """ | 4 """ |
5 import logging | 5 import logging |
6 | 6 |
7 from django.conf import settings | |
8 import redis | 7 import redis |
8 | |
9 from core.services import get_redis_connection | |
10 | |
9 | 11 |
10 # Users and visitors each have 2 sets that we store in a Redis database: | 12 # Users and visitors each have 2 sets that we store in a Redis database: |
11 # a current set and an old set. Whenever a user or visitor is seen, the | 13 # a current set and an old set. Whenever a user or visitor is seen, the |
12 # current set is updated. At some interval, the current set is renamed | 14 # current set is updated. At some interval, the current set is renamed |
13 # to the old set, thus destroying it. At any given time, the union of the | 15 # to the old set, thus destroying it. At any given time, the union of the |
14 # current and old sets is the "who's online" set. | 16 # current and old sets is the "who's online" set. |
15 | |
16 # Redis connection and database settings | |
17 | |
18 HOST = getattr(settings, 'WHOS_ONLINE_REDIS_HOST', 'localhost') | |
19 PORT = getattr(settings, 'WHOS_ONLINE_REDIS_PORT', 6379) | |
20 DB = getattr(settings, 'WHOS_ONLINE_REDIS_DB', 0) | |
21 | 17 |
22 # Redis key names: | 18 # Redis key names: |
23 USER_CURRENT_KEY = "wo_user_current" | 19 USER_CURRENT_KEY = "wo_user_current" |
24 USER_OLD_KEY = "wo_user_old" | 20 USER_OLD_KEY = "wo_user_old" |
25 USER_KEYS = [USER_CURRENT_KEY, USER_OLD_KEY] | 21 USER_KEYS = [USER_CURRENT_KEY, USER_OLD_KEY] |
37 def _get_connection(): | 33 def _get_connection(): |
38 """ | 34 """ |
39 Create and return a Redis connection. Returns None on failure. | 35 Create and return a Redis connection. Returns None on failure. |
40 """ | 36 """ |
41 try: | 37 try: |
42 conn = redis.Redis(host=HOST, port=PORT, db=DB) | 38 conn = get_redis_connection() |
43 return conn | 39 return conn |
44 except redis.RedisError, e: | 40 except redis.RedisError, e: |
45 logger.error(e) | 41 logger.error(e) |
46 | 42 |
47 return None | 43 return None |