擅长:python、mysql、java
<p>又一个很好的例子CacheMixin
<a href="https://gist.github.com/cyberdelia/1231560" rel="noreferrer">from cyberdelia github</a></p>
<pre><code>class CacheMixin(object):
cache_timeout = 60
def get_cache_timeout(self):
return self.cache_timeout
def dispatch(self, *args, **kwargs):
return cache_page(self.get_cache_timeout())(super(CacheMixin, self).dispatch)(*args, **kwargs)
</code></pre>
<p>用例:</p>
<pre><code>from django.views.generic.detail import DetailView
class ArticleView(CacheMixin, DetailView):
cache_timeout = 90
template_name = "article_detail.html"
queryset = Article.objects.articles()
context_object_name = "article"
</code></pre>