Hey Ian, On Tue, 2006-07-11 at 11:59 +1000, Ian Holsman wrote: > this is what I was thinking of > from django.db import models > from django.db.models.query import QuerySet > > class SmallQuerySet(QuerySet): > cache={} > def get(self, *args, **kwargs): > cachekey = "%s:%s:%s" % (self.model, str(args), str(kwargs ) ) > if self.cache.has_key( cachekey ) : > return self.cache[cachekey] > x= super(SmallQuerySet, self).get(*args,**kwargs) > self.cache[ cachekey ] = x > return x > > class SmallManager(models.Manager): > def __init__(self): > super(SmallManager, self).__init__() > > def get_query_set(self): > """Returns a new QuerySet object. Subclasses can override > this method > to easily customise the behaviour of the Manager. > """ > return SmallQuerySet(self.model) > > the aim is to then use this model manager on Site and ContentType. > > this of course would mean you would need to 'bounce' your webserver > if/when you change records in models which use this class. > but that shouldn't be too often. > > is this totally out of line? > or should I just be using the 'regular' cache here.
Tricky implementation ("tricky" != "bad"), but it looks like it should work more or less. Don't you need to override every public method of QuerySet, not just get() (e.g. filter(), order_by()...)? In a long running process, memory usage is going to be an issue. You are essentially caching the results of every single query during the runtime of the process, aren't you? So if I retrieve something that temporarily sucks in a couple of thousand lines of data and I only need it once, I pay the memory penalty forever. A "sorted dict" structure that allows you to remove the oldest entries easily would work around this somewhat (managing total memory usage wouldn't be easy, but you could manage the number of cached querysets). I'm a little concerned about whether an "active" application is just going to end up thrashing the cache a lot without seeing any real benefit, but the overhead of that should not be too much. And it's testable once we have an implementation anyway. I think your idea is probably worthwhile. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers -~----------~----~----~----~------~----~------~--~---