Friday, July 30, 2010

Re: generate cache by visiting protected links

> I still have 2 approaches i can take.

I still believe that there is a 3rd option there - to bypass the
view / auth / login machinery completely :)


If your primary goal is to cache the statistics and the statistics
does not depend on the user that is logged in (you did pass a user
with pk=1 in one of your previous emails), then you don't really need
to call any view to cache the data. You can call the function that
calculates the statistics (that would cache the data internally within
the function), then call this function from anywhere upon start of
your webserver (or cron job, as mentioned before).

As long as you have a global cache (i.e. memcached), this would work
very nicely and you would not have to do any urllib2 login magic.

Let me give you a short example:

def stats_top_callers_per_year(some_parameters_not_tied_to_a_specific_user):
cache_key = 'stats_top_callers_per_year'
data = cache.get(cache_key)
if data:
return data
# calculate the data somehow
cache.set(cache_key, data) # <- set appropriate timeout there


Then have small script called before (or shortly after) your web
server start, that would import this function from your code, hence
calculating the statistics. Then, as long as you use memcached, your
web would use the cached statistics every time the
stats_top_callers_per_year() would be called frmo your views.

Jirka

--
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