Opened 18 years ago

Closed 18 years ago

#2341 closed enhancement (wontfix)

[patch] CacheDebugWrapper — at Version 1

Reported by: ian@… Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adrian Holovaty)

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)

Change History (1)

comment:1 by Adrian Holovaty, 18 years ago

Description: modified (diff)
Resolution: wontfix
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top