Created
September 21, 2011 15:17
-
-
Save coffindragger/1232331 to your computer and use it in GitHub Desktop.
Django Caching Notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Cache One Object (Active Homepage Layout) | |
by_pk or by_* (any field) | |
lazy queryset methods (loaded and cached when first used) | |
warm queryset methods (preloaded by warming) | |
- Cache Entire Table (Categories) | |
load entire table into in-memory hash, indexes on pk and slug | |
.cached manager for loading from cached, use_for_related=True | |
- Cache Busy Queries (Page 1 of Story List by Category) | |
pre-warmed, never expire | |
when invalidated, re-warm instead of flush | |
- Cache HTML (Template Tag) | |
template chunk (homepage optimization) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Homepage(cachemodels.CacheModel): | |
is_active = models.BooleanField() | |
name = models.CharField() | |
@cachemodels.cached_method | |
def featured_books(self): | |
return self.homepagebook_set.all() | |
class HomepageBook(cachemodels.CacheModel): | |
homepage = models.ForeignKey(Homepage) | |
book = models.ForeignKey(Book) | |
class Category(cachemodels.CachedTable): | |
name = models.CharField() | |
slug = models.SlugField() | |
class Author(cachemodels.CacheModel): | |
name = models.CharField() | |
bio = models.TextField() | |
class BookManager(cachemodels.CacheModelManager): | |
@cachemodels.cached_query | |
def cached_listings(self, category=None, page=1, limit=10): | |
qs = self.all() | |
if category: | |
qs.filter(category=category) | |
offset = (page-1)*limit | |
return qs[offset:offset+limit] | |
@cachemodels.cached_query | |
def popular(self, limit=5): | |
return self.filter(is_active=True).order_by('-denorm_popularity')[:limit] | |
class Book(cachemodels.CacheModel): | |
is_active = models.BooleanField() | |
slug = models.SlugField() | |
name = models.CharField() | |
author = models.ForeignKey(Author) | |
category = models.ForeignKey(Category) | |
denorm_popularity = models.IntegerField() | |
@cachemodels.cached_method | |
def related_books(self, how_many=5): | |
return Book.objects.filter(category=self.category) | |
@cachemodels.denormalized_field('denorm_popularity') | |
def popularity(self): | |
return Comment.objects.filter(book=self).count() | |
class Comment(cachemodels.CacheModel): | |
book = models.ForeignKey(Book) | |
body = models.TextField() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>{{book.name}}</h2> | |
<h3>{{book.author}}</h3> | |
<ul> | |
{%for related in book.related_books%} | |
<li> | |
<img src="{{related.cover}}"/> | |
<p>{{related.name}}</p> | |
<p class="author">{{related.author}}</p> | |
<p>{{related.category}}</p> | |
</li> | |
{%endfor%} | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def book_detail(request, book_slug): | |
book = Book.objects.get_by_slug(book_slug) | |
return render_template(request, 'book/detail.html', {'book': book}) | |
def book_listing(request, category=None): | |
books = Book.objects.cached_listings(category=category, page=request.GET.get('page','1')) | |
return render_template(request, 'book/listing.html', {'books': books}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment