thadeusb / flask-cache

Cache extension for Flask
http://packages.python.org/Flask-Cache/
Other
700 stars 185 forks source link

Flask-cache redis cache failed #126

Open ZhouLihua opened 9 years ago

ZhouLihua commented 9 years ago

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

thadeusb commented 8 years ago

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
AndreiD commented 8 years ago

same problem. the imports fail...

so what's the fix for this ?

thadeusb commented 8 years ago

Install redis-py using pip into your virtualenv and make sure your server is configured to use that virtualenv.

ihor-nahuliak commented 6 years ago

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.