Skip to content

Commit

Permalink
Fix #15
Browse files Browse the repository at this point in the history
- Check for backend availability when configuring cache, not when making first query.
- Default backend is now chosen from sqlite and memory.
  • Loading branch information
reclosedev committed Oct 9, 2013
1 parent 3cfdc11 commit 242335c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 16 additions & 0 deletions requests_cache/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@
registry['redis'] = RedisCache
except ImportError:
RedisCache = None


def create_backend(backend_name, cache_name, options):
if backend_name is None:
backend_name = _get_default_backend_name()
try:
return registry[backend_name](cache_name, **options)
except KeyError:
raise ValueError('Unsupported backend "%s" try one of: %s' %
(backend_name, ', '.join(registry.keys())))


def _get_default_backend_name():
if 'sqlite' in registry:
return 'sqlite'
return 'memory'
17 changes: 9 additions & 8 deletions requests_cache/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CachedSession(OriginalSession):
""" Requests ``Sessions`` with caching support.
"""

def __init__(self, cache_name='cache', backend='sqlite', expire_after=None,
def __init__(self, cache_name='cache', backend=None, expire_after=None,
allowable_codes=(200,), allowable_methods=('GET',),
**backend_options):
"""
Expand All @@ -44,6 +44,8 @@ def __init__(self, cache_name='cache', backend='sqlite', expire_after=None,
are prefixed with ``'cache_name:'``
:param backend: cache backend name e.g ``'sqlite'``, ``'mongodb'``, ``'redis'``, ``'memory'``.
(see :ref:`persistence`). Or instance of backend implementation.
Default value is ``None``, which means use ``'sqlite'`` if available,
otherwise fallback to ``'memory'``.
:param expire_after: number of seconds after cache will be expired
or `None` (default) to ignore expiration
:type expire_after: float
Expand All @@ -55,12 +57,9 @@ def __init__(self, cache_name='cache', backend='sqlite', expire_after=None,
:ref:`sqlite <backends_sqlite>`, :ref:`mongo <backends_mongo>`
and :ref:`redis <backends_redis>` backends API documentation
"""
if isinstance(backend, basestring):
try:
self.cache = backends.registry[backend](cache_name, **backend_options)
except KeyError:
raise ValueError('Unsupported backend "%s" try one of: %s' %
(backend, ', '.join(backends.registry.keys())))
if backend is None or isinstance(backend, basestring):
self.cache = backends.create_backend(backend, cache_name,
backend_options)
else:
self.cache = backend

Expand Down Expand Up @@ -136,7 +135,7 @@ def cache_disabled(self):
self._is_cache_disabled = False


def install_cache(cache_name='cache', backend='sqlite', expire_after=None,
def install_cache(cache_name='cache', backend=None, expire_after=None,
allowable_codes=(200,), allowable_methods=('GET',),
session_factory=CachedSession, **backend_options):
"""
Expand All @@ -146,6 +145,8 @@ def install_cache(cache_name='cache', backend='sqlite', expire_after=None,
:param session_factory: Session factory. It should inherit :class:`CachedSession` (default)
"""
if backend:
backend = backends.create_backend(backend, cache_name, backend_options)
_patch_session_factory(
lambda : session_factory(cache_name=cache_name,
backend=backend,
Expand Down

0 comments on commit 242335c

Please sign in to comment.