Skip to content

Commit

Permalink
fix some refactoring regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
aehlke committed Jan 15, 2013
1 parent 7eb21ac commit 033e321
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
29 changes: 3 additions & 26 deletions cachecow/cache.py
Expand Up @@ -32,7 +32,7 @@
_ALL_CHARS = string.maketrans('', '')


def _key_arg_iterator(key_args, max_depth=1):
def key_arg_iterator(key_args, max_depth=1):
'''
Yields items from key arguments as we allow them in `cached_function` et al.
Expand All @@ -45,7 +45,7 @@ def _key_arg_iterator(key_args, max_depth=1):
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):
for y in key_arg_iterator(x, max_depth=max_depth - 1):
yield y
except TypeError: # It's not an iterable, so forget recursing.
yield key_args
Expand Down Expand Up @@ -106,7 +106,7 @@ def make_key(obj):
[2] http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
'''
key = '.'.join(imap(_format_key_arg, _key_arg_iterator(obj)))
key = '.'.join(imap(_format_key_arg, key_arg_iterator(obj)))

# If the resulting key is too long, hash the part after the prefix, and
# truncate as needed.
Expand All @@ -124,7 +124,6 @@ def make_key(obj):
return key



def _make_namespace_prefix():
'''
Returns a likely-to-be-unique value that can be incremented with
Expand Down Expand Up @@ -175,28 +174,6 @@ def invalidate_namespace(namespace):
pass


def _make_key(key_args, namespace, func_args, func_kwargs):
'''
Returns the cache key to use for the decorated function. Calls and replaces
any callable items in `key_args` with their return values before sending
`key_args` over to `make_key`. Does the same for a callable `namespace`.
'''
def call_if_callable(obj):
if callable(obj):
return obj(*func_args, **func_kwargs)
return obj

key_args = map(call_if_callable, _key_arg_iterator(key_args))

namespace = call_if_callable(namespace)
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):
'''
Returns an int.
Expand Down
28 changes: 25 additions & 3 deletions cachecow/decorators.py
Expand Up @@ -12,6 +12,28 @@
from cachecow.intpacker import pack_int


def _make_key_for_func(key_args, namespace, func_args, func_kwargs):
'''
Returns the cache key to use for the decorated function. Calls and replaces
any callable items in `key_args` with their return values before sending
`key_args` over to `make_key`. Does the same for a callable `namespace`.
'''
def call_if_callable(obj):
if callable(obj):
return obj(*func_args, **func_kwargs)
return obj

key_args = map(call_if_callable, key_arg_iterator(key_args))

namespace = call_if_callable(namespace)
if namespace:
key_args.append(_get_namespace_prefix(make_key(namespace)))

logger.debug(u'_make_key_for_func passed namespace: {0}'.format(namespace))
logger.debug(u'_make_key_for_func returning: {0}'.format(make_key(key_args)))
return make_key(key_args)


def _add_delete_cache_member(func, key=None, namespace=None):
'''
Adds a `delete_cache` member function to `func`. Pass it the same args
Expand All @@ -22,7 +44,7 @@ def _add_delete_cache_member(func, key=None, namespace=None):
'''
def delete_cache(*args, **kwargs):
key_args = key or _make_key_args_from_function(func, *args, **kwargs)
_key = _make_key(key_args, namespace, args, kwargs)
_key = _make_key_for_func(key_args, namespace, args, kwargs)
cache.delete(_key)
func.delete_cache = delete_cache

Expand Down Expand Up @@ -122,7 +144,7 @@ def decorator(func):
def wrapped(*args, **kwargs):
key_args = (key
or _make_key_args_from_function(func, *args, **kwargs))
_key = _make_key(key_args, namespace, args, kwargs)
_key = _make_key_for_func(key_args, namespace, args, kwargs)

val = cache.get(_key)
if val is None:
Expand Down Expand Up @@ -198,7 +220,7 @@ def wrapped(request, *args, **kwargs):

# Serialize the key.
# Add `request` to `args` since _make_key wants all func args in it.
_key = _make_key(key_args, namespace, (request,) + args, kwargs)
_key = _make_key_for_func(key_args, namespace, (request,) + args, kwargs)

val = cache.get(_key)
if val is None:
Expand Down
16 changes: 9 additions & 7 deletions cachecow/tests/tests.py
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-

from django.conf import settings
from django.test import TestCase
from datetime import timedelta
from itertools import chain
from cachecow.cache import (make_key, cached_function, _format_key_arg,
_make_key_args_from_function, _timedelta_to_seconds,
invalidate_namespace, _key_arg_iterator)

from django.conf import settings
from django.test import TestCase

from cachecow.cache import (make_key, _format_key_arg, timedelta_to_seconds,
invalidate_namespace, key_arg_iterator)
from cachecow.decorators import cached_function
from cachecow.intpacker import pack_int, unpack_int


Expand Down Expand Up @@ -102,7 +104,7 @@ def my_func(self):

def test_timedelta_to_s(self):
t = timedelta(days=2)
s = _timedelta_to_seconds(t)
s = timedelta_to_seconds(t)
self.assertEqual(s, 3600*48)

def test_simple_namespaced_key(self):
Expand Down Expand Up @@ -204,7 +206,7 @@ def get_age(p):
def test_key_arg_iterator(self):
args = ['a', 'b', 1, 2, [3, 4], [5, [6, 7]]]
flat_args = ['a', 'b', 1, 2, 3, 4, 5, [6, 7]] # Only flattened one level
iterated_args = list(_key_arg_iterator(args, max_depth=1))
iterated_args = list(key_arg_iterator(args, max_depth=1))
self.assertEqual(iterated_args, flat_args)


Expand Down

0 comments on commit 033e321

Please sign in to comment.