#2341: [patch] CacheDebugWrapper
-----------------------------+----------------------------------------------
 Reporter:  [EMAIL PROTECTED]  |        Owner:  adrian 
     Type:  enhancement      |       Status:  closed 
 Priority:  normal           |    Milestone:         
Component:  Core framework   |      Version:         
 Severity:  normal           |   Resolution:  wontfix
 Keywords:                   |  
-----------------------------+----------------------------------------------
Changes (by adrian):

  * resolution:  => wontfix
  * status:  new => closed

Old description:

> add some instrumentation to the cache object so we can see how effective
> it is.
> personally I think the counters could go on all the cache objects.
> {{{
> Index: __init__.py
> ===================================================================
> --- __init__.py (revision 3337)
> +++ __init__.py (working copy)
> @@ -18,6 +18,8 @@
>  from cgi import parse_qsl
>  from django.conf import settings
>  from django.core.cache.backends.base import InvalidCacheBackendError
> +from django.core import signals
> +from django.dispatch import dispatcher
> 
>  BACKENDS = {
>      # name for use in settings file --> name of module in "backends"
> directory
> @@ -48,7 +50,55 @@
>      if host.endswith('/'):
>          host = host[:-1]
> 
> -    cache_class = getattr(__import__('django.core.cache.backends.%s' %
> BACKENDS[scheme], '', '', ['']), 'CacheClass')
> -    return cache_class(host, params)
> +        cache_class = getattr(__import__('django.core.cache.backends.%s'
> % BACKENDS[scheme], '', '', ['']), 'CacheClass')
> +    if settings.DEBUG:
> +        return CacheDebugWrapper( cache_class(host,params ))
> +    else:
> +        return cache_class(host, params)
> 
> +class CacheDebugWrapper(object):
> +    def __init__(self, real_cache ):
> +        self._cache = real_cache
> +        self.gets = 0
> +        self.get_hits = 0
> +        self.sets = 0
> +        self.deletes= 0
> +        self.get_manys= 0
> +        self.queries = []
> +
> +    def get(self,key,default=None):
> +        self.gets += 1
> +        val = self._cache.get( key )
> +        self.queries.append( key )
> +        if val is None:
> +            return default
> +        else:
> +            self.get_hits += 1
> +            return val
> +
> +    def set(self, key, value, timeout=0):
> +        self.sets += 1
> +        self._cache.set(key, value, timeout)
> +
> +    def delete(self, key):
> +        self.deletes += 1
> +        self._cache.delete(key)
> +
> +    def get_many(self, keys):
> +        self.get_manys += 1
> +        return self._cache.get_multi(keys)
> +
> +def reset_queries():
> +    if settings.DEBUG:
> +        cache.queries = []
> +        cache.gets = 0
> +        cache.get_hits = 0
> +        cache.sets = 0
> +        cache.deletes= 0
> +        cache.get_manys= 0
> +        cache.queries = []
> +
> +
> +dispatcher.connect(reset_queries, signal=signals.request_started)
> +
>  cache = get_cache(settings.CACHE_BACKEND)
> }}}

New description:

 add some instrumentation to the cache object so we can see how effective
 it is.
 personally I think the counters could go on all the cache objects.
 {{{
 Index: __init__.py
 ===================================================================
 --- __init__.py (revision 3337)
 +++ __init__.py (working copy)
 @@ -18,6 +18,8 @@
  from cgi import parse_qsl
  from django.conf import settings
  from django.core.cache.backends.base import InvalidCacheBackendError
 +from django.core import signals
 +from django.dispatch import dispatcher
 
  BACKENDS = {
      # name for use in settings file --> name of module in "backends"
 directory
 @@ -48,7 +50,55 @@
      if host.endswith('/'):
          host = host[:-1]
 
 -    cache_class = getattr(__import__('django.core.cache.backends.%s' %
 BACKENDS[scheme], '', '', ['']), 'CacheClass')
 -    return cache_class(host, params)
 +        cache_class = getattr(__import__('django.core.cache.backends.%s'
 % BACKENDS[scheme], '', '', ['']), 'CacheClass')
 +    if settings.DEBUG:
 +        return CacheDebugWrapper( cache_class(host,params ))
 +    else:
 +        return cache_class(host, params)
 
 +class CacheDebugWrapper(object):
 +    def __init__(self, real_cache ):
 +        self._cache = real_cache
 +        self.gets = 0
 +        self.get_hits = 0
 +        self.sets = 0
 +        self.deletes= 0
 +        self.get_manys= 0
 +        self.queries = []
 +
 +    def get(self,key,default=None):
 +        self.gets += 1
 +        val = self._cache.get( key )
 +        self.queries.append( key )
 +        if val is None:
 +            return default
 +        else:
 +            self.get_hits += 1
 +            return val
 +
 +    def set(self, key, value, timeout=0):
 +        self.sets += 1
 +        self._cache.set(key, value, timeout)
 +
 +    def delete(self, key):
 +        self.deletes += 1
 +        self._cache.delete(key)
 +
 +    def get_many(self, keys):
 +        self.get_manys += 1
 +        return self._cache.get_multi(keys)
 +
 +def reset_queries():
 +    if settings.DEBUG:
 +        cache.queries = []
 +        cache.gets = 0
 +        cache.get_hits = 0
 +        cache.sets = 0
 +        cache.deletes= 0
 +        cache.get_manys= 0
 +        cache.queries = []
 +
 +
 +dispatcher.connect(reset_queries, signal=signals.request_started)
 +
  cache = get_cache(settings.CACHE_BACKEND)
 }}}

Comment:

 Eh, I'm not sure this has general interest, so I'm marking it as a
 wontfix. Memcached already has its own framework for viewing this sort of
 data, BTW, and although the other backends don't, I don't see this as a
 big deal.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/2341>
Django <http://code.djangoproject.org/>
The web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---

Reply via email to