Ejemplo n.º 1
0
def _create_cached_wrapper(user_function, max_size, ttl, algorithm,
                           thread_safe):
    """
    Factory that creates an actual executed function when a function is decorated with @cached
    """
    if max_size == 0:
        return statistic_cache.get_caching_wrapper(user_function, max_size,
                                                   ttl, algorithm, thread_safe)
    elif max_size is None:
        return plain_cache.get_caching_wrapper(user_function, max_size, ttl,
                                               algorithm, thread_safe)
    else:
        cache_toolkit = get_cache_toolkit(algorithm)
        return cache_toolkit.get_caching_wrapper(user_function, max_size, ttl,
                                                 algorithm, thread_safe)
def validate():
    """
    Use this function to validate your extended caching algorithms.
    """
    global _non_internal_algorithms_found
    internal_algorithms = ['FIFO', 'LRU', 'LFU']
    has_cache_info = True

    for name, member in CachingAlgorithmFlag.__members__.items():
        if name not in internal_algorithms:

            @cached(max_size=5, ttl=0.5, algorithm=member, thread_safe=True)
            def tested_function(x):
                return x

            def undecorated_tested_function(x):
                return x

            _non_internal_algorithms_found = True
            print('Found extended algorithm <{}>'.format(name))
            try:
                cache_toolkit = get_cache_toolkit(member)
            except KeyError:
                _error(
                    'Cannot find mapping configuration for algorithm <{}>\n'.
                    format(name))
                return
            if not hasattr(cache_toolkit, 'get_caching_wrapper'):
                _error(
                    'Cannot find get_caching_wrapper function in module <{}>\n'
                    .format(cache_toolkit.__name__))
                return
            if not callable(cache_toolkit.get_caching_wrapper):
                _error(
                    'Expected {}.get_caching_wrapper to be callable\n'.format(
                        cache_toolkit.__name__))
                return
            wrapper = cache_toolkit.get_caching_wrapper(
                user_function=undecorated_tested_function,
                max_size=5,
                ttl=0.5,
                algorithm=member,
                thread_safe=True)
            if not hasattr(wrapper, 'cache_info'):
                has_cache_info = False
                _error(
                    'Cannot find cache_info function in the cache wrapper of <{}>\n'
                    .format(cache_toolkit.__name__))
            if not callable(wrapper.cache_info):
                has_cache_info = False
                _error(
                    'Expected cache_info of wrapper of <{}> to be callable\n'.
                    format(cache_toolkit.__name__))
            if not hasattr(wrapper, 'cache_clear'):
                _error(
                    'Cannot find cache_clear function in the cache wrapper of <{}>\n'
                    .format(cache_toolkit.__name__))
            if not callable(wrapper.cache_clear):
                _error(
                    'Expected cache_clear of wrapper of <{}> to be callable\n'.
                    format(cache_toolkit.__name__))

            for x in range(0, 5):
                tested_function(x)

            if has_cache_info:
                info = tested_function.cache_info()
                if not isinstance(info, CacheInfo):
                    _error(
                        'The return value of cache_info is not an instance of CacheInfo'
                    )
                else:
                    if not isinstance(info.hits, int):
                        _error('Expected cache_info().hits to be an integer')
                    if not isinstance(info.misses, int):
                        _error('Expected cache_info().misses to be an integer')
                    if not isinstance(info.current_size, int):
                        _error(
                            'Expected cache_info().current_size to be an integer'
                        )
                    if info.max_size is not None and not isinstance(
                            info.max_size, int):
                        _error(
                            'Expected cache_info().max_size to be an integer')
                    if info.algorithm != member:
                        _error(
                            'Expected cache_info().algorithm = <{}> to be <{}>'
                            .format(info.algorithm, member))
                    if info.ttl is not None and not isinstance(
                            info.ttl, int) and not isinstance(info.ttl, float):
                        _error(
                            'Expected cache_info().ttl to be an integer or a float'
                        )
                    if not isinstance(info.thread_safe, bool):
                        _error(
                            'Expected cache_info().thread_safe to be a bool')