File size: 5,265 Bytes
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae32d7
 
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "net/http_cache.hpp"

#include <algorithm>
#include <cctype>
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <sstream>

namespace astra {

namespace {

int64_t now_epoch_sec() {
    using namespace std::chrono;
    return duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
}

uint64_t fnv1a_64(const std::string& s) {
    uint64_t h = 14695981039346656037ULL;
    for (unsigned char c : s) {
        h ^= c;
        h *= 1099511628211ULL;
    }
    return h;
}

} // namespace

HttpCache::HttpCache(std::string cache_dir, size_t max_memory_bytes)
    : cache_dir_(std::move(cache_dir)), max_memory_bytes_(max_memory_bytes) {
    stats_.max_bytes = max_memory_bytes_;
    std::error_code ec;
    std::filesystem::create_directories(cache_dir_, ec);
}

std::string HttpCache::disk_path_for(const std::string& url) const {
    std::ostringstream oss;
    oss << cache_dir_ << "/" << std::hex << std::setw(16) << std::setfill('0') << fnv1a_64(url) << ".acache";
    return oss.str();
}

int64_t HttpCache::parse_max_age(const std::string& cache_control) {
    std::string low = cache_control;
    std::transform(low.begin(), low.end(), low.begin(),
                   [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
    if (low.find("no-store") != std::string::npos || low.find("no-cache") != std::string::npos) {
        return 0;
    }
    size_t pos = low.find("max-age=");
    if (pos == std::string::npos) return 300;
    return std::max<int64_t>(0, std::atoll(low.c_str() + pos + 8));
}

bool HttpCache::lookup(const std::string& url, CachedResource& out) {
    int64_t now = now_epoch_sec();

    // 1. Check bounded RAM cache
    auto it = mem_entries_.find(url);
    if (it != mem_entries_.end()) {
        const auto& res = it->second.res;
        if (res.max_age_sec > 0 && (now - res.stored_epoch_sec) <= res.max_age_sec) {
            it->second.last_access_tick = ++tick_;
            out = res;
            ++stats_.hits;
            return true;
        }
        stats_.current_bytes -= it->second.res.body.size();
        mem_entries_.erase(it);
    }

    // 2. Check persistent disk cache
    std::ifstream f(disk_path_for(url), std::ios::binary);
    if (f) {
        std::string stored_url, ctype, etag;
        int64_t epoch = 0, max_age = 0;
        size_t body_len = 0;
        if (std::getline(f, stored_url) && std::getline(f, ctype) && std::getline(f, etag)) {
            if ((f >> epoch >> max_age >> body_len) && stored_url == url) {
                f.get(); // consume newline
                if (max_age > 0 && (now - epoch) <= max_age && body_len <= 16ULL * 1024 * 1024) {
                    std::string body(body_len, '\0');
                    if (f.read(body.data(), static_cast<std::streamsize>(body_len))) {
                        out = CachedResource{stored_url, ctype, etag, std::move(body), epoch, max_age};
                        // Promote to bounded RAM cache
                        if (out.body.size() <= max_memory_bytes_ / 4) {
                            mem_entries_[url] = MemSlot{out, ++tick_};
                            stats_.current_bytes += out.body.size();
                            evict_if_needed();
                        }
                        ++stats_.hits;
                        return true;
                    }
                }
            }
        }
    }

    ++stats_.misses;
    return false;
}

void HttpCache::store(const CachedResource& entry) {
    if (entry.max_age_sec <= 0 || entry.body.size() > 16ULL * 1024 * 1024) return;

    CachedResource copy = entry;
    if (copy.stored_epoch_sec == 0) copy.stored_epoch_sec = now_epoch_sec();

    // Write to disk cache
    std::error_code ec;
    std::filesystem::create_directories(cache_dir_, ec);
    std::ofstream f(disk_path_for(copy.url), std::ios::binary);
    if (f) {
        f << copy.url << "\n"
          << copy.content_type << "\n"
          << copy.etag << "\n"
          << copy.stored_epoch_sec << " " << copy.max_age_sec << " " << copy.body.size() << "\n";
        f.write(copy.body.data(), static_cast<std::streamsize>(copy.body.size()));
    }

    // Store in RAM cache if within per-entry limit
    if (copy.body.size() <= max_memory_bytes_ / 2) {
        auto it = mem_entries_.find(copy.url);
        if (it != mem_entries_.end()) {
            stats_.current_bytes -= it->second.res.body.size();
        }
        stats_.current_bytes += copy.body.size();
        mem_entries_[copy.url] = MemSlot{std::move(copy), ++tick_};
        evict_if_needed();
    }
    ++stats_.stores;
}

void HttpCache::evict_if_needed() {
    while (stats_.current_bytes > max_memory_bytes_ && !mem_entries_.empty()) {
        auto oldest = mem_entries_.begin();
        for (auto it = mem_entries_.begin(); it != mem_entries_.end(); ++it) {
            if (it->second.last_access_tick < oldest->second.last_access_tick) {
                oldest = it;
            }
        }
        stats_.current_bytes -= oldest->second.res.body.size();
        mem_entries_.erase(oldest);
        ++stats_.evictions;
    }
}

void HttpCache::clear() {
    mem_entries_.clear();
    stats_.current_bytes = 0;
}

} // namespace astra