Cache with Django
Redis
Ivaylo Donchev
Web developer @ ~ 2 years
Student @ FMI ~ 4 years
Some questions:
What am I caching...
Where to store my cache?
Cache invalidation?
Hmm, what about race conditions?
CACHE
Template part
{% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}
A string
>>> cache.set('my_key', 'hello, world!', 30)
>>> cache.get('my_key')
Query result
>>> cache.set('all_users', User.objects.all(), 30)
>>> cache.get('all')
Out[1]: <QuerySet [<User: >]>
A page
@cache_page(60 * 15)
def my_view(request):
pass
Where to store it ?
Django's own memory
cache server
DB
file
FAST
SLOW
Unstable
Stable
Where to store it ?
IN DJANGO
IN
FILESYSTEM
HOW IT WORKS ?
- Purge
- TTL :/
- Refresh
- Ban
Cache invalidation strategies
Race conditions
After two hours of debugging
Why this is good/bad ?
Why this is good/bad ?
Sample json response:
{
"id": 1,
"email": "ivodonchev267@gmail.com",
"password": hack-me-pass,
"last_login": "2017-02-04T20:55:08.914461+00:00",
"profile": {
"id": 1,
"full_name": "Ivaylo Donchev,
"age": 22
},
},
Caching string vs caching data structures
When you make livedemo
Q/A
Cache
By Ivaylo Donchev
Cache
- 741