Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Created May 12, 2018 21:07
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruanbekker/9243d334c27413eadbac738284de80a2 to your computer and use it in GitHub Desktop.
Save ruanbekker/9243d334c27413eadbac738284de80a2 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