thadeusb/flask-cache

Flask-cache redis cache failed

ZhouLihua opened this issue · 4 comments

When use redis as the Cache, it raise ImportError: redis is not a valid FlaskCache backend.

my config:

Flask-Cache redis config

DEBUG = True
CACHE_TYPE = "redis"
CACHE_DEFAULT_TIMEOUT = 300
CACHE_REDIS_HOST = "127.0.0.1"
CACHE_REDIS_PORT = 6379
CACHAE_REDIS_PASSWORD = "123456"
CACHE_REDIS_DB = 0

It is possible that the application is running outside of the python environment that contains werkzeug or redis...

If the following two imports fail, then the redis backend is not created.

from werkzeug.contrib.cache import RedisCache
from redis import from_url as redis_from_url

same problem. the imports fail...

so what's the fix for this ?

The issue is here:

app.extensions['cache'][self] = cache_obj(
                app, config, cache_args, cache_options)

But the first parameter of <class 'werkzeug.contrib.cache.RedisCache'> constructor is host. So the next code is running:

if isinstance(host, string_types):
     ...
    self._client = redis.Redis(host=host, port=port, password=password,
                                       db=db, **kwargs)
else:
    self._client = host

else branch is running and in the end we have Flask application instance in _client variable instead of Redis instance.