update architecture cache section to reflect recent changes
All checks were successful
Generic zig build / build (push) Successful in 1m21s
Generic zig build / deploy (push) Successful in 14s

This commit is contained in:
Emil Lerch 2026-01-09 13:54:32 -08:00
parent 6a6101c495
commit ea88a88e31
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -119,28 +119,26 @@ pub const RateLimitConfig = struct {
### Cache
**Single-layer cache with two storage backends:**
**Single layer, two-tier cache system (L1 memory + L2 file):**
**Interface:**
```zig
pub const Cache = struct {
lru: LRU,
file_store: FileStore,
lru: Lru, // L1: In-memory cache
cache_dir: ?[]const u8, // L2: Optional file-backed storage
pub fn get(self: *Cache, key: []const u8) ?[]const u8;
pub fn put(self: *Cache, key: []const u8, value: []const u8, ttl: u64) !void;
};
```
**Cache Key:**
```
{user_agent}:{path}:{query}:{client_ip}
```
**Storage Strategy:**
- Small responses (<1KB): In-memory LRU only
- Large responses (≥1KB): LRU stores file path, data on disk
- TTL: 1000-2000s (randomized to prevent thundering herd)
- **L1 (Memory)**: LRU cache with configurable max entries
- **L2 (Disk)**: Optional file-backed storage for persistence
- Files stored as JSON with key, value, and expiration timestamp
- On eviction from L1, data remains in L2
- On cache miss in L1, checks L2 and promotes to L1 if found
- **TTL**: 1000-2000s (randomized to prevent thundering herd)
**Cache Locations:**
@ -148,23 +146,32 @@ All caches default to `$XDG_CACHE_HOME/wttr` (typically `~/.cache/wttr`).
1. **Weather Response Cache**
- Location: `$WTTR_CACHE_DIR` (default: `~/.cache/wttr/`)
- Format: JSON (one file for each entry/location)
- Size: 10,000 entries (configurable via `WTTR_CACHE_SIZE`)
- Expiration: 1000-2000 seconds (randomized)
- Eviction: LRU
- Implementation: `src/cache/Cache.zig`, `src/cache/Lru.zig`
2. **Geocoding Cache**
- Purpose: Caches results from nominatim API to minimize API calls
- Location: `$WTTR_GEOCACHE_FILE` (default: `~/.cache/wttr/geocache.json`)
- Format: JSON
- Format: JSON (single file with all entries)
- Expiration: None (persists indefinitely)
- Implementation: `src/location/GeoCache.zig`
3. **IP2Location Cache**
- Purpose: Caches results from IP2Location.io API to minimize API calls
- Location: `$IP2LOCATION_CACHE_FILE` (default: `~/.cache/wttr/ip2location.cache`)
- Format: Binary (32-byte records)
- Format: Text file with header `#Ip2location:v2` followed by CSV lines: `ip,lat,lon,name`
Note that name stored at the end so any bytes are included, including commas
- Expiration: None (persists indefinitely)
- Storage: In-memory hash map (u128 IP → Location) + append-only file
- Implementation: `src/location/Ip2location.zig` (internal Cache struct)
4. **GeoIP Database**
- Location: `$WTTR_GEOLITE_PATH` (default: `~/.cache/wttr/GeoLite2-City.mmdb`)
- Auto-downloaded if missing
- Implementation: `src/location/GeoLite2.zig`
### Location Resolver (`src/location/resolver.zig`)