Skip to content

lonelyenvoy/python-memoization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

python-memoization

A minimalist functional caching lib for Python, with TTL and auto memory management support.

Installation

pip install memoization

Usage in 2 lines

def fun(arg):
    return do_something_slow(arg)

Wanna caching this function? That's what you need:

from memoization import cached

@cached()
def fun(arg):
    return do_something_slow(arg)

Repetitive calls to fun() with the same arguments then run fun() only once.

Advanced topics

TTL (Time-To-Live)

@cached(ttl=5)  # cache expires after 5 seconds
def read_user_info(user):
    return expensive_db_query(user)

For impure functions, TTL will be a solution.

Limited cache capacity

@cached(max_items=1000)  # cache holds no more than 1000 items
def get_compiled_binary(filename):
    return a_very_large_object(filename)

When the cache is fully occupied, the data first came will be overwritten.

Contributing

Any improvement or bug-fixing is welcome. Create a pull request when you are done.

License

The MIT License