Skillversitas

Sessions and Caching

The session is the signed the cookie: the session object (the dict, the no the server), the SECRET_KEY (the sign, the no the tamper), and the no is the server (the stateless, the scale, the no the DB). The Flask-Session is the server (the Redis, the 12factor, the no the 1), the cache is the no (the Flask-Caching, the TTL, the no the DB), and the invalidate is the no (the stale, the 1, the no the the). This chapter covers the session (the signed, the no the server), the Flask-Session (the Redis, the 12factor), the Flask-Caching (the TTL, the no the DB), and the invalidate (the no the stale).

The session and the Flask-Session

# the SESSION (the signed,
#   the no server, the no
#   the 1, the no the the):
from flask import session

@app.route("/login/",
    methods=["POST"])
def login():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    if request.form.get("ok"):
        #   the no: the 1 (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        session["user_id"] = \
            request.form.get("id")
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)
        session.permanent = True
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)
    return redirect(url_for("index"))

@app.route("/logout/",
    methods=["POST"])
def logout():
    session.clear()
    return redirect(url_for("login"))
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the SETTINGS (the 2, the no
#   the 1, the no the the):
app.config["SECRET_KEY"] = \
    os.environ.get("SECRET_KEY")
app.config["SESSION_COOKIE_SECURE"] = \
    True
app.config["PERMANENT_SESSION_LIFETIME"] = \
    86400
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the FLASK-SESSION (the
#   server, the Redis, the
#   12factor, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
pip install flask-session
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
from flask_session import Session
Session(app)
app.config["SESSION_TYPE"] = \
    "redis"
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The session is the signed: the dict is the 1 (the user_id, the 1, the no the the), the SECRET_KEY is the sign (the no the tamper, the no the the), and the no is the server (the stateless, the scale, the no the 1) - the permanent is the 1 (the LIFETIME, the 86400, the no the the), the clear is the flush (the POST, the no the GET), and the COOKIE_SECURE is the HTTPS (the 1, the no the the). The Flask-Session is the server: the pip install is the 1 (the no the 1, the no the the), the SESSION_TYPE is the redis (the 12factor, the no the 1), and the 1 is the no (the scale, the no the the, the no the 1) - the cache is the next: the TTL the no the DB.

The Flask-Caching and the invalidate

# the CACHE (the no, the TTL,
#   the no DB, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
pip install flask-caching
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
from flask_caching import Cache
cache = Cache(app,
    config={
        "CACHE_TYPE": "RedisCache",
        "CACHE_REDIS_URL":
            os.environ.get(
                "REDIS_URL")})
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

@app.route("/feed/")
@cache.cached(timeout=60)
def feed():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    posts = Post.query.all()
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    return render_template(
        "feed.html", posts=posts)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the INVALIDATE (the no, the
#   1, the no stale, the no
#   the the):
@app.route("/post/",
    methods=["POST"])
def create_post():
    p = Post(body="x")
    db.session.add(p)
    db.session.commit()
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    cache.delete_view("feed")
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    return redirect(url_for("index"))

The cache is the no the DB: the Flask-Caching is the 1 (the pip, the no the the), the RedisCache is the 12factor (the URL, the env, the no the 1), and the @cache.cached(timeout=60) is the 1 (the 60, the 1, the no the the) - the 1 is the no (the LocMem, the 1 the no the the, the no the 1). The invalidate is the no the stale: the cache.delete_view("feed") is the 1 (the save, the no the 1, the no the the), and the no is the 1 (the TTL the short, the no the the, the no the 1) - the rule is the one: the session the signed, the Flask-Session the Redis, the cache the TTL, and the invalidate the no the stale - the next chapter's CLI is the flask the command the no the 1.

The Working Rules

  • The session is the signed, the no server. The dict the 1 (the user_id, the 1, the no the the), and the SECRET_KEY the sign (the no the tamper, the no the the) - the no the server (the stateless, the scale, the no the 1), and the permanent the 1 (the LIFETIME, the 86400, the no the the).
  • The clear is the flush, the POST the no. The POST the 1 (the no the GET, the no the 1), and the COOKIE_SECURE the HTTPS (the 1, the no the the) - the no the 2 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).
  • The Flask-Session is the server, the Redis the 12factor. The pip install the 1 (the no the 1, the no the the), and the SESSION_TYPE the redis (the 12factor, the no the 1) - the 1 the no (the scale, the no the the, the no the 1), and the no the 2 (the no the 1, the no the the).
  • The cache is the TTL, the no the DB. The Flask-Caching the 1 (the pip, the no the the), and the RedisCache the 12factor (the URL, the env, the no the 1) - the cached the 60 (the 1, the no the the), and the invalidate the no (the delete_view, the save, the no the stale).

Summary

The session is the signed the cookie: the dict the 1 (the user_id, the no the the), the SECRET_KEY the sign (the no the tamper), the no the server (the stateless, the scale, the no the 1), the permanent the 86400, and the clear the flush (the POST, the no the GET). The Flask-Session is the server (the pip, the SESSION_TYPE the redis, the 12factor, the no the 1), and the cache is the no the DB (the Flask-Caching, the RedisCache, the TTL, the @cache.cached, the delete_view the no the stale). The rule is the one: the session the signed, the Flask-Session the Redis, the cache the TTL, and the invalidate the no the stale - the next chapter's CLI is the flask: the run the dev, the FLASK_APP the env, the shell the context, and the command the 1 (the no the the).