Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- def cache(n):
- lru_cache = dict()
- order = deque()
- def decorator(function):
- def wrapped(*args, **kwargs):
- nonlocal lru_cache, order
- query = str(args) + str(kwargs)
- if lru_cache.get(query, None) is None:
- order.append(query)
- value = function(*args, **kwargs)
- lru_cache[query] = value
- if len(order) > n:
- del lru_cache[order[0]]
- order.popleft()
- else:
- return lru_cache[query]
- return wrapped
- return decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement