Black Lives Matter. Please consider donating to Black Girls Code today.

Using Redis in Dash

Hi! I am trying to use Flask-caching with redis,
and I get an error message “ConnectionRefusedError: [Errno 61] Connection refused” when I try to set the cache.

cache = Cache(app.server, config={
‘CACHE_TYPE’: ‘redis’,
‘CACHE_REDIS_URL’: os.environ.get(‘REDIS_URL’, ‘’)})

cache.set(“test”, “testing”)

How do I set and get data from cache?

Thank you!

Hi! i have redis on same machine that runs dash app with gunicorn so my working config is
app = dash.Dash(name)
server=app.server

CACHE_CONFIG={
‘CACHE_TYPE’:‘redis’,
‘CACHE_REDIS_URL’: os.environ.get(‘REDIS_URL’,‘localhost:6379’)
}

cache=Cache()
cache.init_app(server,config=CACHE_CONFIG)

1 Like

This looks very similar to the code example/tutorial > https://dash.plot.ly/sharing-data-between-callbacks

I found this discussion after searching for solution to this error message.

ValueError: Redis URL must specify one of the followingschemes (redis://, rediss://, unix://)

I’m not using gunicorn (roman mentioned using gunicorn) and the dash.plot.ly code example does not mention using gunicorn, so I’m a bit lost here.

suggestions?


Traceback (most recent call last): File “caching_nd_signalling.py”, line 63, in cache.init_app(app.server, config=CACHE_CONFIG) File “/home/bmt/anaconda3/lib/python3.7/site-packages/flask_caching/init.py”, line 212, in init_app self._set_cache(app, config) File “/home/bmt/anaconda3/lib/python3.7/site-packages/flask_caching/init.py”, line 239, in _set_cache app, config, cache_args, cache_options File “/home/bmt/anaconda3/lib/python3.7/site-packages/flask_caching/backends/init.py”, line 94, in redis kwargs[“host”] = redis_from_url(redis_url, db=kwargs.pop(“db”, None)) File “/home/bmt/anaconda3/lib/python3.7/site-packages/redis/utils.py”, line 19, in from_url return Redis.from_url(url, db, **kwargs) File “/home/bmt/anaconda3/lib/python3.7/site-packages/redis/client.py”, line 638, in from_url connection_pool = ConnectionPool.from_url(url, db=db, **kwargs) File “/home/bmt/anaconda3/lib/python3.7/site-packages/redis/connection.py”, line 999, in from_url ‘schemes (%s)’ % valid_schemes) ValueError: Redis URL must specify one of the followingschemes (redis://, rediss://, unix://)

I actually configured my Redis server this morning and ran into the same thing. If Redis is running on a remote server, there’s a few things you can do:

  1. Update the firewall on Redis server to allow traffic to Redis port (6379)

  2. Update redis.conf to specify what IP addresses to use. By default, Redis binds to 127.0.0.1, which does not allow remote connections. You can comment out the bind, which binds all interfaces or specify exactly which IP addresses should be included in the bind. Example: #bind 127.0.0.1 or bind 127.0.0.1 10.x.x.x

  3. Redis runs in protected mode by default, you can disable this by updating to protected-mode no in the redis.conf file. If you do this though, you should probably use a username and password to authenticate the connection though. Or just limit the remote connections to be from specific hosts.