# roh355/nanocache architecture

> Architecture diagram of the GitHub repository roh355/nanocache, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/roh355/nanocache
- Repository: https://github.com/roh355/nanocache

## Overview

This repository implements a small RESP-speaking in-memory key/value cache server. A client sends a command over TCP; the server parses it, dispatches it to command handlers, and returns an encoded response. The cache supports expiration, while snapshots restore state at startup and save it during shutdown. The sampled server and command sources show these calls directly; no README is available, so the graph reflects the capabilities visible in the source rather than undocumented features.

Main entry points: [`server.cpp`](https://github.com/roh355/nanocache/blob/main/server.cpp) (Server loop), [`commands.cpp`](https://github.com/roh355/nanocache/blob/main/commands.cpp) (Command dispatcher), [`snapshot.cpp`](https://github.com/roh355/nanocache/blob/main/snapshot.cpp) (Snapshot persistence), [`resp_parser.cpp`](https://github.com/roh355/nanocache/blob/main/resp_parser.cpp) (RESP parser and encoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_network["TCP server"]
  node_server["Server loop<br/>[server.cpp]"]
end

subgraph group_protocol["Command protocol"]
  node_parser["RESP parser and encoder<br/>[resp_parser.cpp]"]
  node_commands["Command dispatcher<br/>[commands.cpp]"]
end

subgraph group_storage["Cache storage"]
  node_cache["TTL cache<br/>[cache.cpp]"]
  node_snapshots["Snapshot persistence<br/>[snapshot.cpp]"]
end

node_client(("TCP client"))
node_snapshot_file[("Snapshot file")]

node_client -->|"sends commands"| node_server
node_server -->|"sends responses"| node_client
node_server -->|"parses input"| node_parser
node_server -->|"dispatches commands"| node_commands
node_commands -->|"reads and writes"| node_cache
node_commands -->|"encodes replies"| node_parser
node_server -->|"loads and saves"| node_snapshots
node_snapshots -->|"reads and writes"| node_snapshot_file

click node_server "https://github.com/roh355/nanocache/blob/main/server.cpp"
click node_parser "https://github.com/roh355/nanocache/blob/main/resp_parser.cpp"
click node_commands "https://github.com/roh355/nanocache/blob/main/commands.cpp"
click node_cache "https://github.com/roh355/nanocache/blob/main/cache.cpp"
click node_snapshots "https://github.com/roh355/nanocache/blob/main/snapshot.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server,node_client toneBlue
class node_parser,node_commands,node_snapshot_file toneAmber
class node_cache,node_snapshots toneMint
```

## Components

### TCP server

- [Server loop](https://github.com/roh355/nanocache/blob/main/server.cpp): `server.cpp`

### Command protocol

- [RESP parser and encoder](https://github.com/roh355/nanocache/blob/main/resp_parser.cpp): `resp_parser.cpp`
- [Command dispatcher](https://github.com/roh355/nanocache/blob/main/commands.cpp): `commands.cpp`

### Cache storage

- [TTL cache](https://github.com/roh355/nanocache/blob/main/cache.cpp): `cache.cpp`
- [Snapshot persistence](https://github.com/roh355/nanocache/blob/main/snapshot.cpp): `snapshot.cpp`

### Other components

- **TCP client**
- **Snapshot file**

## Connections

- TCP client → Server loop: sends commands (evidence: `server.cpp`)
- Server loop → TCP client: sends responses (evidence: `server.cpp`)
- Server loop → RESP parser and encoder: parses input (evidence: `server.cpp`)
- Server loop → Command dispatcher: dispatches commands (evidence: `server.cpp`)
- Command dispatcher → TTL cache: reads and writes (evidence: `commands.cpp`)
- Command dispatcher → RESP parser and encoder: encodes replies (evidence: `commands.cpp`)
- Server loop → Snapshot persistence: loads and saves (evidence: `server.cpp`)
- Snapshot persistence → Snapshot file: reads and writes (evidence: `snapshot.cpp`)
