In my django app, a logged in user can create an Entry which has the following attributes
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)
In my view a user can retrieve all Entrys for a particular date as
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')
...
Now ,I need to use cache for this ,instead of doing a database hit everytime a user want to see a listing of Entrys on a day.How should I set the key for cache? Should I use a string made out of of username+creationdate as key?
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)
....