Closed rburghol closed 7 months ago
+1 Thanks for the prompt @rburghol -- I need to think about how to best use this kind of thing in the package. nhdplusTools has undergone a lot of change recently and needs some further clean up.
I've introduced memoise as a dependency for something else and will start working it in over time. Sorry this has been on the back burner for a while.
I roughed in an implementation that I'm pretty happy with. No doubt there'll be issues, but it's a good start. See #364.
You can now set environment variables to control cache location (memory or disc) and duration. I've wrapped functions that make cacheable requests in memoise and use the pattern discussed above to control cache behavior. It defaults to a filesystem cache for one day.
A start is in now -- please test and open follow up issues if things are not right.
This example is based on some code that @dblodgett-usgs shared in an issue on cacheing NWIS queries. https://github.com/DOI-USGS/dataRetrieval/issues/681
Setup
Essentially, one selects a dat directory to store caches, and writes a wrapper around
nhdplustools
queries using the packagememoise
. I used a 1-year timeout since I figured that this data changes slowly, but the veracity of that assumption is less important than the technique:Retrieving point and basin info
After initial setup, calling these functions more than once in a year will see it searching the cache before going out to get fresh data.
First time it takes a full second in my instance, since data is not stored
system.time(nhd <- memo_get_nhdplus(out_point)) Spherical geometry (s2) switched off Spherical geometry (s2) switched on user system elapsed 0.13 0.00 0.83
subsequent query is instantaneous
nhd <- memo_get_nhdplus(out_point)
system.time(nhd <- memo_get_nhdplus(m_cat$basin)) Spherical geometry (s2) switched off although coordinates are longitude/latitude, st_intersects assumes that they are planar Spherical geometry (s2) switched on user system elapsed 3.83 0.14 6.59
again:
system.time(nhd <- memo_get_nhdplus(m_cat$basin)) user system elapsed 0 0 0
m_cat <- memo_plot_nhdplus(list(nhd_out$comid))