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

Trying to setup redis + postgresql cache w/ flask-cache

Open AbeHandler opened this issue 10 years ago • 2 comments

I am having trouble configuring flask cache to work with redis. If I set CACHE_TYPE='redis' I get "ImportError: redis is not a valid FlaskCache backend"

If I set CACHE_TYPE = 'RedisCache' I get the following

cache = Cache(app, config={ 'CACHE_TYPE': 'RedisCache', 'CACHE_KEY_PREFIX': 'fcache', 'CACHE_REDIS_HOST': 'localhost', 'CACHE_REDIS_PORT': '6379', 'CACHE_REDIS_URL': 'redis://localhost:6379' })

@cache.memoize() def query(q): return q.all()

ERROR:flask_cache:Exception possibly due to cache backend. Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask_cache/init.py", line 528, in decorated_function cache_key = decorated_function.make_cache_key(f, _args, *_kwargs) File "/usr/local/lib/python2.7/dist-packages/flask_cache/init.py", line 381, in make_cache_key timeout=_timeout) File "/usr/local/lib/python2.7/dist-packages/flask_cache/init.py", line 350, in _memoize_version version_data_list = list(self.cache.get_many(*fetch_keys)) File "/usr/local/lib/python2.7/dist-packages/werkzeug/contrib/cache.py", line 521, in get_many return [self.load_object(x) for x in self._client.mget(keys)] AttributeError: 'Flask' object has no attribute 'mget'

Not sure what to do -- having trouble finding documentation.

AbeHandler avatar Jul 08 '14 20:07 AbeHandler

CACHE_TYPE='redis' is the correct setting.

You probably do not have a proper redis client installed that is accessible to the python environment you are running this code.

The code that loads the backend functions will not load the redis function, and throw and invalid cache type.

https://github.com/thadeusb/flask-cache/blob/master/flask_cache/backends.py#L56

Try making sure that redis-py is available and that you can import it cleanly from the environment Flask is running in.

thadeusb avatar Jul 08 '14 22:07 thadeusb

Got it. I will check that out and report back to this thread. If using redis + flask cache requires redis-py then you should mention that in the docs to help poor mortals like me. I've been trying to piece this together from google + IRC + stackoverflow and am finding little documentation. Here is an open stack overflow thread on the same problem http://stackoverflow.com/questions/24589123/how-to-cache-sql-alchemy-calls-with-flask-cache-and-redis

AbeHandler avatar Jul 09 '14 01:07 AbeHandler