You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python's requests library decorated with an LRU cache for similar GET requests. Supports an optional TTL policy.
Usage:
fromlru_cache_http_clientimportget_caching_clientimporttimedefno_ttl_google():
URL="https://www.google.com"caching_client=get_caching_client(capacity=2)
start=time.time()
res1=caching_client.get(URL)
after_req_1=time.time()
res2=caching_client.get(URL)
finish=time.time()
print("Time for initial request: {}".format(after_req_1-start))
print("Time for duplicate request: {}".format(finish-after_req_1))
print("Total time: {}".format(finish-start))
defttl_google():
URL="https://www.google.com"caching_client=get_caching_client(capacity=2, ttl_seconds=1)
print("With TTL policy of 1 second")
print("Issuing first req to google...")
res1=caching_client.get(URL)
print("Now sleeping for two seconds")
time.sleep(2)
start=time.time()
print("Issuing second req to google...")
res2=caching_client.get(URL)
finish=time.time()
# `res1` will be different than `res2` because# a second request will be issued.# `res1` expires in the cache due to the ttl policyprint(
"Response object 1 vs 2 is {}".format(
"different"ifres1!=res2else"the same"
)
)
assertres1!=res2