# omid/kash architecture

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

- Interactive diagram: https://gitdiagram.com/omid/kash
- Repository: https://github.com/omid/kash

## Overview

##### Project Type & Purpose
- This is a Rust library (not a full-stack application) with a focus on caching and memoizing function calls.
- The README highlights that it uses macros to intercept function calls, check for cached results, and otherwise call the original function.
- It supports various caching strategies (in-memory, disk-based, Redis-based) and includes examples for different use cases, including async scenarios.

##### Components from the File Structure
- Top-Level Files & Directories:
- Cargo.toml, Makefile, and README.md indicate this is a library project.
- The examples directory shows usage patterns in different contexts.
- The src directory (with lib.rs and stores submodules) provides core library functionality and implementations of different cache stores.
- The kash_macros directory contains the macro definition and helper routines. This area is key since it generates the caching code at compile-time.
- The tests directory has integration/functional tests for verifying the functionality with different caching backends.
- Architectural Pattern Indicator:
- The project is modular. The core caching logic is separated from the macros and store implementations.
- The “stores” in src and their corresponding modules (i.e., disk.rs, redis.rs) show that the design follows the strategy pattern for caching (you may select among different cache store strategies).

##### Key Elements to Capture
a. Main Components:
- User-defined functions decorated with #[kash]: Illustrate how application code benefits from function memoization.
- Kash Macro Processor (kash_macros): Show how the macro transforms a function into one that wraps caching logic. Highlight sub-components for “common”, “io” (with disk and redis), and “mem”.
- Core Library (src/lib.rs): Acts as the integration layer which ties together the generated code from macros with the underlying caching stores.
- Cache Store Implementations (src/stores): Include specific modules for disk and redis caching.
- External Dependencies: Illustrate the use of external crates such as ahash for hashing, redis libraries, and possibly tokio for asynchronous operations.

b. Relationships and Interactions:
- When a user calls a decorated function, the expanded macro logic first checks the corresponding cache store (whether in-memory, disk, or redis) before invoking the function.
- The macro (in kash_macros) encapsulates logic for cache key generation, validation (using TTLs or size limits), and error handling with Rust’s Result types.
- For async functions, interactions with the async runtime (tokio) and connection management for redis are also depicted.
- How function calls flow from user code to the macro-enhanced function.
- The caching decision process (cache hit versus cache miss).
- Integration between the core library and the specific caching store implementations.

c. Architectural Patterns or Principles:
- Modularity: The separation between macro generation and store implementations.
- Strategy Pattern: Different caching strategies (disk, redis, memory) chosen based on configuration and feature flags.
- Decorator Pattern: The #[kash] macro “decorates” functions to transparently add caching behavior.

d. Relevant Technologies & Frameworks:
- Rust language constructs (using Cargo, modules, macros).
- Redis and the specific async connection managers for handling async caching.
- Disk caching possibly using file system operations under a configurable directory.
- Hashing through the ahash crate.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Code Layer
    subgraph "User Code Layer"
        UC["User Code (#[kash])"]:::user
    end

    %% Compile-Time (Macro) Components
    subgraph "Compile-Time Components"
        KM["Kash Macro Processor"]:::macro
        subgraph "Macro Modules"
            KMC["Common Routines"]:::macro
            KMM["In-Memory Support"]:::macro
            KMD["Disk Support"]:::macro
            KMR["Redis Support"]:::macro
        end
    end

    %% Runtime (Caching Infrastructure)
    subgraph "Runtime Components"
        CL["Core Library (src/lib.rs)"]:::runtime
        subgraph "Cache Stores"
            CI["In-Memory Cache (src/stores/mem)"]:::runtime
            CD["Disk Cache (src/stores/disk)"]:::runtime
            CR["Redis Cache (src/stores/redis)"]:::runtime
        end
    end

    %% External Dependencies
    subgraph "External Dependencies"
        EX["ahash, redis, tokio"]:::external
    end

    %% Connections
    UC -->|"transformedBy"| KM
    KM -->|"generatesCodeFor"| CL
    CL -->|"cacheCheck_Store"| CI
    CL -->|"cacheCheck_Store"| CD
    CL -->|"cacheCheck_Store"| CR

    %% External dependency usage
    KM ---|"uses"| EX
    CL ---|"uses"| EX

    %% Cache hit feedback loops
    CI -.->|"cacheHit"| CL
    CD -.->|"cacheHit"| CL
    CR -.->|"cacheHit"| CL

    %% Macro Modules connections
    KM --> KMC
    KM --> KMM
    KM --> KMD
    KM --> KMR

    %% Click Events
    click UC "https://github.com/omid/kash/tree/master/examples"
    click KM "https://github.com/omid/kash/tree/master/kash_macros/"
    click KMC "https://github.com/omid/kash/tree/master/kash_macros/src/common/"
    click KMM "https://github.com/omid/kash/tree/master/kash_macros/src/mem/"
    click KMD "https://github.com/omid/kash/tree/master/kash_macros/src/io/disk/"
    click KMR "https://github.com/omid/kash/tree/master/kash_macros/src/io/redis/"
    click CL "https://github.com/omid/kash/blob/master/src/lib.rs"
    click CD "https://github.com/omid/kash/blob/master/src/stores/disk.rs"
    click CR "https://github.com/omid/kash/blob/master/src/stores/redis.rs"

    %% Styles
    classDef user fill:#CDEBFF,stroke:#333,stroke-width:2px;
    classDef macro fill:#F4E1D2,stroke:#D17D28,stroke-width:2px;
    classDef runtime fill:#E2F7D4,stroke:#38761D,stroke-width:2px;
    classDef external fill:#F9E2E2,stroke:#B91C1C,stroke-width:2px;
```
