1

我正在查询一个选举信息数据库并将它们吐出 JSONP 以用于我们正在创建的一些 ajax/jQuery 小部件。

然而,我想要做的是缓存视图,以便每次呈现小部件时都不会收到新请求。

我遇到了两个问题。

  1. 我应该使用哪种缓存方法来呈现此视图,以便数据库不会受到重创。

  2. 我需要将视图呈现为 JSONP,这意味着我需要回调变量是动态的(它当前与我的脚本一起使用,但我担心它不会与缓存视图一起使用)。

这是我的代码的样子。

from models import race
from models import candidates
from django.http import HttpResponse
from django.utils import simplejson


def data(request):

    data = []
    races = race.objects.all()

    for election in races:

        race_candidate = candidates.objects.filter(race__id=election.pk)

        candidate_info = []
        for n,candidate in enumerate(race_candidate):
            candidate_values = {
                "name":candidate.name,
                "percent":candidate.percent,
                "totalvotes":candidate.totalvotes,
                "partyname":candidate.partyname,
                "partyabbrv":candidate.partyabbrv,
                }

            candidate_info.append(candidate_values)

        race_values = {
            "title":election.title,
            "description":election.description,
            "priority":election.priority,
            "decided":election.decided,
            "candidates":candidate_info,
            }

        data.append(race_values)

    json_races = '{"races":' + simplejson.dumps(data) + '}'

    if("callback" in request.GET.keys()):
        callback = request.GET["callback"]
    else:
        callback = None

    if(callback):
        response = HttpResponse("%s(%s)" % (
                callback,
                simplejson.dumps(data)
                ), mimetype="application/json"
            )
    else:
        response = HttpResponse(json_races, mimetype="application/json")
    return response
4

1 回答 1

3

您必须将低级缓存 API与您喜欢的任何缓存后端一起使用。

from django.core.cache import cache

...

CACHE_TIMEOUT = 3600      # choose your timeout

def data(request):
    jsonstring = cache.get('elections')

    if jsonstring is None:
        data = [{
                "title": election.title,
                "description": election.description,
                "priority": election.priority,
                "decided": election.decided,
                "candidates": [
                      {
                         "name": candidate.name,
                         "percent": candidate.percent,
                         "totalvotes": candidate.totalvotes,
                         "partyname": candidate.partyname,
                         "partyabbrv": candidate.partyabbrv,
                    } for candidate in election.candidates_set.all()
                  ],
                } for election in race.objects.all()]

        jsonstring = simplejson.dumps(data)
        cache.set('elections', jsonstring, CACHE_TIMEOUT)

    callback = request.GET.get('callback')

    if callback:
        response = HttpResponse("%s(%s)" % (callback, jsonstring),
                                mimetype="application/json")
    else:
        response = HttpResponse('{"races":' + jsonstring + '}',
                                mimetype="application/json")

    return response

在你的,根据文档settings.py进行配置。内存后端是最简单的,memcached 可能是最好的。CACHE

于 2012-11-01T16:17:33.880 回答