Skip to content

Instantly share code, notes, and snippets.

@NobayeneSehloho
Forked from ruanbekker/flask_cache_redis.py
Created February 25, 2021 09:20
Show Gist options
  • Save NobayeneSehloho/d8fdb47c1e7c225c953a7d8b8ef8d456 to your computer and use it in GitHub Desktop.
Save NobayeneSehloho/d8fdb47c1e7c225c953a7d8b8ef8d456 to your computer and use it in GitHub Desktop.
Flask-Cache Example with Redis
from flask import Flask
from flask_caching import Cache
import random
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'redis', 'CACHE_REDIS_URL': 'redis://localhost:6379/0'})
@app.route("/route1")
@cache.cached(timeout=10)
def route1():
data = str(random.randint(0,100000)) + '\n'
return data
@app.route("/route2")
@cache.cached(timeout=20)
def route2():
data = str(random.randint(0,100000)) + '\n'
return data
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8098)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment