Permalink
Browse files

fix bug: unicode and str are subclasses of basestring

add some debug-level logging
  • Loading branch information...
1 parent 3dd76b5 commit 07aee70fd1aa165762070a0e824964d622f6d8e5 @aehlke committed May 25, 2011
Showing with 19 additions and 1 deletion.
  1. +13 −1 cachecow/cache.py
  2. +6 −0 cachecow/tests/tests.py
View
@@ -12,6 +12,10 @@
import re
import string
import time
+import logging
+
+# Get an instance of a logger
+logger = logging.getLogger(__name__)
# A memcached limit.
@@ -43,7 +47,7 @@ def _key_arg_iterator(key_args, max_depth=1):
deep, if they contain nested iterables.
'''
# Try traversing deeper into the input, unless it's a string.
- if max_depth >= 0 and not isinstance(key_args, str):
+ if max_depth >= 0 and not isinstance(key_args, basestring):
try:
for x in key_args:
for y in _key_arg_iterator(x, max_depth=max_depth - 1):
@@ -203,6 +207,10 @@ def invalidate_namespace(namespace):
It is an O(1) operation, independent of the number of keys in a namespace.
'''
namespace = make_key(namespace)
+
+ logger.debug('invalidating namespace: {0}'.format(namespace))
+ logger.debug('namespace value was: {0}'.format(cache.get(namespace)))
+
try:
cache.incr(namespace)
except ValueError:
@@ -226,6 +234,8 @@ def call_if_callable(obj):
if namespace:
key_args.append(_get_namespace_prefix(make_key(namespace)))
+ logger.debug(u'_make_key passed namespace: {0}'.format(namespace))
+ logger.debug(u'_make_key returning: {0}'.format(make_key(key_args)))
return make_key(key_args)
def _timedelta_to_seconds(t):
@@ -247,6 +257,8 @@ def _set_cache(key, val, timeout):
timeout = _timedelta_to_seconds(timeout)
if timeout and timeout < 0:
raise Exception('Cache timeout value must not be negative.')
+
+ logger.debug(u'setting cache: {0} = {1}'.format(key,val))
cache.set(key, val, timeout=timeout)
def _add_delete_cache_member(func, key=None, namespace=None):
View
@@ -17,6 +17,12 @@ def test_short_key(self):
for arg in args:
self.assertTrue(arg.replace(' ', '') in key)
+ def test_unicode_obj_key(self):
+ args = [u'foo', 'bar', u'baz']
+ key = make_key(args)
+ for arg in args:
+ self.assertTrue(arg in key)
+
def test_no_brackets_in_list_key(self):
key = make_key([1,2,3,4])
self.assertTrue('[' not in key and ']' not in key)

0 comments on commit 07aee70

Please sign in to comment.