diff --git a/pixivclient.cpp b/pixivclient.cpp index fd93a59..5b7583e 100644 --- a/pixivclient.cpp +++ b/pixivclient.cpp @@ -1,6 +1,7 @@ #include #include "blankie/murl.h" +#include "hiredis_wrapper.h" #include "numberhelper.h" #include "pixivclient.h" @@ -66,9 +67,11 @@ std::vector PixivClient::get_search_suggestions(std::string qu to_lower(query); std::string body = [&]() { std::string cache_key = "pixwhile:searchsugg:"s + std::to_string(FastHash(query.data(), query.size(), 0)); - std::optional cached_body = this->_redis->get(cache_key); - if (cached_body) { - return std::move(*cached_body); + if (this->_redis) { + std::optional cached_body = this->_redis->get(cache_key); + if (cached_body) { + return std::move(*cached_body); + } } httplib::Result res = this->_www_pixiv_net_client.Get("/rpc/cps.php", { @@ -78,7 +81,9 @@ std::vector PixivClient::get_search_suggestions(std::string qu throw HTTPLibException(res.error()); } - this->_redis->set(std::move(cache_key), res->body, 24 * 60 * 60); + if (this->_redis) { + this->_redis->set(std::move(cache_key), res->body, 24 * 60 * 60); + } return std::move(res->body); }(); nlohmann::json j = nlohmann::json::parse(std::move(body)).at("candidates"); @@ -98,11 +103,13 @@ std::vector PixivClient::get_search_suggestions(std::string qu nlohmann::json PixivClient::_call_api(std::string cache_key, std::optional cache_field, time_t expiry, std::string path, httplib::Params params, httplib::Headers headers) { - std::optional success_body = !cache_field - ? this->_redis->get(cache_key) - : this->_redis->hget(cache_key, *cache_field); - if (success_body) { - return nlohmann::json::parse(std::move(*success_body)); + if (this->_redis) { + std::optional success_body = !cache_field + ? this->_redis->get(cache_key) + : this->_redis->hget(cache_key, *cache_field); + if (success_body) { + return nlohmann::json::parse(std::move(*success_body)); + } } httplib::Result res = this->_www_pixiv_net_client.Get(std::move(path), std::move(params), std::move(headers)); @@ -117,11 +124,13 @@ nlohmann::json PixivClient::_call_api(std::string cache_key, std::optional_redis->set(std::move(cache_key), j.dump(), expiry); - } else { - this->_redis->hset(cache_key, std::move(*cache_field), j.dump()); - this->_redis->expire_nx(std::move(cache_key), expiry); + if (this->_redis) { + if (!cache_field) { + this->_redis->set(std::move(cache_key), j.dump(), expiry); + } else { + this->_redis->hset(cache_key, std::move(*cache_field), j.dump()); + this->_redis->expire_nx(std::move(cache_key), expiry); + } } return j; } diff --git a/pixivclient.h b/pixivclient.h index b53719f..54d0b16 100644 --- a/pixivclient.h +++ b/pixivclient.h @@ -5,7 +5,7 @@ #include #include "pixivmodels.h" -#include "hiredis_wrapper.h" +class Redis; // forward declared from hiredis_wrapper.h class PixivClient { public: