View All Posts. MiCHiLU.com powered by Django ;-)

[Django]: Generic Views で CacheMiddleware を使う

あまりにのんびりと feed をレンダリングしていると、せっかく来てくれたクローラーさんに愛想を尽かされてしまう時があります。 なので、いちいちレンダリングするのはやめて cache を使いましょう! johzan さん、通報ありがとうございます。 Ni!

Django 0.96 から URL dispatch で使用できるようになった 文字列の代わりに呼び出し可能オブジェクトを渡す を使ってみます。

myproject/urls.py

from michilu.blog.feeds import LatestEntries, LatestEntries_django, LatestComments
from django.contrib.syndication.views import feed
from django.views.decorators.cache import cache_page

full_feed = cache_page(feed,  60 * 60 * 3)

feeds = {
    'blog':     LatestEntries,
    'django':   LatestEntries_django,
    'comments': LatestComments,
}

urlpatterns += patterns('',
    (r'^feeds/(?P<url>comments)/', feed, {'feed_dict': feeds}),
    (r'^feeds/(?P<url>\w+)/short/', feed, {'feed_dict': feeds}),
    (r'^feeds/(?P<url>django)/', full_feed, {'feed_dict': feeds}),
    (r'^feeds/(?P<url>\w+)/', full_feed, {'feed_dict': feeds}),
)

というように import した Generic Views 、ここでは django.contrib.syndication.views.feed を cache_page でデコレートして full_feed を定義しています。 処理の軽いフィードはそのままの syndication_feeds で、処理の激重な!フィードは cache の効いた full_feed で配信します。

Thu, 19 Apr 2007 02:13:15 +0900 source edit
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.1 Japan License.
View All Posts. MiCHiLU.com powered by Django ;-)