python function cache

Solutions on MaxInterview for python function cache by the best coders in the world

showing results for - "python function cache"
Calista
19 Jul 2020
1# Example use - Decorator code below -----------------------------
2import time
3
4@cache(ttl=timedelta(minutes=3), max_entries=300)
5def add(a, b):
6    time.sleep(2)
7    return a + b
8
9@cache()
10def substract(a, b):
11    time.sleep(2)
12    return a - b
13  
14  
15# Decorator code -----------------------------
16from datetime import datetime, timedelta
17
18def cache(**kwargs):
19  def decorator(function):
20    # static function variable for cache, lazy initialization
21    try: function.cache
22    except: function.cache = {}
23    def wrapper(*args):
24        # if nothing valid in cache, insert something
25        if not args in function.cache or datetime.now() > function.cache[args]['expiry']:
26            if 'max_entries' in kwargs:
27                max_entries = kwargs['max_entries']
28                if max_entries != None and len(function.cache) >= max_entries:
29                    now = datetime.now()
30                    function.cache = { key: function.cache[key] for key in function.cache.keys() if function.cache[key]['expiry'] > now }
31                    # if nothing is expired that is deletable, delete the first
32                    if len(function.cache) >= max_entries:
33                        del function.cache[next(iter(function.cache))]
34            function.cache[args] = {'result': function(*args), 'expiry': datetime.max if 'ttl' not in kwargs else datetime.now() + kwargs['ttl']}
35
36        # answer from cache
37        return function.cache[args]['result']
38    return wrapper
39  return decorator
40
queries leading to this page
40cache pythoncache in pythonpython cache propertypython import cachepython functools cachehow to import cache pythonpython files cachepython cache functionclear cache pythonpython print cacheslocal cache pythonpython cache fileimplement cache in pythonpython cache controlpython how to cache a functionpython cachedifferent types of cache in python 40cache 28 29 in pthonusing cache in pythonpython cache leerenimport cache pythoncache python decoratorpython3 cache function resultpython cache a functionpython cache infopython make a cache functioncache python maintain function namecache property pythoncache values in pythonhow to cache a function in pythoncache 28 29 in pythonpython cachinghow to store the result in cache pythonfunctools cache pythoncache memory pythonhow to create cache in pythonpython cache importbrowser cache in pythonuse 40cache in pythonclean python cachepython cache clearcache function pythonpython cache librarypython cachewhat is 40cache in pythonpython local cachehow to use put data in cache pythonpython cache for timepython3 cachecache python examplecelery python cache python cache pywhat is a cache in pythonwhere is python cache modulesimple python cachecache values in python returncache python functionpython file cachepython code use cachepython cache result of a function 40cache in pythonpython cached functionpython cache a responsecache data pythonrerun cache function pythonpython 40cachepoython caches installpython cache method resultcache pythonpython function cachedata from cache pythonhow to cache in pythonusing cached pythonhow to prevent python cacherequest cachepythonpython function cache