numbat
numbat copied to clipboard
Cache exchange rates
I use lots of short invocations of numbat with fetching-policy = "on-first-use"
which means it re-fetches the rates every time I open it to do a quick currency conversion. The on-startup
fetching policy makes it a bit better, but the delay is still noticeable if the first thing I do uses currency, and it just feels wasteful to download them every time when they're only updated daily. This change reuses the fetched rates for 24 hours to alleviate that delay.
This could make the rates up to a day out of date (if you happened to fetch them the first time right before the European Central Bank releases the next day's rates). In my mind this isn't hugely important as anyone who needs super up-to-date rates is probably using a different source in the first place, but the max age could be changed to something shorter to make it fetch more aggressively. If users want even more control, max-age
could be specified as a config option in the [exchange-rates]
section.
Thank you for looking into this. I believe we also have a ticket for this.
I only had a quick glance at the code. I think the caching should rather be moved to the application layer, not to the numbat-exchange-rates library. I realize that this is a bit harder to implement (as we need to serialize/deserialize), but I think that would be cleaner.
When you say it should be in the "application layer" do you mean that numbat-cli
should specify how writing and loading the cache file works? For that to work with on-demand currency loading there would need to be some sort of callback when the rates get fetched. I've roughed out that change in this commit but it a bit of a mess. Let me know if that's the direction you were thinking of and if so I'll try to clean it up and add it here.
Also I see #315 but that's kind of the opposite issue, was there another ticket you were thinking of?
I don't fully understand why we need this setup with two closures and additional locks.
Could we add a (possibly atomic?) filesystem_caching_enabled
flag to the ExchangeRates
struct, set that according to the config when starting the application, and then internally have two branches for the caching/non-caching case?
I'm closing this due to inactivity. Please feel free to comment in case it should be re-opened.