# vanh01/caching-strategies architecture

> Architecture diagram of the GitHub repository vanh01/caching-strategies, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vanh01/caching-strategies
- Repository: https://github.com/vanh01/caching-strategies

## Overview

##### Main Components of the System
a. Entry Point & Application Bootstrap:
- The cmd directory (specifically cmd/main.go) represents the application’s starting point.
- Dockerfile and docker-compose.yml indicate containerized deployment.
b. Configuration & Initialization:
- config.yaml and the files inside the config folder (e.g., config/config.go) are used to configure the system.
- init.sql suggests there is a SQL component for database initialization.
c. Core Layers (Following a Layered Architecture):
- internal/app: Contains the main application logic (internal/app/app.go) for bootstrapping.
- internal/controller: The routing and API request handling layer (internal/controller/router.go).
- internal/usecase: Contains business logic (internal/usecase/user.go and interfaces.go) which likely coordinates between controllers and repositories.
- internal/repo: Handles data persistence and retrieval, probably interacting with the database (internal/repo/user.go, constant.go).
- internal/model: Holds data models (internal/model/user.go) used across the system.
d. Caching Strategies:
- internal/cus_cache: Implements at least one caching strategy (see read_through.go).
- pkg/cache: Provides caching functionalities using Redis (pkg/cache/redis.go) which acts as a common cache module.
- The test and report folders include scripts and outputs associated with different caching strategies (original, cache-aside, and read-through).
e. Testing & Reporting:
- The test directory contains JavaScript files to perform load tests for the caching strategies.
- The report folder saves output logs or performance assessments for each strategy.

##### Relationships & Data Flow
- The entry point (cmd/main.go) “boots” the app by loading the configuration (from config.yaml via config/ folder) and initializing the application layer.
- Requests are routed by the controller (internal/controller/router.go) which triggers business logic in the usecase layer.
- The usecase layer interacts with the repository to fetch or update data from the underlying database (set up by init.sql) as well as with the caching implementations.
- Caching is handled in two flavors:
- Custom caching strategies (internal/cus_cache) that likely demonstrate specific caching techniques.
- A Redis cache implementation in pkg/cache as a utility.
- Testing scripts simulate client requests against these strategies, and results are written into the report folder, exhibiting performance differences.

##### Architectural Patterns & Design Principles
- Layered Architecture: Clear separation between presentation (controller), business logic (usecase), and data persistence (repo).
- Modular Design: Different caching strategies are isolated in their respective directories (both custom implementations and external integrations like Redis).
- Containerized Deployment: Use of Docker and related configuration files for running the application in isolated environments.
- Separation of concerns: Configurations, business logic, data models, and caching strategies are each in their own directories.

##### A Detailed and Granular Approach
- Every folder or file grouping that provides unique functionality is represented.
- Dependencies (e.g., the relationship between the business logic and both caching implementations).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Container Deployment"
        E["Entry: cmd/main.go"]:::entry
        Docker["Dockerfile"]:::docker
        DC["docker-compose.yml"]:::docker
    end

    subgraph "Configuration"
        ConfigYAML["Config File: config.yaml"]:::config
        ConfigGo["Config Loader: config/config.go"]:::config
        InitSQL["DB Init: init.sql"]:::config
    end

    subgraph "Application Layers"
        App["Application: internal/app/app.go"]:::app
        Controller["API Gateway: internal/controller/router.go"]:::controller
        Usecase["Business Logic: internal/usecase (interfaces.go & user.go)"]:::usecase
        Model["Data Models: internal/model/user.go"]:::model
        Repo["Repository: internal/repo (user.go & constant.go)"]:::repo
        CustomCache["Custom Cache: internal/cus_cache/read_through.go"]:::cache
        RedisCache["Redis Cache: pkg/cache/redis.go"]:::cache
    end

    subgraph "Testing & Reporting"
        TestCacheAside["Test: test/cache-aside.js"]:::test
        TestOriginal["Test: test/original.js"]:::test
        TestReadThrough["Test: test/read-through.js"]:::test
        ReportCacheAside["Report: report/cache-aside.txt"]:::report
        ReportOriginal["Report: report/original.txt"]:::report
        ReportReadThrough["Report: report/read-through.txt"]:::report
    end

    %% Container Deployment Connections
    Docker --> E
    DC --> E

    %% Entry Point loads configuration
    E -->|"loads"| ConfigYAML
    E -->|"loads"| ConfigGo
    E -->|"triggers"| InitSQL

    %% Configuration provides settings to Application
    ConfigYAML -->|"configures"| App
    ConfigGo -->|"configures"| App
    InitSQL -->|"initializes"| App

    %% Application Flow
    App -->|"routes to"| Controller
    Controller -->|"calls"| Usecase
    Usecase -->|"fetches/updates"| Repo
    Usecase -->|"uses"| Model
    Usecase -->|"leverages"| CustomCache
    Usecase -->|"leverages"| RedisCache

    %% Testing & Reporting Flow
    TestCacheAside -->|"simulates"| Controller
    TestOriginal  -->|"simulates"| Controller
    TestReadThrough -->|"simulates"| Controller

    TestCacheAside -->|"outputs"| ReportCacheAside
    TestOriginal  -->|"outputs"| ReportOriginal
    TestReadThrough -->|"outputs"| ReportReadThrough

    %% Click Events
    click E "https://github.com/vanh01/caching-strategies/blob/master/cmd/main.go"
    click Docker "https://github.com/vanh01/caching-strategies/tree/master/Dockerfile"
    click DC "https://github.com/vanh01/caching-strategies/blob/master/docker-compose.yml"
    click ConfigYAML "https://github.com/vanh01/caching-strategies/blob/master/config.yaml"
    click ConfigGo "https://github.com/vanh01/caching-strategies/blob/master/config/config.go"
    click InitSQL "https://github.com/vanh01/caching-strategies/blob/master/init.sql"
    click App "https://github.com/vanh01/caching-strategies/blob/master/internal/app/app.go"
    click Controller "https://github.com/vanh01/caching-strategies/blob/master/internal/controller/router.go"
    click Usecase "https://github.com/vanh01/caching-strategies/blob/master/internal/usecase/interfaces.go"
    click Repo "https://github.com/vanh01/caching-strategies/blob/master/internal/repo/user.go"
    click Model "https://github.com/vanh01/caching-strategies/blob/master/internal/model/user.go"
    click CustomCache "https://github.com/vanh01/caching-strategies/blob/master/internal/cus_cache/read_through.go"
    click RedisCache "https://github.com/vanh01/caching-strategies/blob/master/pkg/cache/redis.go"
    click TestCacheAside "https://github.com/vanh01/caching-strategies/blob/master/test/cache-aside.js"
    click TestOriginal "https://github.com/vanh01/caching-strategies/blob/master/test/original.js"
    click TestReadThrough "https://github.com/vanh01/caching-strategies/blob/master/test/read-through.js"
    click ReportCacheAside "https://github.com/vanh01/caching-strategies/blob/master/report/cache-aside.txt"
    click ReportOriginal "https://github.com/vanh01/caching-strategies/blob/master/report/original.txt"
    click ReportReadThrough "https://github.com/vanh01/caching-strategies/blob/master/report/read-through.txt"

    %% Styles
    classDef entry fill:#f9c,stroke:#333,stroke-width:2px;
    classDef docker fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef config fill:#ccf,stroke:#333,stroke-width:2px;
    classDef app fill:#f96,stroke:#333,stroke-width:2px;
    classDef controller fill:#9f9,stroke:#333,stroke-width:2px;
    classDef usecase fill:#6cf,stroke:#333,stroke-width:2px;
    classDef repo fill:#c9f,stroke:#333,stroke-width:2px;
    classDef model fill:#fc9,stroke:#333,stroke-width:2px;
    classDef cache fill:#f66,stroke:#333,stroke-width:2px;
    classDef test fill:#9cf,stroke:#333,stroke-width:2px;
    classDef report fill:#ccc,stroke:#333,stroke-width:2px;
```
