How to use the memoization.memoization_py2._FreqNode.root function in memoization

To help you get started, we’ve selected a few memoization examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lonelyenvoy / python-memoization / memoization / memoization_py2.py View on Github external
cache[key] = old_root
                        else:
                            # add a node to the linked list
                            last = root_wrapper[0][_PREV]
                            node = [last, root_wrapper[0], key, make_cache_value(result)]  # new node
                            cache[key] = root_wrapper[0][_PREV] = last[_NEXT] = node  # save result to the cache
                            # check whether the cache is full
                            full_wrapper[0] = (cache.__len__() >= max_size)
                    return result

                wrapper._lru_root = root_wrapper[0]
                wrapper._root_name = '_lru_root'

        elif algorithm == CachingAlgorithmFlag.LFU:

            lfu_freq_list_root = _FreqNode.root()  # LFU frequency list root

            def cache_clear():
                with lock:
                    cache.clear()
                    statistics[HITS] = statistics[MISSES] = 0
                    lfu_freq_list_root.prev = lfu_freq_list_root.next = lfu_freq_list_root

            # Limited cache, LFU, with ttl
            def wrapper(*args, **kwargs):
                key = make_key(args, kwargs)
                with lock:
                    result = _access_lfu_cache(cache, key, sentinel)
                    if result is not sentinel and is_cache_value_valid(result):
                        statistics[HITS] += 1
                        return retrieve_result_from_cache_value(result)
                    statistics[MISSES] += 1