Store the cache to a file functools.lru_cache in Python >= 3.2

前端 未结 4 612
清歌不尽
清歌不尽 2020-12-14 01:03

I\'m using @functools.lru_cache in Python 3.3. I would like to save the cache to a file, in order to restore it when the program will be restarted. How could I

4条回答
  •  遥遥无期
    2020-12-14 01:26

    You can use a library of mine, mezmorize

    import random
    from mezmorize import Cache
    
    cache = Cache(CACHE_TYPE='filesystem', CACHE_DIR='cache')
    
    
    @cache.memoize()
    def add(a, b):
        return a + b + random.randrange(0, 1000)
    
    >>> add(2, 5)
    727
    >>> add(2, 5)
    727
    

提交回复
热议问题