Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

We will maintain a set in Redis, which will store the products visited recently. This will be populated whenever we visit a product. The entry will expire in 10 minutes. This change goes in views.py:

from my_app import redis 
 
@catalog.route('/product/<id>') 
def product(id): 
    product = Product.query.get_or_404(id) 
    product_key = 'product-%s' % product.id 
    redis.set(product_key, product.name) 
    redis.expire(product_key, 600) 
    return 'Product - %s, $%s' % (product.name, product.price) 
It is good practice to fetch the expire time, that is, 600, from a configuration value. This can be set on the application object in my_app/__init__.py, and can then be fetched from there.

In the preceding method, note the set() and expire() methods on ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content