Ticket #7338: method_cache_r9787.diff
File method_cache_r9787.diff, 3.2 KB (added by , 16 years ago) |
---|
-
django/db/models/sql/query.py
22 22 from django.core.exceptions import FieldError 23 23 from datastructures import EmptyResultSet, Empty, MultiJoin 24 24 from constants import * 25 from django.core.cache import cache 25 26 26 27 try: 27 28 set … … 90 91 self.extra_params = () 91 92 self.extra_order_by = () 92 93 94 # Cached queryset atribute. None means no cached 95 self.cache_timeout = None 96 93 97 def __str__(self): 94 98 """ 95 99 Returns the query as a string of SQL with the parameter values … … 190 194 obj.extra_where = self.extra_where 191 195 obj.extra_params = self.extra_params 192 196 obj.extra_order_by = self.extra_order_by 197 obj.cache_timeout = self.cache_timeout 193 198 if self.filter_is_sticky and self.used_aliases: 194 199 obj.used_aliases = self.used_aliases.copy() 195 200 else: … … 237 242 """ 238 243 resolve_columns = hasattr(self, 'resolve_columns') 239 244 fields = None 240 for rows in self.execute_sql(MULTI): 245 if self.cache_timeout is not None: 246 # Check cache for stored objects from an exactly equal query 247 k = str(self) 248 try: 249 import hashlib 250 except ImportError: 251 import sha 252 k = sha.new(k).hexdigest() 253 else: 254 k = hashlib.sha1(k).hexdigest() 255 256 if cache.has_key(k) and cache.get(k): 257 sql_result = cache.get(k, []) 258 else: 259 cache.set(k, [i for i in self.execute_sql(MULTI)], self.cache_timeout) 260 sql_result = cache.get(k, []) 261 else: 262 sql_result = self.execute_sql(MULTI) 263 264 for rows in sql_result: 241 265 for row in rows: 242 266 if resolve_columns: 243 267 if fields is None: -
django/db/models/manager.py
140 140 def reverse(self, *args, **kwargs): 141 141 return self.get_query_set().reverse(*args, **kwargs) 142 142 143 def cache(self, timeout=20): 144 return self.get_query_set().cache(timeout=timeout) 145 143 146 def _insert(self, values, **kwargs): 144 147 return insert_query(self.model, values, **kwargs) 145 148 -
django/db/models/query.py
640 640 clone.query.standard_ordering = not clone.query.standard_ordering 641 641 return clone 642 642 643 def cache(self, timeout=20): 644 """ 645 Forces the current queryset to check if a exact equal query has been 646 stored in the cache. The expiration time is the seconds in 'timeout' 647 variable. 648 """ 649 clone = self._clone() 650 clone.query.cache_timeout = timeout 651 return clone 652 643 653 ################### 644 654 # PRIVATE METHODS # 645 655 ###################