Thursday, July 29, 2010

Re: generate cache by visiting protected links

On 28/07/2010 18:51, Jirka Vejrazka wrote:
<snip>
>
> Hi,
>
> is the statistics user-dependent?
>
> If not, just create a cron job (or similar, depending on your
> platform) that will calculate the statistics every hour and maybe even
> create the main blocks of the page to be displayed.
>
> You can either create a custom management command to do this or
> write a script that uses ORM (and templating system) and Django cache.
>
> It very much depends on you setup, but should not be too difficult.
>
> Basically, you probably don't need a "view" to populate the cache.
>
> HTH
>
> Jirka
>
> P.S. http://docs.djangoproject.com/en/dev/howto/custom-management-commands/
>

The custom commands seems indeed something very handy when one would work
via a cronjob.
However, then i would have to repeat the code i wrote for the statistics in
the code to generate it, and that's something i would like to avoid.

For now, i have found a way that seems acceptable.
When the index is hit, i start a thread where all the statistics pages are
visited. For the moment, to test, i only visit 1 statistics page and then
the thread stops. But it's easily adjusted to visit all statistics urls in
a thread that is kept alive and that visits the pages every x hours.

In my index view, i start the thread:

_thread=threading.Thread(target=visit_pages)
_thread.daemon = True
_thread.start()

then the visit_pages function:

def visit_pages():
_request = HttpRequest()
_request.method = 'GET'
_request.user = User.objects.get(pk=1)
ret_val = stats_top_callers_per_year(_request)
cache.set(reverse("stats_top_callers_per_year"), ret_val)

As you can see, it's very easy.
I do need to make my own HttpRequest it needs a user who is allowed to view the
statistics page. The user with id 1 is able to do so. However, when you would
use the "normal" request as it's passed to your views, you're going to interfere
with the user currently logged in, so it's a big no-no.
I'm not sure the method 'GET' needs to be specified. I have to test it.

Next, i call my function so i don't have to repeat the code and then i put it
on the cache.

Only downside for the moment is that i don't know what Django uses as standard key for the cache.
I thought it was the url but i don't know if it's the full url or only the part as you get
from using the reverse function (and thus as the urls are specified in urls.py)

Because of this, i needed to add this to my stats_top_callers_per_year function to make it work:

@csrf_exempt
def stats_top_callers_per_year(request):
ret = cache.get(reverse("stats_top_callers_per_year"))
if ret is not None:
print "Retrieved cached page"
return ret
...

If i find out exactly what the key needs to be, i can remove the last lines of code.

Regards,
Benedict

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment