From 3a5bca6f1ac393d00b91c1c71a25f371a921c799 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 5 Jan 2015 15:21:26 +0600 Subject: [PATCH] Multiple backend cache --- src/watson/registration.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/watson/registration.py b/src/watson/registration.py index 19bcc84..38fe877 100644 --- a/src/watson/registration.py +++ b/src/watson/registration.py @@ -264,7 +264,7 @@ class SearchContext(object): def __init__(self, context_manager): """Initializes the search index context.""" self._context_manager = context_manager - + def __enter__(self): """Enters a block of search index management.""" self._context_manager.start() @@ -578,15 +578,15 @@ default_search_engine = SearchEngine("default") # The cache for the initialized backend. -_backend_cache = None +_backends_cache = {} def get_backend(backend_name=None): """Initializes and returns the search backend.""" - global _backend_cache + global _backends_cache # Try to use the cached backend. - if _backend_cache is not None: - return _backend_cache + if backend_name in _backends_cache: + return _backends_cache[backend_name] # Load the backend class. if not backend_name: backend_name = getattr(settings, "WATSON_BACKEND", "watson.backends.AdaptiveSearchBackend") @@ -601,5 +601,5 @@ def get_backend(backend_name=None): )) # Initialize the backend. backend = backend_cls() - _backend_cache = backend + _backends_cache[backend_name] = backend return backend