Ticket #7338: method_cache_r9787.diff

File method_cache_r9787.diff, 3.2 KB (added by Manuel Saelices, 16 years ago)

Patch that work with django r9787 revision

  • django/db/models/sql/query.py

     
    2222from django.core.exceptions import FieldError
    2323from datastructures import EmptyResultSet, Empty, MultiJoin
    2424from constants import *
     25from django.core.cache import cache
    2526
    2627try:
    2728    set
     
    9091        self.extra_params = ()
    9192        self.extra_order_by = ()
    9293
     94        # Cached queryset atribute. None means no cached
     95        self.cache_timeout = None
     96
    9397    def __str__(self):
    9498        """
    9599        Returns the query as a string of SQL with the parameter values
     
    190194        obj.extra_where = self.extra_where
    191195        obj.extra_params = self.extra_params
    192196        obj.extra_order_by = self.extra_order_by
     197        obj.cache_timeout = self.cache_timeout
    193198        if self.filter_is_sticky and self.used_aliases:
    194199            obj.used_aliases = self.used_aliases.copy()
    195200        else:
     
    237242        """
    238243        resolve_columns = hasattr(self, 'resolve_columns')
    239244        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:
    241265            for row in rows:
    242266                if resolve_columns:
    243267                    if fields is None:
  • django/db/models/manager.py

     
    140140    def reverse(self, *args, **kwargs):
    141141        return self.get_query_set().reverse(*args, **kwargs)
    142142
     143    def cache(self, timeout=20):
     144        return self.get_query_set().cache(timeout=timeout)
     145       
    143146    def _insert(self, values, **kwargs):
    144147        return insert_query(self.model, values, **kwargs)
    145148
  • django/db/models/query.py

     
    640640        clone.query.standard_ordering = not clone.query.standard_ordering
    641641        return clone
    642642
     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
    643653    ###################
    644654    # PRIVATE METHODS #
    645655    ###################
Back to Top