From b5cc6373fbfe2db20bb14216242c2c56f9bfbafe Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 4 Nov 2024 13:56:01 -0500 Subject: Clean up cache every day Also add a slight delay when downloading images to prevent rate limiting from Scryfall. --- imagestore.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'imagestore.cpp') diff --git a/imagestore.cpp b/imagestore.cpp index 09bef92..5094569 100644 --- a/imagestore.cpp +++ b/imagestore.cpp @@ -2,10 +2,14 @@ #include +#include #include #include +#include Magick::Image imagestore::get(std::string key, std::string url) const { + std::lock_guard lock(mutex_); + std::string filename = path_ + "/" + key; Magick::Image pic; @@ -13,6 +17,9 @@ Magick::Image imagestore::get(std::string key, std::string url) const { if (std::ifstream(filename)) { pic.read(filename); } else { + // This is to avoid getting rate limited. + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + std::ostringstream imgbuf; curl::curl_ios imgios(imgbuf); curl::curl_easy imghandle(imgios); @@ -50,3 +57,11 @@ Magick::Image imagestore::get(std::string key, std::string url) const { return pic; } + +void imagestore::cleanup() const { + std::lock_guard lock(mutex_); + + for (const auto& entry : std::filesystem::directory_iterator(path_)) { + std::filesystem::remove_all(entry); + } +} -- cgit 1.4.1