说明
pip install walrus
代码示例
# -*- coding: utf-8 -*-
import time
import datetime
from walrus import Database
db = Database()
cache = db.cache()
curr_time = lambda: str(datetime.datetime.now())
@cache.cached(timeout=2)
def test(p=None): # 参数相同的情况下,超过2S后才会第二次执行函数,反之直接返回缓存值
print('start... p={}'.format(p))
return datetime.datetime.now()
print("test(1) 返回 {}".format(test(1)))
time.sleep(1)
print("test(1) 返回 {}".format(test(1)))
time.sleep(1)
print("test(2) 返回 {}".format(test(2)))
time.sleep(1)
print("test(2) 返回 {}".format(test(2)))
time.sleep(1)
print("test(2) 返回 {}".format(test(2)))
代码运行