Python LRU Cache Decorator Per Instance

前端 未结 3 1450
情书的邮戳
情书的邮戳 2020-12-13 09:16

Using the LRU Cache decorator found here: http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/

from lru_cache import lr         


        
3条回答
  •  醉梦人生
    2020-12-13 09:34

    These days, methodtools will work

    from methodtools import lru_cache
    class Test:
        @lru_cache(maxsize=16)
        def cached_method(self, x):
             return x + 5
    

    You need to install methodtools

    pip install methodtools
    

    If you are still using py2, then functools32 also is required

    pip install functools32
    

提交回复
热议问题