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 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
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
"""
def get_caching_wrapper(user_function, max_size, ttl, algorithm, thread_safe, order_independent, custom_key_maker):
"""
Get a caching wrapper for statistics only, without any actual caching
"""
misses = 0 # number of misses of the cache
lock = RLock() if thread_safe else DummyWithable() # ensure thread-safe
def wrapper(*args, **kwargs):
"""
The actual wrapper
"""
nonlocal misses
with lock:
misses += 1
return user_function(*args, **kwargs)
def cache_clear():
"""
Clear the cache and statistics information
"""
nonlocal misses
with lock:
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
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