Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_caching_wrapper(user_function, max_size, ttl, algorithm, thread_safe, order_independent, custom_key_maker):
"""
Get a caching wrapper for FIFO cache
"""
cache = {} # the cache to store function results
sentinel = object() # sentinel object for the default value of map.get
hits = misses = 0 # hits and misses of the cache
lock = RLock() if thread_safe else DummyWithable() # ensure thread-safe
if ttl is not None: # set up values toolkit according to ttl
values_toolkit = values_toolkit_with_ttl
else:
values_toolkit = values_toolkit_without_ttl
if custom_key_maker is not None: # use custom make_key function
make_key = custom_key_maker
else:
if order_independent: # set up keys toolkit according to order_independent
make_key = keys_toolkit_order_independent.make_key
else:
make_key = keys_toolkit_order_dependent.make_key
# for FIFO list
full = False # whether the cache is full or not
root = [] # linked list
root[:] = [root, root, None, None] # initialize by pointing to self
_PREV = 0 # index for the previous node
_NEXT = 1 # index for the next node
_KEY = 2 # index for the key
_VALUE = 3 # index for the value
def get_caching_wrapper(user_function, max_size, ttl, algorithm, thread_safe, order_independent, custom_key_maker):
"""
Get a caching wrapper for LFU cache
"""
cache = {} # the cache to store function results
sentinel = object() # sentinel object for the default value of map.get
hits = misses = 0 # hits and misses of the cache
lock = RLock() if thread_safe else DummyWithable() # ensure thread-safe
if ttl is not None: # set up values toolkit according to ttl
values_toolkit = values_toolkit_with_ttl
else:
values_toolkit = values_toolkit_without_ttl
if custom_key_maker is not None: # use custom make_key function
make_key = custom_key_maker
else:
if order_independent: # set up keys toolkit according to order_independent
make_key = keys_toolkit_order_independent.make_key
else:
make_key = keys_toolkit_order_dependent.make_key
lfu_freq_list_root = _FreqNode.root() # LFU frequency list root
def wrapper(*args, **kwargs):
"""
The actual wrapper
"""
nonlocal hits, misses
key = make_key(args, kwargs)
cache_expired = False
def get_caching_wrapper(user_function, max_size, ttl, algorithm, thread_safe, order_independent, custom_key_maker):
"""
Get a caching wrapper for LRU cache
"""
cache = {} # the cache to store function results
sentinel = object() # sentinel object for the default value of map.get
hits = misses = 0 # hits and misses of the cache
lock = RLock() if thread_safe else DummyWithable() # ensure thread-safe
if ttl is not None: # set up values toolkit according to ttl
values_toolkit = values_toolkit_with_ttl
else:
values_toolkit = values_toolkit_without_ttl
if custom_key_maker is not None: # use custom make_key function
make_key = custom_key_maker
else:
if order_independent: # set up keys toolkit according to order_independent
make_key = keys_toolkit_order_independent.make_key
else:
make_key = keys_toolkit_order_dependent.make_key
# for LRU list
full = False # whether the cache is full or not
root = [] # linked list
root[:] = [root, root, None, None] # initialize by pointing to self
_PREV = 0 # index for the previous node
_NEXT = 1 # index for the next node
_KEY = 2 # index for the key
_VALUE = 3 # index for the value
def get_caching_wrapper(user_function, max_size, ttl, algorithm, thread_safe, order_independent, custom_key_maker):
"""
Get a caching wrapper for space-unlimited cache
"""
cache = {} # the cache to store function results
sentinel = object() # sentinel object for the default value of map.get
hits = misses = 0 # hits and misses of the cache
lock = RLock() if thread_safe else DummyWithable() # ensure thread-safe
if ttl is not None: # set up values toolkit according to ttl
values_toolkit = values_toolkit_with_ttl
else:
values_toolkit = values_toolkit_without_ttl
if custom_key_maker is not None: # use custom make_key function
make_key = custom_key_maker
else:
if order_independent: # set up keys toolkit according to order_independent
make_key = keys_toolkit_order_independent.make_key
else:
make_key = keys_toolkit_order_dependent.make_key
def wrapper(*args, **kwargs):
"""
The actual wrapper
"""
nonlocal hits, misses
key = make_key(args, kwargs)
value = cache.get(key, sentinel)
if value is not sentinel and values_toolkit.is_cache_value_valid(value):