def _make_key_for_func(key_args, func_args, func_kwargs, namespace=None, skip_prefix=False): ''' 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 = call_if_callable(key_args) key_args = map(call_if_callable, key_arg_iterator(key_args)) return make_key(key_args, namespace=namespace, skip_prefix=skip_prefix)
def invalidate_user_comments(comment): #TODO make django-cachecow namespaces dynamic, and then we can key it off the user ID and wipe it all in # one go instead of deleting individual keys. author = comment.author pages = int(math.ceil(QuestComment.all_objects.filter(author=author).count() / float(knobs.COMMENTS_PER_PAGE))) for page in range(1, pages + 1) + ['top', None]: for viewer_is_author in [True, False]: for endpoint_key in ['user_comments']: key_args = [ endpoint_key, _USER_COMMENTS_CACHE_VERSION, author.username, viewer_is_author, ] if page is not None: key_args.append(page) key = make_key(key_args) cache.delete(key)
def test_long_key(self): args = range(2000) key = make_key(args) self.assertTrue(len(key) <= 250) self.assertTrue('1.2.3.4.5.6.7.8.9.10' not in key, 'key is not hashed')
def test_no_brackets_in_list_key(self): key = make_key([1, 2, 3, 4]) self.assertTrue('[' not in key and ']' not 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_short_key(self): args = ['foo', 'bar', 'b1z', 'qu ux'] key = make_key(args) for arg in args: self.assertTrue(arg.replace(' ', '') 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)