Skip to content

Commit 80905cb

Browse files
perf: cache Horizon account data briefly
1 parent 01573cd commit 80905cb

1 file changed

Lines changed: 28 additions & 23 deletions

File tree

quantara/web_app/contract_tools/blockchain_call.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,37 @@ async def get_balance(
109109

110110
async def _get_account_data(self, holder_address: str) -> dict | None:
111111
"""
112-
Fetch full account data from Horizon.
112+
Fetch full account data from Horizon, cached briefly per account.
113113
"""
114114
if not holder_address:
115115
return None
116-
url = f"{self.horizon_url.rstrip('/')}/accounts/{holder_address}"
117-
try:
118-
async with aiohttp.ClientSession() as session:
119-
async with session.get(url) as response:
120-
if response.status == 404:
121-
logger.info(
122-
"Account %s not found on Stellar network",
123-
holder_address,
124-
)
125-
return None
126-
if response.status != 200:
127-
logger.warning(
128-
"Horizon returned %d for %s", response.status, url
129-
)
130-
return None
131-
return await response.json()
132-
except aiohttp.ClientError as exc:
133-
logger.error("Network error fetching account %s: %s", holder_address, exc)
134-
return None
135-
except (ValueError, KeyError, TypeError) as exc:
136-
logger.error("Data error fetching account %s: %s", holder_address, exc)
137-
return None
116+
117+
async def fetch_account() -> dict | None:
118+
url = f"{self.horizon_url.rstrip('/')}/accounts/{holder_address}"
119+
try:
120+
async with aiohttp.ClientSession() as session:
121+
async with session.get(url) as response:
122+
if response.status == 404:
123+
logger.info(
124+
"Account %s not found on Stellar network",
125+
holder_address,
126+
)
127+
return None
128+
if response.status != 200:
129+
logger.warning(
130+
"Horizon returned %d for %s", response.status, url
131+
)
132+
return None
133+
return await response.json()
134+
except aiohttp.ClientError as exc:
135+
logger.error("Network error fetching account %s: %s", holder_address, exc)
136+
return None
137+
except (ValueError, KeyError, TypeError) as exc:
138+
logger.error("Data error fetching account %s: %s", holder_address, exc)
139+
return None
140+
141+
cache_key = f"horizon:account:{self.horizon_url.rstrip('/')}:{holder_address}"
142+
return await get_cached_or_fetch(cache_key, ttl=5, fetch_fn=fetch_account)
138143

139144
async def get_token_balances(
140145
self, holder_address: str

0 commit comments

Comments
 (0)