From 4f7bae766b2bcd33c40ca3d0050870357f466e23 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sat, 12 Oct 2013 18:45:02 +0300 Subject: [PATCH] implemented a config cache --- doc/todo.txt | 1 - src/config.py | 51 +++++++++++++++++++++++++++++++++++++++++-------- src/handlers.py | 2 -- src/server.py | 17 ++++++++++++++++- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/doc/todo.txt b/doc/todo.txt index 0ca7dee..79e2361 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -1,5 +1,4 @@ --> authentication -> cache configuration -> prevent Request closed errors by stopping mjpg clients before stopping motion diff --git a/src/config.py b/src/config.py index 7c7306a..3c408d6 100644 --- a/src/config.py +++ b/src/config.py @@ -14,9 +14,16 @@ _CAMERA_CONFIG_FILE_NAME = 'thread-%(id)s.conf' _MAIN_CONFIG_FILE_PATH = os.path.join(settings.CONF_PATH, 'motion.conf') _CAMERA_CONFIG_FILE_PATH = os.path.join(settings.CONF_PATH, _CAMERA_CONFIG_FILE_NAME) +_main_config_cache = None +_camera_config_cache = None +_camera_ids_cache = None + def get_main(as_lines=False): - # TODO use a cache + global _main_config_cache + + if not as_lines and _main_config_cache: + return _main_config_cache config_file_path = os.path.join(settings.PROJECT_PATH, _MAIN_CONFIG_FILE_PATH) @@ -57,11 +64,13 @@ def get_main(as_lines=False): data = _conf_to_dict(lines, list_names=['thread']) _set_default_motion(data) + _main_config_cache = data + return data def set_main(data): - # TODO use a cache + global _main_config_cache _set_default_motion(data) @@ -101,11 +110,18 @@ def set_main(data): finally: file.close() - + + _main_config_cache = data + return data def get_camera_ids(): + global _camera_ids_cache + + if _camera_ids_cache: + return _camera_ids_cache + config_path = settings.CONF_PATH logging.debug('listing config dir %(path)s...' % {'path': config_path}) @@ -133,6 +149,8 @@ def get_camera_ids(): camera_ids.sort() + _camera_ids_cache = camera_ids + return camera_ids @@ -146,7 +164,10 @@ def has_enabled_cameras(): def get_camera(camera_id, as_lines=False): - # TODO use a cache + global _camera_config_cache + + if not as_lines and _camera_config_cache and camera_id in _camera_config_cache: + return _camera_config_cache[camera_id] camera_config_path = _CAMERA_CONFIG_FILE_PATH % {'id': camera_id} @@ -186,11 +207,16 @@ def get_camera(camera_id, as_lines=False): _set_default_motion_camera(data) + if _camera_config_cache is None: + _camera_config_cache = {} + + _camera_config_cache[camera_id] = data + return data def set_camera(camera_id, data): - # TODO use a cache + global _camera_config_cache if data['@proto'] == 'v4l2': _set_default_motion_camera(data) @@ -246,12 +272,19 @@ def set_camera(camera_id, data): finally: file.close() + + if _camera_config_cache is None: + _camera_config_cache = {} + + _camera_config_cache[camera_id] = data return data def add_camera(device_details): - # TODO use a cache + global _camera_ids + + _camera_ids = None # determine the last camera id camera_ids = get_camera_ids() @@ -291,8 +324,10 @@ def add_camera(device_details): def rem_camera(camera_id): - # TODO use a cache - + global _camera_ids + + _camera_ids = None + camera_config_name = _CAMERA_CONFIG_FILE_NAME % {'id': camera_id} camera_config_path = _CAMERA_CONFIG_FILE_PATH % {'id': camera_id} diff --git a/src/handlers.py b/src/handlers.py index 6b85070..2343c03 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -51,12 +51,10 @@ class BaseHandler(RequestHandler): main_config = config.get_main() if user == main_config.get('@admin_username') and pwd == main_config.get('@admin_password'): - logging.debug('authenticated as admin') return 'admin' elif user == main_config.get('@normal_username') and pwd == main_config.get('@normal_password'): - logging.debug('authenticated as normal user') return 'normal' diff --git a/src/server.py b/src/server.py index 5cddb1c..006e979 100644 --- a/src/server.py +++ b/src/server.py @@ -7,6 +7,21 @@ import settings import template +def log_request(handler): + if handler.get_status() < 400: + log_method = logging.debug + + elif handler.get_status() < 500: + log_method = logging.warning + + else: + log_method = logging.error + + request_time = 1000.0 * handler.request.request_time() + log_method("%d %s %.2fms", handler.get_status(), + handler._request_summary(), request_time) + + application = Application( [ (r'^/$', handlers.MainHandler), @@ -19,7 +34,7 @@ application = Application( (r'^/movie/(?P\d+)/(?Pdownload)/(?P.+)/?$', handlers.MovieHandler), ], debug=settings.DEBUG, - log_function=logging.debug, + log_function=log_request, static_path=settings.STATIC_PATH, static_url_prefix=settings.STATIC_URL ) -- 2.39.5