Created
May 12, 2018 21:07
-
-
Save ruanbekker/9243d334c27413eadbac738284de80a2 to your computer and use it in GitHub Desktop.
Flask-Cache Example with Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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