Fix null pointer dereferences when redis is disabled
This commit is contained in:
parent
109556f2b7
commit
c0a424615c
|
@ -1,6 +1,7 @@
|
||||||
#include <FastHash.h>
|
#include <FastHash.h>
|
||||||
|
|
||||||
#include "blankie/murl.h"
|
#include "blankie/murl.h"
|
||||||
|
#include "hiredis_wrapper.h"
|
||||||
#include "numberhelper.h"
|
#include "numberhelper.h"
|
||||||
#include "pixivclient.h"
|
#include "pixivclient.h"
|
||||||
|
|
||||||
|
@ -66,10 +67,12 @@ std::vector<SearchSuggestion> PixivClient::get_search_suggestions(std::string qu
|
||||||
to_lower(query);
|
to_lower(query);
|
||||||
std::string body = [&]() {
|
std::string body = [&]() {
|
||||||
std::string cache_key = "pixwhile:searchsugg:"s + std::to_string(FastHash(query.data(), query.size(), 0));
|
std::string cache_key = "pixwhile:searchsugg:"s + std::to_string(FastHash(query.data(), query.size(), 0));
|
||||||
|
if (this->_redis) {
|
||||||
std::optional<std::string> cached_body = this->_redis->get(cache_key);
|
std::optional<std::string> cached_body = this->_redis->get(cache_key);
|
||||||
if (cached_body) {
|
if (cached_body) {
|
||||||
return std::move(*cached_body);
|
return std::move(*cached_body);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
httplib::Result res = this->_www_pixiv_net_client.Get("/rpc/cps.php", {
|
httplib::Result res = this->_www_pixiv_net_client.Get("/rpc/cps.php", {
|
||||||
{"lang", "en"}, {"keyword", query}
|
{"lang", "en"}, {"keyword", query}
|
||||||
|
@ -78,7 +81,9 @@ std::vector<SearchSuggestion> PixivClient::get_search_suggestions(std::string qu
|
||||||
throw HTTPLibException(res.error());
|
throw HTTPLibException(res.error());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->_redis) {
|
||||||
this->_redis->set(std::move(cache_key), res->body, 24 * 60 * 60);
|
this->_redis->set(std::move(cache_key), res->body, 24 * 60 * 60);
|
||||||
|
}
|
||||||
return std::move(res->body);
|
return std::move(res->body);
|
||||||
}();
|
}();
|
||||||
nlohmann::json j = nlohmann::json::parse(std::move(body)).at("candidates");
|
nlohmann::json j = nlohmann::json::parse(std::move(body)).at("candidates");
|
||||||
|
@ -98,12 +103,14 @@ std::vector<SearchSuggestion> PixivClient::get_search_suggestions(std::string qu
|
||||||
|
|
||||||
nlohmann::json PixivClient::_call_api(std::string cache_key, std::optional<std::string> cache_field, time_t expiry,
|
nlohmann::json PixivClient::_call_api(std::string cache_key, std::optional<std::string> cache_field, time_t expiry,
|
||||||
std::string path, httplib::Params params, httplib::Headers headers) {
|
std::string path, httplib::Params params, httplib::Headers headers) {
|
||||||
|
if (this->_redis) {
|
||||||
std::optional<std::string> success_body = !cache_field
|
std::optional<std::string> success_body = !cache_field
|
||||||
? this->_redis->get(cache_key)
|
? this->_redis->get(cache_key)
|
||||||
: this->_redis->hget(cache_key, *cache_field);
|
: this->_redis->hget(cache_key, *cache_field);
|
||||||
if (success_body) {
|
if (success_body) {
|
||||||
return nlohmann::json::parse(std::move(*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));
|
httplib::Result res = this->_www_pixiv_net_client.Get(std::move(path), std::move(params), std::move(headers));
|
||||||
if (!res) {
|
if (!res) {
|
||||||
|
@ -117,12 +124,14 @@ nlohmann::json PixivClient::_call_api(std::string cache_key, std::optional<std::
|
||||||
j = j.at("body");
|
j = j.at("body");
|
||||||
// trim data sent to redis without making our own serialization of our objects
|
// trim data sent to redis without making our own serialization of our objects
|
||||||
j.erase("ads");
|
j.erase("ads");
|
||||||
|
if (this->_redis) {
|
||||||
if (!cache_field) {
|
if (!cache_field) {
|
||||||
this->_redis->set(std::move(cache_key), j.dump(), expiry);
|
this->_redis->set(std::move(cache_key), j.dump(), expiry);
|
||||||
} else {
|
} else {
|
||||||
this->_redis->hset(cache_key, std::move(*cache_field), j.dump());
|
this->_redis->hset(cache_key, std::move(*cache_field), j.dump());
|
||||||
this->_redis->expire_nx(std::move(cache_key), expiry);
|
this->_redis->expire_nx(std::move(cache_key), expiry);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include "pixivmodels.h"
|
#include "pixivmodels.h"
|
||||||
#include "hiredis_wrapper.h"
|
class Redis; // forward declared from hiredis_wrapper.h
|
||||||
|
|
||||||
class PixivClient {
|
class PixivClient {
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in New Issue