magic-removal: Removed caching hack for managers, disabling caching for everything except related objects

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2154 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2006-01-29 01:09:10 +00:00
parent a33aa41fbf
commit d580b42d45
2 changed files with 14 additions and 17 deletions

View File

@ -369,11 +369,5 @@ class ManagerDescriptor(object):
def __get__(self, instance, type=None): def __get__(self, instance, type=None):
if instance != None: if instance != None:
raise AttributeError, "Manager isn't accessible via %s instances" % type.__name__ raise AttributeError, "Manager isn't accessible via %s instances" % type.__name__
# HACK return self.manager
# We need a new instance every time. Otherwise, the cache that
# the manager keeps never gets dropped, which is pain for memory usage,
# and concurrency and means that queries don't get updated when you do
# a model_obj.save(). (This hack helps some tests to pass, but isn't a real fix)
#return self.manager.__class__()
return copy.deepcopy(self.manager)

View File

@ -67,6 +67,7 @@ class QuerySet(object):
self._tables = None self._tables = None
self._offset = None self._offset = None
self._limit = None self._limit = None
self._use_cache = False
def filter(self, **kwargs): def filter(self, **kwargs):
"""Returns a new query instance with the query arguments """Returns a new query instance with the query arguments
@ -240,21 +241,24 @@ class QuerySet(object):
return select, " ".join(sql), params return select, " ".join(sql), params
def _fetch_data(self): def _fetch_data(self):
if self._result_cache is None: if self._use_cache:
self._result_cache = list(self.get_iterator()) if self._result_cache is None:
self._result_cache = list(self.get_iterator())
return self._result_cache
else:
return list(self.get_iterator())
def __iter__(self): def __iter__(self):
"""Gets an iterator for the data""" """Gets an iterator for the data"""
# Fetch the data or use get_iterator? If not, we can't # Fetch the data or use get_iterator? If not, we can't
# do sequence operations - or doing so will require re-fetching # do sequence operations - or doing so will require re-fetching
# Also, lots of things in current template system break if # Also, lots of things in current template system break if we
# don't get it all. # don't get it all.
self._fetch_data() return iter(self._fetch_data())
return iter(self._result_cache)
def __len__(self): def __len__(self):
self._fetch_data() return len(self._fetch_data())
return len(self._result_cache)
def __getitem__(self, k): def __getitem__(self, k):
"""Retrieve an item or slice from the set of results""" """Retrieve an item or slice from the set of results"""
@ -278,8 +282,7 @@ class QuerySet(object):
else: else:
# TODO: possibly use a new query which just gets one item # TODO: possibly use a new query which just gets one item
# if we haven't already got them all? # if we haven't already got them all?
self._fetch_data() return self._fetch_data()[k]
return self._result_cache[k]
def get_iterator(self): def get_iterator(self):
# self._select is a dictionary, and dictionaries' key order is # self._select is a dictionary, and dictionaries' key order is