One - One Code All

Blog Content

python内存缓存管理wrapcache

Python   2015-09-19 14:05:31

参考库: https://github.com/hustcc/wrapcache

import wrapcache

from time import sleep
import random

@wrapcache.wrapcache(timeout = 3)
def need_cache_function(input, t = 2, o = 3):
    sleep(2)
    return random.randint(1, 100)

if __name__ == "__main__":
    for i in range(10):
        sleep(1)
        print(need_cache_function(1, t = 2, o = 3))
    
    #get cache Programmatic
    key_func = wrapcache.keyof(need_cache_function, 1, o = 3, t = 2)
    print(wrapcache.get(key_func))
    #remove cache Programmatic
    print(wrapcache.remove(wrapcache.keyof(need_cache_function, 1, o = 3, t = 2)))



wrapcache.keyof(func, *args, **kws): get the key of function output cache.
wrapcache.get(key, adapter = MemoryAdapter): get the value of cache.
wrapcache.set(key, value, timeout = -1, adapter = MemoryAdapter): set cache use code.
wrapcache.remove(key, adapter = MemoryAdapter): remove a cache.
wrapcache.flush(adapter = MemoryAdapter): clear all the cache.


上一篇:量化入门书籍推荐
下一篇:docker logs实时查看日志tail

The minute you think of giving up, think of the reason why you held on so long.