commit eed11a1a793d285f9cf8274fb98f3173b4657d02
parent 40c2b73fd7d6548c6de95ccece41cdecfb447179
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 21:09:23 -0500
Added caching; enabled only for gemini:// sites
Diffstat:
8 files changed, 73 insertions(+), 8 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
build:
- g++ src/main.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lssl -lcrypto -lncursesw -o browser.out
+ g++ src/main.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lssl -lcrypto -lncursesw -o browser.out
install:
echo "Not implemented"
@@ -8,12 +8,12 @@ clean:
echo "Not implemented"
browser-test:
- g++ tests/browser-test.cpp src/plaintext.cpp src/browser.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
+ g++ tests/browser-test.cpp src/cache.cpp src/plaintext.cpp src/browser.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
./test.out
rm test.out
fetch-test:
- g++ tests/fetch-test.cpp src/link.cpp src/site.cpp src/plaintext.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
+ g++ tests/fetch-test.cpp src/link.cpp src/cache.cpp src/site.cpp src/plaintext.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
./test.out
rm test.out
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -5,6 +5,7 @@
#include <vector>
#include "line.hpp"
#include "site.hpp"
+#include "cache.hpp"
#include "link.hpp"
class Browser {
@@ -12,13 +13,16 @@ class Browser {
std::unordered_map<std::string, int> previousStatusCodes;
std::vector<Link*> siteHistory;
int previousIdx = -1;
+ Cache* cache;
Site* currentSite;
std::vector<Line*> lines;
std::vector<std::size_t> links; // these point to line indices
void setLinksOfCurrentLines();
public:
Browser();
- void goToSite(std::string url, bool addToHistory);
+ ~Browser();
+ void goToSite(std::string url, bool addToHistory, bool refresh = false);
+ void refresh();
Site* getCurrentSite();
std::optional<uri> getPriorUri();
std::vector<std::pair<std::string, int>> renderSite();
diff --git a/include/cache.hpp b/include/cache.hpp
@@ -0,0 +1,12 @@
+// cache for prior sites and such.
+#include "site.hpp"
+#include <optional>
+#include <unordered_map>
+
+class Cache {
+ private:
+ std::unordered_map<std::string,Site> cache;
+ public:
+ std::optional<Site> getSite(const std::string& site);
+ void addSite(std::string address, Site site);
+};
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -7,7 +7,11 @@
#include <utility>
#include <vector>
-void Browser::goToSite(std::string url, bool addToHistory) {
+void Browser::refresh() {
+ goToSite(getPriorUri().value().to_string(), false, true);
+}
+
+void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
Link* prior = nullptr;
@@ -25,7 +29,21 @@ void Browser::goToSite(std::string url, bool addToHistory) {
destination = new Link{url,prior->getLinkDestination()};
}
- Site* site = client.fetchSite(*destination);
+
+ Site* site = nullptr;
+
+ std::string urlString = destination->getLinkDestination().to_string();
+ if(urlString.find("gemini://") != -1 && !refresh) {
+ std::optional<Site> cachedSite = cache->getSite(urlString);
+ if(cachedSite != std::nullopt) {
+ site = new Site(*cachedSite);
+ }
+ }
+
+ if(site == nullptr) {
+ site = client.fetchSite(*destination);
+ }
+
if(site == nullptr || site->getUnreachable()) {
if(site != nullptr) {
@@ -48,6 +66,11 @@ void Browser::goToSite(std::string url, bool addToHistory) {
delete currentSite;
}
currentSite = site;
+
+ if(urlString.find("gemini://") != -1) {
+ cache->addSite(urlString, *site);
+ }
+
lines = toLines(site);
setLinksOfCurrentLines();
previousStatusCodes[destination->getLinkDestination().to_string()] = site->getStatusCode();
@@ -84,8 +107,17 @@ std::vector<Line*> Browser::toLines(Site* site) {
Browser::Browser() {
currentSite = nullptr;
+ cache = new Cache{};
}
+// TODO: SHould add more stuff here too, like the links stuff.
+Browser::~Browser() {
+ if (cache != nullptr) {
+ delete cache;
+ }
+}
+
+
Site* Browser::getCurrentSite() {
return currentSite;
}
diff --git a/src/cache.cpp b/src/cache.cpp
@@ -0,0 +1,17 @@
+#include "../include/cache.hpp"
+#include "../include/site.hpp"
+#include <cassert>
+#include <optional>
+
+std::optional<Site> Cache::getSite(const std::string& site) {
+ auto it = cache.find(site);
+ if (it != cache.end()) {
+ return it->second;
+ }
+ return std::nullopt;
+}
+
+void Cache::addSite(std::string address, Site site) {
+ assert(address.find("gemini://") != -1);
+ cache.insert_or_assign(address,site);
+}
diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp
@@ -71,7 +71,6 @@ Site* GeminiClient::getNetworkedSite(Link link) {
Site* GeminiClient::fetchSite(Link link) {
-
std::string destination = link.getLinkDestination().to_string();
// TODO: Actually handle uris
diff --git a/src/main.cpp b/src/main.cpp
@@ -277,6 +277,8 @@ int main(int argc, char** argv) {
y += LINES / 2;
} else if (input == 0x15) {
y -= LINES / 2;
+ } else if (input == 'r'){
+ b.refresh();
} else if(input == 'f') {
b.goForward();
diff --git a/src/site.cpp b/src/site.cpp
@@ -33,4 +33,3 @@ void Site::setUnreachable() {
bool Site::getUnreachable() {
return unreachable;
}
-