Sebastien
Sebastien

Reputation: 712

How can I automate my Appengine Django into memcaching my templates?

I have an App Engine application that use Django to serve a website.

There are "dynamic" and "static" pages (meaning that they don't often change). I want to speed up the loading time of my static pages by memcaching the rendered templates.

This is how it looks.

I change this code in my view:

from django.shortcuts import render_to_response

def myview(request):
    return render_to_response('page.html')

by this one:

from django.shortcuts import render_to_response
from google.appengine.api import memcache

TEMPLATE_CACHE = 3600 * 12

def myview(request):
    t = memcache.get("page.html")
    if t is None:
        t = render_to_response('page.html')
        memcache.set("page.html", t, TEMPLATE_CACHE)
    return t

But since I don't want to implement this behavior in each and every on my "static" view, i'm looking for a nice and django way to do this in my urls.py, like this:

urlpatterns = patterns('',
    (r'^index/$',  cacheView('views.index')),
    (r'^page1/$',  'views.page1'),
    (r'^page2/$',  cacheView('views.page2')),
    (r'^page3/$',  cacheView('views.page3')),
    (r'^page4/$',  'views.page4'),
)

Is it possible?

Do you have such a way to do this? What could you suggest?

Upvotes: 0

Views: 35

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

Presumably your using django-nonrel. In which case you can use all of Django's normal caching functionality, including the per-view cache decorator which does exactly what you want.

(Note, for clarity, it doesn't seem to be templates you want to cache, but the output from the view itself.)

Upvotes: 2

Related Questions