File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ # python-memoization
2+ A minimalist functional caching lib for Python
3+
4+ ## Usage in 2 lines
5+
6+ ``` python
7+ def fun (arg ):
8+ return do_something_slow(arg)
9+ ```
10+
11+ Wanna caching this function? That's what you need:
12+
13+ ``` python
14+ from memoization import cached
15+
16+ @cached ()
17+ def fun (arg ):
18+ return do_something_slow(arg)
19+ ```
20+
21+ Repetitive calls to ``` fun() ``` with the same arguments then run ``` fun() ``` only once.
22+
23+
24+ ## Advanced topics
25+
26+ ### TTL (Time-To-Live)
27+
28+ ``` python
29+ @cached (ttl = 5 ) # cache expires after 5 seconds
30+ def read_user_info (user ):
31+ return expensive_db_query(user)
32+ ```
33+
34+ For impure functions, TTL will be a solution.
35+
36+ ### Limited cache capacity
37+
38+ ``` python
39+ @cached (max_items = 1000 ) # cache holds no more than 1000 items
40+ def get_compiled_binary (filename ):
41+ return a_very_large_object(filename)
42+ ```
43+
44+ When the cache is fully occupied, the data first came will be overwritten.
45+
46+
47+ ## Contributing
48+
49+ Any improvement or bug-fixing is welcome. Create a [ pull request] ( https://github.com/lonelyenvoy/python-memoization/pulls ) when you are done.
50+
51+ ## License
52+
53+ [ The MIT License] ( https://github.com/lonelyenvoy/python-memoization/blob/master/LICENSE )
You can’t perform that action at this time.
0 commit comments