私のdjangoアプリでは、ログインしたユーザーがEntry
次の属性を持つを作成できます
from django.db import models
from datetime import date
from django.contrib.auth.models import User
class Entry(models.Model):
creationdate=models.DateField(default=date.today)
description=models.TextField()
author=models.ForeignKey(User,null=True)
私の見解では、ユーザーはEntry
特定の日付のすべてを次のように取得できます。
def entries_on_ a_day(request,year,month,day):
#month as 'jan','feb' etc
...
entries_for_date = Entry.objects.filter(creationdate__year=year,creationdate__month=get_month_as_number(month),creationdate__day=day,author=request.user).order_by('-creationdate')
...
cache
これで、ユーザーが1日にsのリストを表示するたびにデータベースヒットを実行する代わりに、これを使用する必要がありEntry
ます。キャッシュのキーをどのように設定すればよいですか?username+creationdate
キーとして作成された文字列を使用する必要がありますか?
from django.core.cache import cache
def entries_on_ a_day(request,year,month,day):
creationdate=new date(year,get_month_as_number(month),day)
key = request.user.username+ str(creationdate)
if key not in cache:
entries_for_date = Entry.objects.filter(creationdate__year=year,creationdate__month=get_month_as_number(month),creationdate__day=day,author=request.user).order_by('-creationdate')
cache.set(key,entries_for_date)
entries = cache.get(key)
....