foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

cache.cpp (890B)


      1 #include "../include/cache.hpp"
      2 
      3 #include <cassert>
      4 #include <mutex>
      5 #include <optional>
      6 
      7 #include "../include/shared.hpp"
      8 #include "../include/site.hpp"
      9 
     10 std::optional<Site> Cache::getSite(const std::string& site) {
     11     std::lock_guard<std::mutex> lock(mutex);
     12     auto it = cache.find(site);
     13 
     14     if (it != cache.end()) {
     15         return it->second;
     16     }
     17 
     18     return std::nullopt;
     19 }
     20 
     21 void Cache::addSite(std::string address, Site site) {
     22     std::lock_guard<std::mutex> lock(mutex);
     23     assert(address.find("gemini://") != std::string::npos);
     24 
     25     auto it = cache.find(address);
     26     if (it != cache.end()) {
     27         it->second = std::move(site);
     28         return;
     29     }
     30     cache.emplace(address, std::move(site));
     31 
     32     evictionQueue.push_front(address);
     33     if ((int)evictionQueue.size() > CACHE_SIZE) {
     34         cache.erase(evictionQueue.back());
     35         evictionQueue.pop_back();
     36     }
     37 }