Friday, March 11, 2011

caching static/domain tables

I have in my models, some tables that hold static information, like
status codes, types and descriptions, for example:

in models.py:

class CustomerStatus(models.Model):
name = models.CharField()

class CustomerType(models.Model):
name = models.CharField()

class Customer(models.Model):
name = models.CharField()
type = models.ForeignKey(CustomerType)
status = models.ForeignKey(CustomerStatus)

so, we may have statuses like 'active', 'inactive', 'blocked',
'waiting', 'whatever',
and types like 'small', 'medium', 'other'....
this statuses and types will *never* ever change.

and I use them in the templates to show select inputs, so my pre-form
view is like:

def newCustomer(request):
customer = Customer()
statuses = CustomerStatus.objects.all()
types = CustomerTypes.objects.all()
c = locals()
c.update(csrf(request))
return render_to_response('customer/customerform.html', c)

oka, okay, I could use a ModelForm on Customer, but for sake of the
example, let's consider the code above. (albeit I think the final
effect would be the same)

so, in every request, Django hits the database twice, to get the types
and statuses. but I know that they will never change. so, I'm
wondering about caching the results of those hits.

but I if query them explicitly, Django will still hit the database in
every other access to that tables.

so I thought I could use a custom manager ou something to override
the get_query_set method and check the cache before going to the
database.

this way, even if Django queries those tables by key, or by any other
way, the results would be cached on every second try.

but I don't want to install any awesome caching solution for my app,
and cache everything and have to worry about expiring caches and all
those side-effects. I just want a simple spartan solution, to cache
forever those kind of tables. I would probably mark them somehow, or
by creating a new attribute like cache=True, or by injecting a new
manager, or extending some class and adding mixings... I'm no sure.

here is the point: what is the best point to do this? use a new
manager and intercept the get_query_set method, and return a custom
cachedQuerySet? and if this is the better way, is there any snippet
explaining how to extend the QuerySet class (which methods are
mandatory to override, etc)? it seems a little complex to mess with
without proper skills...

any other suggestion?

thanks in advance,

Cesar

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate