Function Memoizing¶
Caches can also be used to store function calls, as a way of memoize.
Because this is dict based, any function calls must have hashable arguments & keywords. Better to not cache than to blow up entirely.
Example
import faste
@faste.decor.rr_cache()
def takes_long(*args):
time.sleep(5)
return args
takes_long("a", "b", "c")
# waits 5 seconds before returning ("a", "b", "c",)
takes_long("a", "b", "c")
# returns ("a", "b", "c",) immediately
takes_long.clear_cache()
takes_long("a", "b", "c")
# waits 5 seconds before returning ("a", "b", "c",)
Cache Decorators¶
-
faste.decor.
rr_cache
(max_size=128)¶ Random Replacement cache decorator, implementing
faste.caches.RRCache
Keyword Arguments: max_size (int) – max cache size
-
faste.decor.
lru_cache
(max_size=128)¶ Least Recently Used cache decorator, implementing
faste.caches.LRUCache
Keyword Arguments: max_size – max cache size
-
faste.decor.
lfu_cache
(max_size=128)¶ Least Frequently Used cache decorator, implementing
faste.caches.LFUCache
Keyword Arguments: max_size – max cache size
-
faste.decor.
timed_cache
(timeout, max_size=128)¶ Time based decorator, implementing
faste.caches.TimeoutCache
Parameters: - timeout (int) – Cache key timeout
- max_size (int) – (keyword) max size.