You can use Redis Labs Cloud to cache application data. Redis Cloud is a fully-managed service offering a variety of options for running Redis.
Redis Labs offers a free tier that provides 30 MB of storage.
To use Redis by Redis Labs:
- Sign up for a Redis Labs Cloud account.
- Create a Redis Database using Redis Cloud. Be sure to specify
GCE/us-central1as the Cloud for the lowest latency. Specify a strong password as your Redis instance will be publicly accessible.
- Note the
Endpointfor the database.
Installing a Redis client libraryWe recommend using the
redisPython library for accessing your Redis instance.
redis in your
Flask==1.0.2 gunicorn==19.9.0 redis==3.1.0
If running locally, install your dependencies using
pip install -r requirements.txt
Connecting to Redis by Redis LabsConfigure environment variables in your
app.yamlwith the endpoint and password for your Redis by Redis Labs database:
env_variables: REDIS_HOST: your-redis-host REDIS_PORT: your-redis-port REDIS_PASSWORD: your-redis-password
redis_host = os.environ.get('REDIS_HOST', 'localhost') redis_port = int(os.environ.get('REDIS_PORT', 6379)) redis_password = os.environ.get('REDIS_PASSWORD', None) redis_client = redis.StrictRedis( host=redis_host, port=redis_port, password=redis_password)
Testing Redis locally
If you are testing your app locally, you can still connect to your the Redis Labs Redis database. However, you might want to run a local instance of Redis to avoid interacting with production data.
Follow the directions in the Redis documentation to install and run Redis locally. Note that it currently isn't possible to run Redis locally on Windows.