Beispiel #1
0
    def _get_from_cache(self, target_self, *args, **kwargs):
        target_self_cls_name = reflection.get_class_name(target_self,
                                                         fully_qualified=False)
        func_name = "%(module)s.%(class)s.%(func_name)s" % {
            'module': target_self.__module__,
            'class': target_self_cls_name,
            'func_name': self.func.__name__,
        }
        key = (func_name, ) + args
        if kwargs:
            key += helpers.dict2tuple(kwargs)
        # oslo.cache expects a string or a buffer
        key = str(key)
        try:
            item = target_self._cache.get(key)
        except TypeError:
            LOG.debug(
                "Method %(func_name)s cannot be cached due to "
                "unhashable parameters: args: %(args)s, kwargs: "
                "%(kwargs)s", {
                    'func_name': func_name,
                    'args': args,
                    'kwargs': kwargs
                })
            return self.func(target_self, *args, **kwargs)

        if item is self._not_cached:
            item = self.func(target_self, *args, **kwargs)
            target_self._cache.set(key, item)

        return item
Beispiel #2
0
    def _get_from_cache(self, target_self, *args, **kwargs):
        target_self_cls_name = reflection.get_class_name(target_self,
                                                         fully_qualified=False)
        func_name = "%(module)s.%(class)s.%(func_name)s" % {
            'module': target_self.__module__,
            'class': target_self_cls_name,
            'func_name': self.func.__name__,
        }
        key = (func_name,) + args
        if kwargs:
            key += helpers.dict2tuple(kwargs)
        # oslo.cache expects a string or a buffer
        key = str(key)
        try:
            item = target_self._cache.get(key)
        except TypeError:
            LOG.debug("Method %(func_name)s cannot be cached due to "
                      "unhashable parameters: args: %(args)s, kwargs: "
                      "%(kwargs)s",
                      {'func_name': func_name,
                       'args': args,
                       'kwargs': kwargs})
            return self.func(target_self, *args, **kwargs)

        if item is self._not_cached:
            item = self.func(target_self, *args, **kwargs)
            target_self._cache.set(key, item)

        return item
Beispiel #3
0
def dict2tuple(d):
    return helpers.dict2tuple(d)
Beispiel #4
0
 def test_dict(self):
     input_dict = {'foo': 'bar', '42': 'baz', 'aaa': 'zzz'}
     expected = (('42', 'baz'), ('aaa', 'zzz'), ('foo', 'bar'))
     output_tuple = helpers.dict2tuple(input_dict)
     self.assertEqual(expected, output_tuple)
Beispiel #5
0
def dict2tuple(d):
    return helpers.dict2tuple(d)
Beispiel #6
0
 def test_dict(self):
     input_dict = {'foo': 'bar', '42': 'baz', 'aaa': 'zzz'}
     expected = (('42', 'baz'), ('aaa', 'zzz'), ('foo', 'bar'))
     output_tuple = helpers.dict2tuple(input_dict)
     self.assertEqual(expected, output_tuple)