python/django_snippets.py
changeset 521 67f37d330ad0
parent 246 5ea4585bfe9b
     1.1 --- a/python/django_snippets.py	Mon Feb 16 09:13:56 2009 -0500
     1.2 +++ b/python/django_snippets.py	Sun Dec 18 13:13:02 2011 -0500
     1.3 @@ -6,7 +6,7 @@
     1.4  def cache_key_for_function(func, args=None, kwargs=None):
     1.5      """Calculate a cache key for a function
     1.6  
     1.7 -    Prefix keys with the module and function name
     1.8 +    Prefixes keys with the module and function name.
     1.9  
    1.10      Use sha1 of repr() of args and kwargs as the final piece of the
    1.11      cache key. This limits the maximum length (so we don't go over
    1.12 @@ -45,6 +45,44 @@
    1.13      return cache_key
    1.14  
    1.15  
    1.16 +def cached_function(func):
    1.17 +    """A caching decorator for functions"""
    1.18 +    from functools import wraps
    1.19 +    from django.core.cache import cache
    1.20 +
    1.21 +    @wraps(func)
    1.22 +    def _closure(*args, **kwargs):
    1.23 +        cache_key = cache_key_for_function(
    1.24 +            func, args, kwargs)
    1.25 +
    1.26 +        val = cache.get(cache_key)
    1.27 +        if val is None:
    1.28 +            val = func(*args, **kwargs)
    1.29 +            cache.set(cache_key, val)
    1.30 +        return val
    1.31 +    return _closure
    1.32 +
    1.33 +
    1.34 +def cached_method(method):
    1.35 +    """A caching decorator for methods"""
    1.36 +    from functools import wraps
    1.37 +    from django.core.cache import cache
    1.38 +
    1.39 +    @wraps(method)
    1.40 +    def _closure(self, *args, **kwargs):
    1.41 +        cache_key = cache_key_for_method(
    1.42 +            self, method, (self,)+args, kwargs)
    1.43 +
    1.44 +        val = cache.get(cache_key)
    1.45 +        if val is None:
    1.46 +            val = method(self, *args, **kwargs)
    1.47 +            cache.set(cache_key, val)
    1.48 +        return val
    1.49 +    return _closure
    1.50 +
    1.51 +cached_property = decorators(property, cached_method)
    1.52 +
    1.53 +
    1.54  def dictproperty(method):
    1.55      """Turn dictionary or attribute access into a function call"""
    1.56  
    1.57 @@ -87,44 +125,6 @@
    1.58      return _decorator
    1.59  
    1.60  
    1.61 -def cached_function(func):
    1.62 -    """A caching decorator for functions"""
    1.63 -    from functools import wraps
    1.64 -    from django.core.cache import cache
    1.65 -
    1.66 -    @wraps(func)
    1.67 -    def _closure(*args, **kwargs):
    1.68 -        cache_key = cache_key_for_function(
    1.69 -            func, args, kwargs)
    1.70 -
    1.71 -        val = cache.get(cache_key)
    1.72 -        if val is None:
    1.73 -            val = func(*args, **kwargs)
    1.74 -            cache.set(cache_key, val)
    1.75 -        return val
    1.76 -    return _closure
    1.77 -
    1.78 -
    1.79 -def cached_method(method):
    1.80 -    """A caching decorator for methods"""
    1.81 -    from functools import wraps
    1.82 -    from django.core.cache import cache
    1.83 -
    1.84 -    @wraps(method)
    1.85 -    def _closure(self, *args, **kwargs):
    1.86 -        cache_key = cache_key_for_method(
    1.87 -            self, method, (self,)+args, kwargs)
    1.88 -
    1.89 -        val = cache.get(cache_key)
    1.90 -        if val is None:
    1.91 -            val = method(self, *args, **kwargs)
    1.92 -            cache.set(cache_key, val)
    1.93 -        return val
    1.94 -    return _closure
    1.95 -
    1.96 -cached_property = decorators(property, cached_method)
    1.97 -
    1.98 -
    1.99  def use_template(template_name, request_context=True):
   1.100      """A view decorator that wraps render_to_response and uses RequestContext by default"""
   1.101      from functools import wraps
   1.102 @@ -172,6 +172,7 @@
   1.103              return getattr(self.get_query_set(), attr, *args)
   1.104  
   1.105  
   1.106 +# http://adam.gomaa.us/blog/2009/feb/16/subclassing-django-querysets/
   1.107  class QuerySet(models.query.QuerySet):
   1.108      """Base QuerySet class for adding custom methods that are made
   1.109      available on both the manager and subsequent cloned QuerySets"""