tarkah / tickrs

Realtime ticker data in your terminal 📈
MIT License
1.17k stars 58 forks source link

Cache data between time range view changes #11

Closed daniel-j-h closed 3 years ago

daniel-j-h commented 3 years ago

Hey hey :wave:

It looks like we're fetching data every time we change the time range plots.

On slow connections this leads to lag when users switch to a different time resolution and then back e.g. between 1D -> 1W -> 1D.

It looks like we already cache the data between tickets, since tab-ing through them draws the plots instantly.

tarkah commented 3 years ago

Hey, thanks for the few suggestions! Yeah, I can definitely cache the data. I'll still destroy the API connection when the time frame isn't selected, so if it's been a few minutes you've been off the 1D time frame and you switch back to it, it'll pop up immediately, but it still might take a second or two for the latest few data points to show up.

Let me know if that sounds good.

daniel-j-h commented 3 years ago

Yeap that sounds good!

Maybe we can adapt the cache based on the time range? The 5Y data can probably be cached longer than e.g. the 1D data :smile:

tarkah commented 3 years ago

So I've already got that logic built in. The 1D data has 1 minute data points so I only request an update every 1 minute, whereas the 5Y data is 1 day data points so it sleeps for 24 hours before requesting an update. However, regardless of time frame it queries the current market price every second so current price is always live and accurate.

When a stock is added, I only create an API connection for the chosen time frame instead of them all. I did this to keep things lightweight in case someone had a ton of stocks added. I'd prefer to keep it this way.

Therefore, the cache would only be used to store data already fetched from the API so that there isn't a visual lag when switching to a time frame that's already been selected. But since the API connection needs to be recreated, any newer data points might take a second or two to show up on the graph.