Table of Contents

Flask

Here we explain how you setup and install MemCachier with Flask. While Flask has a built-in caching backend its features are limited to manual caching. For this reason we recommend you use the Flask-Caching package. Flask-Caching supports memoization, fragment caching (Jinja2 snippets), and whole view caching. For more details about how to use Flask-Caching please refer to its documentation

Flask-Caching requires the pylibmc client which relies on the C libmemcached library. This should be fairly straight-forward to install with your package manager on Linux or Windows. For Mac OSX users, homebrew provides and easy solution. We also have a blog post for Ubuntu users on how to do this.

Once libmemcached is installed, then install Flask-Caching and pylibmc:

$ pip install Flask-Caching pylibmc

Be sure to update your requirements.txt file with these new requirements (note that your versions may differ than what’s below):

Flask-Caching==1.4.0
pylibmc==1.5.2

Next, configure your Flask app the following way:

The values for MEMCACHIER_SERVERS, MEMCACHIER_USERNAME, and MEMCACHIER_PASSWORD are listed on your cache overview page. Make sure to add them to your environment.

After this, you can start writing cache code in your Flask app:

Function memoization

Flask-Caching provides a decorator to memoize functions. This basically means when the function is called, Flask-Cache will check if the result is in the cache and if it is not it will run the function and save the result to the cache. The memoize decorator works as follows:

If you need to invalidate stale data you can either delete all memoized results for a function with cache.delete_memoized(run_expensive_computation) or a result for a specific parameter with cache.delete_memoized(run_expensive_computation, parameter).

View caching

Flask-Caching also provides a decorator to cache views:

It is important to note that the @cache.cached() decorator is directly above the definition of the index() function, i.e., below the @bp.route() decorator.

The views are cached with a key of the form 'view/' + request.path. This is important to know if you ever need to invalidate a cached view. You can do that with cache.delete('view/'+path_of_stale_view)

Jinja2 snippet caching

Flask-Caching provides a Jinja2 control flow statement to cache snippets. The statement has the form {% cache timeout, key, ... %} where all additional parameters after the key are just appended to the key. In practice this may look as follows:

Here the timeout is None but it can also be a variable that contains a time or an integer denoting seconds.

The cached snippet from the above example can be invalidated (deleted) as follows:

Session caching

Memcache works well for storing information for short-lived sessions that time out. However, because Memcache is a cache and therefore not persistent, long-lived sessions are better suited to permanent storage options, such as your database.

To store sessions in Memcache, you need Flask-Session.

$ pip install Flask-Session pylibmc

Be sure to update your requirements.txt file with these new requirements (note that your versions may differ than what’s below):

Flask-Session==0.3.1
pylibmc==1.5.2

Now, configure Flask-Session:

You can now use sessions in your app like so: