How sai007nikhil/smart-cache works
Project Type and Purpose
• SmartCache is a standalone caching service (a backend microservice) written in Go.
• Purpose: Provide high‐performance in-memory caching with TTL, hybrid eviction (LRU+LFU), optional disk persistence, and a RESTful API.
File Structure
• Top‐level modules:
• api/ → HTTP endpoint handlers
• cache/ → Core in-memory key-value store
• eviction/ → Eviction strategies (LRU, LFU, Hybrid)
• persistence/→ Disk persistence backends (BoltDB, BadgerDB, WAL)
• config/ → Configuration loader (YAML/JSON/env)
• k8s/, Dockerfile, docker-compose.yml → Deployment artifacts
• main.go ties everything together: loads config, initializes modules, starts HTTP server.
• Test files and scripts demonstrate high concurrency, benchmarks, and integration tests.
Extract Architectural Insights from the README
• Modular architecture: Cache Core, Eviction Engine, Persistence Layer, API Layer, Configuration.
• Key design goals: thread safety (fine-grained locking), pluggable eviction, optional persistence, TTL GC.
• Production readiness: Docker, K8s, configurable via YAML or env vars.
Main Components and Their Relationships
a. API Layer
• Exposes REST endpoints (/api/v1/cache, /stats)
• Forwards requests to Cache Core
b. Cache Core
• In-memory key-value store
• Handles TTL expiration by background GC
• Delegates eviction decisions to Eviction Engine
• Invokes Persistence Layer for writes/reads if enabled
c. Eviction Engine (pluggable)
• Implements LRU, LFU, or Hybrid strategies
• Configurable weights (hybrid_alpha, hybrid_beta)
• API: Evictor.Evict()
d. Persistence Layer (optional)
• Two backends: BoltDB or BadgerDB
• Uses WAL (Write-Ahead Logging) for durability
• Snapshotting at intervals
e. Configuration Manager
• Reads YAML/JSON or environment variables
• Supplies parameters to all modules: server mode/port, cache size, TTL, eviction policy, persistence options
f. Infrastructure Modules
• Dockerfile & docker-compose.yml for containerization
• k8s/ for Kubernetes deployment, service, PVC, configmap
Architectural Patterns and Principles
• Modular monolith: clear separation of concerns (API vs. business logic vs. persistence)
• Strategy pattern: eviction methods pluggable at runtime
• Dependency injection: modules wired together in main.go based on config
• Concurrency control: fine-grained locks within Cache Core, safe for 100K+ ops/sec
• Observer/ticker for TTL GC