lru cache example

Solutions on MaxInterview for lru cache example by the best coders in the world

showing results for - "lru cache example"
Agustina
02 May 2019
1from functools import lru_cache
2import requests
3
4@lru_cache(maxsize=32)
5def get_with_cache(url):
6    try:
7        r = requests.get(url)
8        return r.text
9    except:
10        return "Not Found"
11
12
13for url in ["https://google.com/",
14            "https://martinheinz.dev/",
15            "https://reddit.com/",
16            "https://google.com/",
17            "https://dev.to/martinheinz",
18            "https://google.com/"]:
19    get_with_cache(url)
20
21print(get_with_cache.cache_info())
22# CacheInfo(hits=2, misses=4, maxsize=32, currsize=4)