Skip to content

Commit

Permalink
Lowered lru_cache size and made cache_info and cache_clear work on lr…
Browse files Browse the repository at this point in the history
…u_cached methods.
  • Loading branch information
fredrik-corneliusson committed Nov 8, 2022
1 parent 5bfbec5 commit c7cf437
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def test_calendar(self):
def test_isin(self):
data = self.ticker.isin
self.assertIsInstance(data, str, "data has wrong type")
self.assertEquals("ARDEUT116159", data, "data is empty")
self.assertEqual("ARDEUT116159", data, "data is empty")

data_cached = self.ticker.isin
self.assertIs(data, data_cached, "data not cached")
Expand Down
12 changes: 8 additions & 4 deletions yfinance/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
except ImportError:
import json as json

cache_maxsize = 128
cache_maxsize = 64


def freezeargs(func):
def lru_cache_freezeargs(func):
"""
Decorator transforms mutable dictionary arguments into immutable
Needed so lru_cache can cache method calls what has dict arguments.
Expand All @@ -28,6 +28,10 @@ def wrapped(*args, **kwargs):
kwargs = {k: frozendict(v) if isinstance(v, dict) else v for k, v in kwargs.items()}
return func(*args, **kwargs)

# copy over the lru_cache extra methods to this wrapper to be able to access them
# after this decorator has been applied
wrapped.cache_info = func.cache_info
wrapped.cache_clear = func.cache_clear
return wrapped


Expand All @@ -42,7 +46,7 @@ def __init__(self, ticker: str, session=None):
self._ticker = ticker
self._session = session or requests

@freezeargs
@lru_cache_freezeargs
@lru_cache(maxsize=cache_maxsize)
def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30):
proxy = self._get_proxy(proxy)
Expand All @@ -62,7 +66,7 @@ def _get_proxy(self, proxy):
proxy = {"https": proxy}
return proxy

@freezeargs
@lru_cache_freezeargs
@lru_cache(maxsize=cache_maxsize)
def get_json_data_stores(self, url, proxy=None):
'''
Expand Down

0 comments on commit c7cf437

Please sign in to comment.