1- .. django-async-cache documentation master file, created by
2- sphinx-quickstart on Mon Jul 30 21:40:46 2012.
3- You can adapt this file completely to your liking, but it should at least
4- contain the root `toctree` directive.
5-
61================
72Django Cacheback
83================
@@ -17,7 +12,7 @@ synchronously.
1712.. _rq : http://python-rq.org/
1813
1914Using this library, you can rework your views so that all reads are from
20- cache - which can be a significant performance boost.
15+ cache - which can be a significant performance boost.
2116
2217A corollary of this technique is that cache stampedes can be easily avoided,
2318avoiding sudden surges of expensive reads when cached items becomes stale.
@@ -37,24 +32,24 @@ Consider a view for showing a user's tweets:
3732 from myproject.twitter import fetch_tweets
3833
3934 def show_tweets(request, username):
40- return render(request, 'tweets.html',
35+ return render(request, 'tweets.html',
4136 {'tweets': fetch_tweets(username)})
4237
4338This works fine but the ``fetch_tweets `` function involves a HTTP round-trip and
44- is slow.
39+ is slow.
4540
4641Performance can be improved by using Django's `low-level cache API `_:
4742
4843.. _`low-level cache API` : https://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs#the-low-level-cache-api
49-
44+
5045.. sourcecode :: python
5146
5247 from django.shortcuts import render
5348 from django.cache import cache
5449 from myproject.twitter import fetch_tweets
5550
5651 def show_tweets(request, username):
57- return render(request, 'tweets.html',
52+ return render(request, 'tweets.html',
5853 {'tweets': fetch_cached_tweets(username)})
5954
6055 def fetch_cached_tweets(username):
@@ -88,7 +83,7 @@ cache asynchronously instead of during the request/response cycle:
8883 from myproject.tasks import update_tweets
8984
9085 def show_tweets(request, username):
91- return render(request, 'tweets.html',
86+ return render(request, 'tweets.html',
9287 {'tweets': fetch_cached_tweets(username)})
9388
9489 def fetch_cached_tweets(username):
@@ -116,7 +111,7 @@ where the ``myproject.tasks.update_tweets`` task is implemented as:
116111 def update_tweets(username, ttl):
117112 tweets = fetch_tweets(username)
118113 now = datetime.datetime.now()
119- cache.set(username, (tweets, now+ttl), 2592000)
114+ cache.set(username, (tweets, now+ttl), 2592000)
120115
121116Some things to note:
122117
@@ -147,7 +142,7 @@ Here's the same functionality implemented using a django-cacheback decorator:
147142 from cacheback.decorators import cacheback
148143
149144 def show_tweets(request, username):
150- return render(request, 'tweets.html',
145+ return render(request, 'tweets.html',
151146 {'tweets': cacheback(60*15, fetch_on_miss=False)(fetch_tweets)(username)})
152147
153148Here the decorator simply wraps the ``fetch_tweets `` function - nothing else is
@@ -206,4 +201,3 @@ Indices and tables
206201* :ref: `genindex `
207202* :ref: `modindex `
208203* :ref: `search `
209-
0 commit comments