Jede Antwort trägt jetzt, was sie gekostet hat: X-Junction-Tokens-In: 1200 X-Junction-Tokens-Out: 300 X-Junction-Cost-Usd: 0.000426 und im Log: usage model=deepseek-v4.1-flash in=1200 cached=1000 out=300 cost_usd=0.000426 Die Preise stehen in examples/prices.conf, abgeschrieben von der Preisseite des Anbieters (ollama.com/pricing, 5.11.2026) — pro Million Token, mit Off-Peak-Preisen, Monatsbudget und Anfragefenster für die Go-Modelle. Zwei Entscheidungen, die wichtiger sind als die Zahlen: Kein "*"-Eintrag. Ein Modell ohne Zeile kostet unbekannt, nicht null, und das steht dann auch so im Log. Eine erfundene Null in einem Kostenbericht ist schlimmer als gar keine Zahl. Ohne Preistabelle wird gar nichts gerechnet — dann fehlt die Kopfzeile, statt eine Null zu zeigen. Damit die Antwort vollständig vorliegt, wird bei gesetzter Tabelle auch ohne Cache vollständig gelesen; ein nicht gestreamter JSON-Rumpf ist ohnehin endlich. Dazu Single-Flight: gleiche Anfragen, die gleichzeitig eintreffen, warten auf die erste und teilen ihr Ergebnis. Drei identische Anfragen an ein Ziel mit 3 s Antwortzeit dauern 3 s statt 9, ein Aufruf geht raus, zwei werden bedient. 111 Prüfungen, keine Fehler.
290 lines
7.8 KiB
C
290 lines
7.8 KiB
C
/* junction/cache.c — LRU cache of complete (non-streamed) responses.
|
|
*
|
|
* Only fully received, non-streaming responses are cached: a streamed answer has no
|
|
* end we could store, and replaying one would turn a live stream into a fake one.
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "cache.h"
|
|
|
|
#include <pthread.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
typedef struct entry {
|
|
uint64_t key;
|
|
char *head;
|
|
size_t head_len;
|
|
char *body;
|
|
size_t body_len;
|
|
double stored;
|
|
double ttl;
|
|
struct entry *prev; /* more recent */
|
|
struct entry *next; /* less recent */
|
|
} entry;
|
|
|
|
/* every request now runs in its own thread, so the cache needs a lock */
|
|
static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;
|
|
#define LOCK_CACHE() pthread_mutex_lock(&g_mu)
|
|
#define UNLOCK_CACHE() pthread_mutex_unlock(&g_mu)
|
|
|
|
static entry *g_head, *g_tail;
|
|
static size_t g_entries, g_bytes;
|
|
static size_t g_max_entries = 0, g_max_bytes = 0;
|
|
static double g_ttl = 0;
|
|
static unsigned long g_hits, g_misses;
|
|
|
|
static double now_s(void)
|
|
{
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
|
|
}
|
|
|
|
void j_cache_config(size_t max_entries, size_t max_bytes, double ttl)
|
|
{
|
|
g_max_entries = max_entries;
|
|
g_max_bytes = max_bytes;
|
|
g_ttl = ttl;
|
|
}
|
|
|
|
void j_cache_stats(size_t *entries, size_t *bytes, unsigned long *hits, unsigned long *misses)
|
|
{
|
|
LOCK_CACHE();
|
|
if (entries) *entries = g_entries;
|
|
if (bytes) *bytes = g_bytes;
|
|
if (hits) *hits = g_hits;
|
|
if (misses) *misses = g_misses;
|
|
UNLOCK_CACHE();
|
|
}
|
|
|
|
/* FNV-1a, 64 bit — enough to key a local cache; this is not a security boundary. */
|
|
static void fnv(uint64_t *h, const void *p, size_t n)
|
|
{
|
|
const unsigned char *b = p;
|
|
for (size_t i = 0; i < n; i++) {
|
|
*h ^= b[i];
|
|
*h *= 1099511628211ULL;
|
|
}
|
|
}
|
|
|
|
uint64_t j_cache_key(const char *method, const char *path, const char *model,
|
|
const char *body, size_t body_len)
|
|
{
|
|
uint64_t h = 1469598103934665603ULL;
|
|
fnv(&h, method, strlen(method));
|
|
fnv(&h, "\x1f", 1);
|
|
fnv(&h, path, strlen(path));
|
|
fnv(&h, "\x1f", 1);
|
|
fnv(&h, model, strlen(model));
|
|
fnv(&h, "\x1f", 1);
|
|
if (body) fnv(&h, body, body_len);
|
|
return h;
|
|
}
|
|
|
|
static void unlink_entry(entry *e)
|
|
{
|
|
if (e->prev) e->prev->next = e->next; else g_head = e->next;
|
|
if (e->next) e->next->prev = e->prev; else g_tail = e->prev;
|
|
e->prev = e->next = NULL;
|
|
}
|
|
|
|
static void free_entry(entry *e)
|
|
{
|
|
unlink_entry(e);
|
|
g_entries--;
|
|
g_bytes -= e->head_len + e->body_len;
|
|
free(e->head);
|
|
free(e->body);
|
|
free(e);
|
|
}
|
|
|
|
static void touch(entry *e)
|
|
{
|
|
if (e == g_head) return;
|
|
unlink_entry(e);
|
|
e->prev = NULL;
|
|
e->next = g_head;
|
|
if (g_head) g_head->prev = e;
|
|
g_head = e;
|
|
if (!g_tail) g_tail = e;
|
|
}
|
|
|
|
static void expire_old(void)
|
|
{
|
|
double t = now_s();
|
|
entry *e = g_tail;
|
|
while (e) {
|
|
entry *prev = e->prev;
|
|
if (e->ttl > 0 && t - e->stored >= e->ttl) free_entry(e);
|
|
e = prev;
|
|
}
|
|
}
|
|
|
|
int j_cache_get(uint64_t key, const char **head, size_t *head_len,
|
|
const char **body, size_t *body_len, double *age)
|
|
{
|
|
UNLOCK_CACHE(); LOCK_CACHE(); /* no-op on the first call, keeps the shape */
|
|
expire_old();
|
|
for (entry *e = g_head; e; e = e->next) {
|
|
if (e->key != key) continue;
|
|
if (e->ttl > 0 && now_s() - e->stored >= e->ttl) { free_entry(e); break; }
|
|
touch(e);
|
|
g_hits++;
|
|
if (head) *head = e->head;
|
|
if (head_len) *head_len = e->head_len;
|
|
if (body) *body = e->body;
|
|
if (body_len) *body_len = e->body_len;
|
|
if (age) *age = now_s() - e->stored;
|
|
UNLOCK_CACHE();
|
|
return 0;
|
|
}
|
|
g_misses++;
|
|
UNLOCK_CACHE();
|
|
return -1;
|
|
}
|
|
|
|
int j_cache_put(uint64_t key, const char *head, size_t head_len,
|
|
const char *body, size_t body_len, double ttl)
|
|
{
|
|
LOCK_CACHE();
|
|
if (g_max_entries == 0 || g_max_bytes == 0) { UNLOCK_CACHE(); return -1; }
|
|
if (ttl <= 0) ttl = g_ttl;
|
|
if (ttl <= 0) { UNLOCK_CACHE(); return -1; }
|
|
if (head_len + body_len > g_max_bytes) { UNLOCK_CACHE(); return -1; }
|
|
|
|
for (entry *e = g_head; e; e = e->next) {
|
|
if (e->key == key) { free_entry(e); break; }
|
|
}
|
|
while ((g_entries + 1 > g_max_entries) || (g_bytes + head_len + body_len > g_max_bytes)) {
|
|
if (!g_tail) break;
|
|
free_entry(g_tail);
|
|
}
|
|
|
|
entry *e = calloc(1, sizeof *e);
|
|
if (!e) { UNLOCK_CACHE(); return -1; }
|
|
e->head = malloc(head_len ? head_len : 1);
|
|
e->body = malloc(body_len ? body_len : 1);
|
|
if (!e->head || !e->body) {
|
|
free(e->head); free(e->body); free(e);
|
|
UNLOCK_CACHE();
|
|
return -1;
|
|
}
|
|
memcpy(e->head, head, head_len);
|
|
memcpy(e->body, body, body_len);
|
|
e->head_len = head_len;
|
|
e->body_len = body_len;
|
|
e->key = key;
|
|
e->stored = now_s();
|
|
e->ttl = ttl;
|
|
|
|
e->next = g_head;
|
|
if (g_head) g_head->prev = e;
|
|
g_head = e;
|
|
if (!g_tail) g_tail = e;
|
|
g_entries++;
|
|
g_bytes += head_len + body_len;
|
|
UNLOCK_CACHE();
|
|
return 0;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- Single-Flight
|
|
*
|
|
* Zwei Agenten, die gleichzeitig dieselbe Frage stellen, kosten heute zweimal Geld.
|
|
* Hier wartet der zweite auf den ersten und bedient sich danach aus dem Cache.
|
|
*
|
|
* Die Tabelle ist klein und absichtlich begrenzt: sie hält nur Schlüssel, keine
|
|
* Antworten. Ist sie voll, verhält sich jede Anfrage wie bisher — lieber ein Aufruf
|
|
* zu viel als eine Anfrage, die auf einen Platz wartet, den es nicht gibt. */
|
|
#define J_FLIGHT_SLOTS 64
|
|
|
|
static pthread_cond_t g_fl_cond = PTHREAD_COND_INITIALIZER;
|
|
static struct { uint64_t key; int waiters; int done; } g_fl[J_FLIGHT_SLOTS];
|
|
static unsigned long g_joined;
|
|
|
|
static int fl_find(uint64_t key)
|
|
{
|
|
for (int i = 0; i < J_FLIGHT_SLOTS; i++)
|
|
if (g_fl[i].key == key) return i;
|
|
return -1;
|
|
}
|
|
|
|
static int fl_free(void)
|
|
{
|
|
for (int i = 0; i < J_FLIGHT_SLOTS; i++)
|
|
if (g_fl[i].key == 0) return i;
|
|
return -1;
|
|
}
|
|
|
|
int j_cache_flight_begin(uint64_t key)
|
|
{
|
|
if (!key) return 1;
|
|
LOCK_CACHE();
|
|
int i = fl_find(key);
|
|
if (i >= 0) { g_fl[i].waiters++; UNLOCK_CACHE(); return 0; }
|
|
i = fl_free();
|
|
if (i < 0) { UNLOCK_CACHE(); return 1; }
|
|
g_fl[i].key = key;
|
|
g_fl[i].waiters = 0;
|
|
g_fl[i].done = 0;
|
|
UNLOCK_CACHE();
|
|
return 1;
|
|
}
|
|
|
|
int j_cache_flight_wait(uint64_t key, double timeout_s)
|
|
{
|
|
if (!key) return -1;
|
|
struct timespec deadline;
|
|
clock_gettime(CLOCK_REALTIME, &deadline);
|
|
double add = timeout_s > 0 ? timeout_s : 0;
|
|
deadline.tv_sec += (time_t)add;
|
|
deadline.tv_nsec += (long)((add - (double)(time_t)add) * 1e9);
|
|
if (deadline.tv_nsec >= 1000000000L) { deadline.tv_sec++; deadline.tv_nsec -= 1000000000L; }
|
|
|
|
LOCK_CACHE();
|
|
int i = fl_find(key);
|
|
if (i < 0) { UNLOCK_CACHE(); return 0; } /* schon fertig */
|
|
int rc = 0;
|
|
while (!g_fl[i].done) {
|
|
rc = pthread_cond_timedwait(&g_fl_cond, &g_mu, &deadline);
|
|
if (rc != 0) break; /* Zeitablauf */
|
|
}
|
|
int done = g_fl[i].done;
|
|
g_fl[i].waiters--;
|
|
if (g_fl[i].waiters <= 0 && g_fl[i].done) g_fl[i].key = 0;
|
|
if (done) g_joined++;
|
|
UNLOCK_CACHE();
|
|
return (done && rc == 0) ? 0 : -1;
|
|
}
|
|
|
|
void j_cache_flight_end(uint64_t key)
|
|
{
|
|
if (!key) return;
|
|
LOCK_CACHE();
|
|
int i = fl_find(key);
|
|
if (i >= 0) {
|
|
g_fl[i].done = 1;
|
|
pthread_cond_broadcast(&g_fl_cond);
|
|
if (g_fl[i].waiters <= 0) g_fl[i].key = 0;
|
|
}
|
|
UNLOCK_CACHE();
|
|
}
|
|
|
|
unsigned long j_cache_flight_joined(void)
|
|
{
|
|
LOCK_CACHE();
|
|
unsigned long v = g_joined;
|
|
UNLOCK_CACHE();
|
|
return v;
|
|
}
|
|
|
|
void j_cache_clear(void)
|
|
{
|
|
LOCK_CACHE();
|
|
while (g_head) free_entry(g_head);
|
|
g_head = g_tail = NULL;
|
|
g_entries = g_bytes = 0;
|
|
UNLOCK_CACHE();
|
|
}
|