title: How to cache a variable with Flask?
body: <p>I am building a web form using Flask and would like the user to be able to enter multiple entries, and give them the opportunity to regret an entry with an undo button, before sending the data to the database. I am trying to use <a href="https://flask-caching.readthedocs.io/en/latest/" rel="nofollow noreferrer">Flask-Caching</a> but have not managed to set it up properly.</p>
  
  <p>I have followed <a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" rel="nofollow noreferrer">The Flask Mega-Tutorial</a> for setting up Flask (this is my first Flask app).</p>
  
  <pre><code>+---app
  |   |   forms.py
  |   |   routes.py
  |   |   __init__.py
  |   +---static
  |   +---templates
  </code></pre>
  
  <p>I wonder how I need to configure the Flask app to basically be able to do the following things:</p>
  
  <pre><code>cache.add("variable_name", variable_data)
  variable_name = cache.get("variable_name")
  cache.clear()
  </code></pre>
  
  <p>in one of the pages (functions with @app.route decorators)?</p>
  
  <p>In app.<strong>init</strong>.py I have:</p>
  
  <pre><code>from flask import Flask
  from config import Config
  from flask_caching import Cache
  
  app = Flask(__name__)
  app.config.from_object(Config)
  cache = Cache(app, config={'CACHE_TYPE': 'simple'})
  
  from app import routes
  </code></pre>
  
  <p>In routes.py I have:</p>
  
  <pre><code>from flask import current_app
  </code></pre>
  
  <p>and I use code below when I try to call the cache.</p>
  
  <pre><code>current_app.cache.add("variable_name", variable_data)
  </code></pre>
  
  <p>What I get when trying to use the form is the following error:</p>
  
  <pre><code>AttributeError: 'Flask' object has no attribute 'cache'
  </code></pre>
  
  <p>Pretty much all tutorials I've found have simply had the app declaration and all the routes in the same module. But how do I access the cache when I have the routes in another module?</p>