Page cache in Python + Flask with Flask-Caching

Page cache in Python + Flask with Flask-Caching

Package installation

Use the Flask-Caching package

$ pip install Flask-Caching

How to use with Blueprint

Directory structure Click here for a description of the Blueprint directory structure (https://qiita.com/morita-toyscreation/items/8471a2ccc481a840c372)

weekend-hackathon/  
|-- app  
|   |-- views  
|   |    `-- sample.py  
|   |-- cache.py
|   `-- __init__.py  
|-- app.py  
|-- Dockerfile  
`-- requirements.txt 

cache.py

Use simple for page cache, memcached, redis, etc.

from flask_caching import Cache

cache = Cache(config={"CACHE_TYPE": "simple"})

__init__.py

Apply cache settings to app by doing cache.init_app (app)

from flask import Flask

from app.cache import cache
from app.views.about import about
from app.views.main import main


def get_app() -> Flask:
    app = Flask(__name__)
    cache.init_app(app)
    _register_blueprint(app)
    return app


def _register_blueprint(app: Flask) -> None:
    app.register_blueprint(about)
    app.register_blueprint(main)

sample.py

@ cache.cached (timeout = 50) By adding a decorator, the target page becomes a page cache.

from flask import Blueprint

from app.cache import cache

sample = Blueprint("sample", __name__)


@sample.route("/")
@cache.cached(timeout=50)
def index():
    print("sample.index")
    return "sample.index"

Recommended Posts

Page cache in Python + Flask with Flask-Caching
Programming with Python Flask
Create a new page in confluence with Python
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Use HTTP cache in Python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Web application with Python + Flask ② ③
Web application with Python + Flask ④
(For myself) Flask_8 (Add / Edit / Delete in database with python)
Number recognition in images with Python
Testing with random numbers in Python
SNS Python basics made with Flask
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
[Python] Use Basic/Digest authentication with Flask
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Application development with Docker + Python + Flask
Get started with Python in Blender
Working with DICOM images in Python
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Ant book in python: page 42 coin problem
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Experience Linux speeding up with Page cache
Tips for dealing with binaries in Python
POST variously with Python and receive with Flask
Display Python 3 in the browser with MAMP
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Ant book in python: page 43 Interval scheduling
[Personal note] Web page scraping with python3
Dealing with "years and months" in Python
Process multiple lists with for in Python
Replace non-ASCII with regular expressions in Python
Connect with mysql.connector with ssh tunnel in Python 3.7
Launch a Flask app in Python Anywhere
One liner webServer (with CGI) in python
Easy web app with Python + Flask + Heroku
Get Started with TopCoder in Python (2020 Edition)
[Python] Quickly create an API with Flask