From e33310f26351e25fda718a353f8b8e9ece0007b5 Mon Sep 17 00:00:00 2001 From: frosty Date: Tue, 10 Mar 2026 03:40:34 -0400 Subject: feature: added caching --- src/Infobox/Wikipedia.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src/Infobox/Wikipedia.c') diff --git a/src/Infobox/Wikipedia.c b/src/Infobox/Wikipedia.c index ffcc75b..d22fd89 100644 --- a/src/Infobox/Wikipedia.c +++ b/src/Infobox/Wikipedia.c @@ -1,4 +1,5 @@ #include "Wikipedia.h" +#include "../Cache/Cache.h" #include "../Proxy/Proxy.h" #include "../Scraping/Scraping.h" #include @@ -117,6 +118,32 @@ InfoBox fetch_wiki_data(char *api_url) { struct WikiMemoryStruct chunk; InfoBox info = {NULL, NULL, NULL, NULL}; + if (!api_url) { + return info; + } + + char *cache_key = cache_compute_key(api_url, 0, "wikipedia"); + if (cache_key && get_cache_ttl_infobox() > 0) { + char *cached_data = NULL; + size_t cached_size = 0; + if (cache_get(cache_key, get_cache_ttl_infobox(), &cached_data, + &cached_size) == 0 && + cached_data && cached_size > 0) { + xmlDocPtr doc = + xmlReadMemory(cached_data, cached_size, "noname.xml", NULL, 0); + if (doc != NULL) { + xmlNode *root_element = xmlDocGetRootElement(doc); + extract_wiki_info(root_element, &info); + xmlFreeDoc(doc); + } + free(cached_data); + free(cache_key); + return info; + } + free(cached_data); + } + free(cache_key); + chunk.memory = malloc(1); chunk.size = 0; @@ -132,7 +159,13 @@ InfoBox fetch_wiki_data(char *api_url) { res = curl_easy_perform(curl_handle); - if (res == CURLE_OK) { + if (res == CURLE_OK && chunk.size > 0) { + cache_key = cache_compute_key(api_url, 0, "wikipedia"); + if (cache_key && get_cache_ttl_infobox() > 0) { + cache_set(cache_key, chunk.memory, chunk.size); + } + free(cache_key); + xmlDocPtr doc = xmlReadMemory(chunk.memory, chunk.size, "noname.xml", NULL, 0); if (doc != NULL) { -- cgit v1.2.3