flask-cache icon indicating copy to clipboard operation
flask-cache copied to clipboard

Flask-cache redis cache failed

Open ZhouLihua opened this issue 9 years ago • 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

ZhouLihua avatar Oct 23 '15 06:10 ZhouLihua

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

thadeusb avatar Jan 26 '16 23:01 thadeusb

same problem. the imports fail...

so what's the fix for this ?

AndreiD avatar Jan 30 '16 21:01 AndreiD

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

thadeusb avatar Jan 31 '16 17:01 thadeusb

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.

ihor-nahuliak avatar Mar 28 '18 23:03 ihor-nahuliak