1

I am testing lru_cache for the first time. A simple request. The code works fine but multiple runs of the code do not change cache_info()

import requests
from functools import lru_cache

url = 'http://www.python.org/dev/peps/pep-'

my_list = [290, 308, 320, 8, 218, 320, 279, 289, 320]

@lru_cache(maxsize=20, typed=False)
def fetch_data():
    for item in my_list:
        data = requests.get(url+str(item))
        print(len(data.text))


if __name__ == "__main__":
    fetch_data()
    print(fetch_data.cache_info())

Results:

53159
41324
21598
118304
20618
21598
20153
30451
21598
**CacheInfo(hits=0, misses=1, maxsize=20, currsize=1)**
➜  ~ /usr/local/bin/python3 pythonProject/main_6.py
53159
41324
21598
118304
20618
21598
20153
30451
21598
**CacheInfo(hits=0, misses=1, maxsize=20, currsize=1)**

With the subsequent runs, I was expecting the hits/misses/currsize parameters to change?

marqeu
  • 11
  • 3

1 Answers1

0

I think this solution does fix it.

import requests
from functools import lru_cache

url = 'http://www.python.org/dev/peps/pep-'

my_list = [290, 308, 320, 8, 218, 320, 279, 289, 320]

@lru_cache(maxsize=20, typed=False)
def fetch_data(item):
    data = requests.get(url+str(item))
    print(len(data.text))
    return data.text


if __name__ == "__main__":
    for item in my_list:
        fetch_data(item)
    print(fetch_data.cache_info())
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
marqeu
  • 11
  • 3