Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to fail silently, even when the Flask app is in debug mode. #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ python:
- "2.7"
- "3.3"
install:
- pip install . --use-mirrors
- pip install coverage --use-mirrors
- pip install redis --use-mirrors
- "if [[ $TRAVIS_PYTHON_VERSION == 2.7* ]]; then pip install pylibmc --use-mirrors; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == 3.3* ]]; then pip install python3-memcached --use-mirrors; fi"
- pip install .
- pip install coverage
- pip install redis
- "if [[ $TRAVIS_PYTHON_VERSION == 2.7* ]]; then pip install pylibmc; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == 3.3* ]]; then pip install python3-memcached; fi"
script: nosetests --with-coverage --cover-package=flask_cache
services:
- memcached
Expand Down
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ The following configuration values exist for Flask-Cache:
``CACHE_REDIS_URL`` URL to connect to Redis server.
Example ``redis://user:password@localhost:6379/2``.
Used only for RedisCache.
``CACHE_IGNORE_BACKEND_ERRORS`` Silently ignore backend exceptions, logging them instead.
Defaults to ``False`` when the Flask app is in debug mode,
and to ``True`` otherwise.
=============================== ==================================================================


Expand Down
15 changes: 9 additions & 6 deletions flask_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def init_app(self, app, config=None):
config.setdefault('CACHE_ARGS', [])
config.setdefault('CACHE_TYPE', 'null')
config.setdefault('CACHE_NO_NULL_WARNING', False)
config.setdefault('CACHE_IGNORE_BACKEND_ERRORS', not app.debug)

self._ignore_backends_errors = config['CACHE_IGNORE_BACKEND_ERRORS']

if config['CACHE_TYPE'] == 'null' and not config['CACHE_NO_NULL_WARNING']:
warnings.warn("Flask-Cache: CACHE_TYPE is set to null, "
Expand Down Expand Up @@ -288,7 +291,7 @@ def decorated_function(*args, **kwargs):
cache_key = decorated_function.make_cache_key(*args, **kwargs)
rv = self.cache.get(cache_key)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return f(*args, **kwargs)
Expand All @@ -299,7 +302,7 @@ def decorated_function(*args, **kwargs):
self.cache.set(cache_key, rv,
timeout=decorated_function.cache_timeout)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return f(*args, **kwargs)
Expand Down Expand Up @@ -528,7 +531,7 @@ def decorated_function(*args, **kwargs):
cache_key = decorated_function.make_cache_key(f, *args, **kwargs)
rv = self.cache.get(cache_key)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return f(*args, **kwargs)
Expand All @@ -539,7 +542,7 @@ def decorated_function(*args, **kwargs):
self.cache.set(cache_key, rv,
timeout=decorated_function.cache_timeout)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return rv
Expand Down Expand Up @@ -666,7 +669,7 @@ def add(self, b):
cache_key = f.make_cache_key(f.uncached, *args, **kwargs)
self.cache.delete(cache_key)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")

Expand All @@ -689,6 +692,6 @@ def delete_memoized_verhash(self, f, *args):
try:
self._memoize_version(f, delete=True)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")