Sessions and Caching
The session is the signed the cookie: the request.session (the dict, the write, the no the tamper), the SESSION_ENGINE (the db, the file, the cache), and the rotation (the cycle_key, the login, the no the 1). The cache is the 2 (the locmem, the Redis, the 12factor), the cache decorator is the no the DB (the 1 the arg, the key the 1), and the template fragment is the no the render (the {% cache %}, the 2 the no). This chapter covers the session (the dict, the rotation, the engine), the cache (the 2, the key, the TTL), the @cache_page (the view, the no the DB), and the {% cache %} (the fragment, the no the render).
The session: The dict and the rotation
# the SESSION (the dict, the no
# tamper, the signed):
def note_list(request):
# the read (the no, the
# the default, the no
# the KeyError):
n = request.session.get(
"note_views", 0)
# the write (the 1, the
# the 1, the no
# the flush):
request.session["note_views"] = \
n + 1
# the no: the 1 (the key,
# the 1, the no
# the 1):
request.session.pop("tmp", None)
# the 2: the flush (the
# the no, the 1, the
# the no the 1):
# request.session.flush()
# the no: the 1 (the no
# the 1, the no
# the 1):
# the ROTATION (the cycle_key, the
# login, the no the 1):
def my_login(request):
user = authenticate(request, **kw)
if user is not None:
login(request, user)
# the no: the 1 (the
# the key, the no
# the 1, the no
# the 1):
request.session.cycle_key()
# the no: the no (the 1, the
# the 1, the no
# the 1)
# the ENGINE (the 3, the no
# the 1):
# the 1: the db (the default,
# the table, the no
# the 1):
# the 2: the file (the no,
# the 1, the no
# the scale):
# the 3: the cache (the
# the Redis, the no
# the table, the no
# the 1):
# the SESSION_ENGINE =
# "django.contrib.sessions."
# "backends.cache"
# the no: the 1 (the scale,
# the no the table, the
# no the 1)
The session is the dict: the request.session.get is the read (the default, the no the KeyError), the [key] = value is the write (the 1 the 1, the no the flush), the pop is the no (the 1, the 1 the no), and the flush is the no (the 1, the no the 1) - the signed is the no the tamper (the cookie the hash, the no the the the read, the no the secure the no the 12factor). The rotation is the cycle_key: the login is the 1 (the key the no the 1, the no the 1, the no the the), and the password change is the 1 (the no, the 1). The engine is the 3: the db (the default, the table, the no the 1), the file (the no, the scale the no), and the cache (the Redis, the table the no, the scale the 1) - the SESSION_COOKIE_AGE is the 2 the week (the 1 the 1), the SESSION_EXPIRE_AT_BROWSER_CLOSE is the 1 (the 1 the no the 1), and the cache is the next: the 2 the no the DB.
The cache: The 2 backend and the decorator
# the CACHE (the 2, the Redis, the
# 12factor, the no the DB):
# the settings (the 2, the no
# the 1, the no the 1):
CACHES = {
"default": {
"BACKEND": "django.core.cache."
"backends.RedisCache",
"LOCATION": os.environ.get(
"REDIS_URL",
"redis://127.0.0.1:6379/1"),
"TIMEOUT": 300, # the 5
# the min
},
"fragments": {
"BACKEND": "django.core.cache."
"backends.LocMemCache",
"LOCATION": "fragments",
},
}
# the no: the 1 (the 1, the
# the no the 1, the no
# the 1):
from django.core.cache import cache
# the 1: the get/set (the key,
# the TTL, the no
# the 1):
def note_list(request):
key = "notes:all"
notes = cache.get(key)
if notes is None:
notes = list(Note.objects.
values("title", "created"))
cache.set(key, notes, 300)
# the 2: the no (the 1,
# the 1, the no
# the DB):
return JsonResponse(
{"notes": notes})
# the 2: the invalidate (the no,
# the 1, the no
# the stale):
def note_save(request, pk):
n = get_object_or_404(Note,
pk=pk)
n.title = request.POST.get(
"title", "")
n.save()
cache.delete("notes:all")
# the no: the no (the stale,
# the 1, the no
# the no)
# the 3: the @cache_page (the
# the view, the no
# the DB, the no
# the 1):
from django.views.decorators.cache import \
cache_page
@cache_page(60 * 15)
def note_list(request):
# the no: the 1 (the GET,
# the no, the no
# the DB):
return render(request,
"notes/list.html")
# the no: the no (the no, the
# the 1, the no
# the 1):
The cache is the 2: the LocMemCache (the 1 the process, the dev, the no the scale) and the RedisCache (the 1 the no, the 12factor, the scale the 1, the no the the) - the TIMEOUT is the TTL (the 300 the 5 the min, the 0 is the no the 1, the default the 300). The get/set is the 1: the key is the 1 (the notes:all, the namespace, the no the clash), the TTL is the 1 (the 300, the no the stale), and the delete is the no (the save, the no the stale, the no the 1) - the @cache_page is the view (the 900 the 15 the min, the GET the no the DB, the POST is the no the cache, the no the the), and the {% cache %} is the fragment (the 2 the no, the 60 * 5 the 5 the min, the no the the, the no the the the render) - the rule is the one: the session the signed, the rotation the login, the cache the 2, the @cache_page the view, and the {% cache %} the fragment - the next chapter's middleware is the before the after the no the 1 the class.
The Working Rules
- The session is the dict, the signed the no tamper. The .get the default (the no the KeyError), the [key] the write (the 1, the no the flush), and the flush the no (the 1, the no the 1) - the cookie the hash (the no the read, the no the secure the 12factor), and the cycle_key the login (the no the 1, the no the 1).
- The engine is the 3, the cache the 12factor. The db the default (the table, the no the 1), the file the no (the scale the no), and the cache the Redis (the table the no, the scale the 1) - the SESSION_COOKIE_AGE the 2 the week (the 1), and the EXPIRE_AT_BROWSER_CLOSE the 1 (the no the 1).
- The cache is the 2, the Redis the scale. The LocMemCache the 1 (the process, the dev), and the RedisCache the no (the 12factor, the scale the 1) - the TIMEOUT the TTL (the 300 the 5, the 0 the no), and the key the namespace (the notes:all, the no the clash).
- The @cache_page is the view, the no the DB. The 900 the 15 (the GET the no the DB), and the POST the no the cache (the no the 1) - the delete the save (the no the stale, the no the 1), and the {% cache %} the fragment (the 5 the min, the no the render, the no the user's data).
Summary
The session is the signed the cookie: the request.session the dict (the .get the default, the write the 1, the flush the no, the pop the 1), the signed the no the tamper (the cookie the hash, the no the read, the no the secure), the rotation the cycle_key (the login the no the 1, the password the 1), and the engine the 3 (the db the default the table, the file the no the scale, the cache the Redis the 12factor). The cache is the 2 (the LocMemCache the 1 the dev, the RedisCache the no the 12factor, the TIMEOUT the 300 the 5): the get/set the key the TTL, the delete the no (the save, the no the stale), the @cache_page the view (the 15 the min, the GET the no the DB, the POST the no the cache), and the {% cache %} the fragment (the 5 the min, the no the render, the no the user's data). The rule is the one: the session the signed, the rotation the login, the cache the 2, and the invalidate the no the stale - the next chapter's middleware is the hook: the before, the after, the order, and the custom class the 1 the no the view.