commit 4412852aab0b3d0fc56548b6774b803c9b6adfe1
parent 526165e3c3c38941ea32fc9d59424bd3a56d73ff
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 23 Sep 2026 12:09:15 -0500
Added (provisional) aggressive caching
Diffstat:
5 files changed, 96 insertions(+), 3 deletions(-)
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -28,6 +28,9 @@ class Browser {
std::vector<std::pair<std::string, int>> renderSite();
std::vector<Line*> toLines(Site* site);
void followLinkNumber(int linkToFollow);
+ std::vector<Link> getLinkLines();
+
+ void justCacheSite(Link link);
void goBack();
void goForward();
};
diff --git a/include/cache.hpp b/include/cache.hpp
@@ -1,12 +1,20 @@
// cache for prior sites and such.
#include "site.hpp"
+#include <mutex>
+#include <deque>
#include <optional>
#include <unordered_map>
+enum Importance {
+ IMPORTANT,
+ NOT_IMPORTANT
+};
+
class Cache {
private:
std::unordered_map<std::string,Site> cache;
+ std::mutex mutex;
public:
std::optional<Site> getSite(const std::string& site);
- void addSite(std::string address, Site site);
+ void addSite(std::string address, Site site, Importance importance = IMPORTANT);
};
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -88,6 +88,29 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
previousStatusCodes[destination->getLinkDestination().to_string()] = site->getStatusCode();
}
+void Browser::justCacheSite(Link link) {
+ auto client = GeminiClient{};
+
+
+ std::string urlString = link.getLinkDestination().to_string();
+
+ Site* site = nullptr;
+
+
+ if(urlString.find("gemini://") != -1) {
+ std::optional<Site> cachedSite = cache->getSite(urlString);
+ if(cachedSite == std::nullopt) {
+ site = client.fetchSite(link);
+ }
+ }
+
+ if(site != nullptr) {
+ cache->addSite(urlString, *site, NOT_IMPORTANT);
+ }
+ return;
+}
+
+
void Browser::setLinksOfCurrentLines() {
links = std::vector<std::size_t> {};
for(std::size_t i = 0; i < lines.size(); ++i) {
@@ -152,6 +175,15 @@ std::optional<uri> Browser::getPriorUri() {
}
+std::vector<Link> Browser::getLinkLines() {
+ std::vector<Link> res {};
+ for(auto& ln : links) {
+ res.push_back(*dynamic_cast<Link*>(lines[ln]));
+ }
+ return res;
+}
+
+
void Browser::followLinkNumber(int linkToFollow) {
if(links.size() > linkToFollow-1 && linkToFollow-1 >= 0) {
std::size_t pos = links[linkToFollow-1];
diff --git a/src/cache.cpp b/src/cache.cpp
@@ -1,9 +1,12 @@
#include "../include/cache.hpp"
#include "../include/site.hpp"
#include <cassert>
+#include <mutex>
#include <optional>
+
std::optional<Site> Cache::getSite(const std::string& site) {
+ std::lock_guard<std::mutex> lock(mutex);
auto it = cache.find(site);
if (it != cache.end()) {
return it->second;
@@ -11,7 +14,8 @@ std::optional<Site> Cache::getSite(const std::string& site) {
return std::nullopt;
}
-void Cache::addSite(std::string address, Site site) {
+void Cache::addSite(std::string address, Site site, Importance importance) { // TODO: Use importance
+ std::lock_guard<std::mutex> lock(mutex);
assert(address.find("gemini://") != -1);
cache.insert_or_assign(address,site);
}
diff --git a/src/main.cpp b/src/main.cpp
@@ -1,18 +1,31 @@
#include "../include/browser.hpp"
+#include "../include/gemini-client.hpp"
#include "../include/utils.hpp"
#include <algorithm>
+#include <atomic>
#include <cmath>
#include <cstddef>
#include <filesystem>
#include <iostream>
#include <ncurses.h>
#include <string>
+#include <thread>
#include <utility>
#include <vector>
+const int THREAD_NUM = 4;
+std::vector<std::thread> threads(THREAD_NUM);
+std::vector<std::atomic<bool>> done(THREAD_NUM);
+
// we do this because this is built against ncurses, would be nice to do away w/ this
// bc ppl use lots of emojis on gemini sites.
+void aggressiveCaching(Browser* bPtr, std::vector<Link> targets, int threadIdx) {
+ for(auto& target : targets) {
+ bPtr->justCacheSite(target);
+ }
+ done[threadIdx] = true;
+}
void removeNonAscii(std::vector<std::pair<std::string, int>>& strLs) {
@@ -233,7 +246,12 @@ std::string handleUserInput(DrawState ds) {
int main(int argc, char** argv) {
- Browser b{};
+ for (auto& d : done) {
+ d = true;
+ }
+
+ Browser* bPtr = new Browser{};
+ Browser& b = *bPtr;
DrawState ds {};
@@ -268,6 +286,8 @@ int main(int argc, char** argv) {
int y = 0;
int x = 0;
+ std::thread cacheThread;
+
// this is the main loop.
while( input != 'q') {
@@ -326,6 +346,11 @@ int main(int argc, char** argv) {
current = b.renderSite();
removeNonAscii(current);
current = breakLines(current,COLS);
+ } else {
+ b.goBack();
+ current = b.renderSite();
+ removeNonAscii(current);
+ current = breakLines(current,COLS);
}
} else if (b.getCurrentSite()->getStatusCode() >= 30 && b.getCurrentSite()->getStatusCode() <= 39){
@@ -348,8 +373,29 @@ int main(int argc, char** argv) {
ds.strLs = current;
draw(ds);
refresh();
+
+
+ bool dispatched = false;
+ for(int i = 0; i < THREAD_NUM && dispatched == false; ++i) {
+ if(done[i]) {
+ if(threads[i].joinable()) {
+ threads[i].join();
+ }
+ auto lls = b.getLinkLines();
+ done[i] = false;
+ threads[i] = std::thread(aggressiveCaching, bPtr, lls, i);
+ dispatched = true;
+ }
+ }
+
input = getch();
}
endwin();
+
+ for (auto& t : threads) {
+ if (t.joinable()) {
+ t.join();
+ }
+ }
}