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 +++++++++++++++ imagestore.h | 5 +++++ server_main.cpp | 7 +++++-- 3 files changed, 25 insertions(+), 2 deletions(-) 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); + } +} diff --git a/imagestore.h b/imagestore.h index 8c3fecf..2c3495d 100644 --- a/imagestore.h +++ b/imagestore.h @@ -3,6 +3,7 @@ #include +#include #include class imagestore { @@ -11,8 +12,12 @@ class imagestore { Magick::Image get(std::string key, std::string url) const; + void cleanup() const; + private: std::string path_; + + mutable std::mutex mutex_; }; #endif /* end of include guard: IMAGESTORE_H_80B1E49F */ diff --git a/server_main.cpp b/server_main.cpp index 8d2c4aa..34cb50c 100644 --- a/server_main.cpp +++ b/server_main.cpp @@ -36,7 +36,8 @@ class server { socket_.listen(port_); socket_.start_accept(); - asio::post(std::bind(&server::cleanup_thread, this)); + std::thread cleanup(std::bind(&server::cleanup_thread, this)); + cleanup.detach(); std::cout << "Listening on port " << port_ << "..." << std::endl; @@ -145,7 +146,9 @@ class server { void cleanup_thread() { for (;;) { - // sleep + std::this_thread::sleep_for(std::chrono::hours(24)); + + images_.cleanup(); } } -- cgit 1.4.1