# projectdiscovery/gcache architecture

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

- Interactive diagram: https://gitdiagram.com/projectdiscovery/gcache
- Repository: https://github.com/projectdiscovery/gcache

## Overview

##### Project type and purpose
- This is a Go‐based, in‐process cache library (gcache) providing multiple eviction strategies (Simple, LRU, LFU, ARC), optional expiration, loader functions, and event hooks.  It is intended to be embedded in other Go applications to provide thread‐safe, in‐memory caching.

##### File structure
- Top‐level “.go” files (arc.go, lru.go, lfu.go, simple.go) each implement a CacheStrategy.
- cache.go defines the Cache interface and the core cache manager / builder.
- singleflight.go implements a request‐coalescing loader.
- stats.go, clock.go, utils.go support expiration, statistics, and misc helpers.
- internal/list implements a doubly‐linked list for LRU/ARC.
- tests validate each component; examples/ shows usage.

##### README for additional insights
- Describes how to choose and build caches with different strategies, expiration, loader functions, and event handlers.
- Emphasizes concurrency safety, loader guarantees, and event hooks.

a. Main components
- Client Code
- Cache.Builder & Cache Interface
- Core Cache Manager (cache.go)
- Eviction Strategy Modules (simple, LRU, LFU, ARC)
- Storage Layer (map + internal/list for ordering)
- Singleflight Loader (singleflight.go)
- Expiration & Clock (clock.go)
- Event Handlers (Evicted, Added, Purge hooks)
- Utility & Stats (stats.go, utils.go)
b. Relationships & Interactions
- Client calls Builder → selects Strategy → Build() → returns a Cache instance.
- Client invokes Cache.Set/Get methods on Cache instance.
- Cache.Get:
- Checks map; if miss and loader configured, delegates to singleflight to call user LoaderFunc → writes to storage.
- Checks expiration via Clock.
- Updates ordering/list in strategy.
- Fires event handlers.
- Cache.Set:
- Inserts into map + list.
- If capacity exceeded, calls chosen strategy’s evict method → removes entries → triggers EvictedFunc.
- Strategy modules depend on internal/list and utils.
- Stats module observes hits, misses, evictions.
c. Architectural patterns & design principles
- Strategy Pattern: simple, LRU, LFU, ARC algorithms interchangeable.
- Builder Pattern: chaining configuration before instantiation.
- Singleflight: request coalescing pattern to avoid thundering‐herd on loader.
- Event callback hooks for extensibility.
- Separation of concerns: core cache manager vs eviction logic vs storage vs utilities.
d. Key technologies & libraries
- Pure Go (no external dependencies).
- internal/list for doubly linked list.
- sync.RWMutex for concurrency safety.
- time package for expiration.
- internal singleflight logic (modeled after golang.org/x/sync/singleflight).

##### Essential elements
- Event handlers (EvictedFunc, AddedFunc, PurgeVisitorFunc)

Summary
- How the Strategy pattern and Builder pattern work together, how singleflight coalesces loads, and how hooks extend behavior.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Nodes
    ClientApp["Client Application"]:::external
    BuilderInterface["gcache.Builder & Cache Interface"]:::core
    CoreManager["Core Cache Manager"]:::core

    subgraph "Eviction Strategies"
        direction TB
        Simple["Simple Strategy"]:::strategy
        LRU["LRU Strategy"]:::strategy
        LFU["LFU Strategy"]:::strategy
        ARC["ARC Strategy"]:::strategy
    end

    subgraph "Storage Layer"
        direction TB
        MapStorage["map[interface{}]interface{}"]:::util
        ListStorage["internal/list (doubly-linked list)"]:::util
    end

    Loader["Singleflight Loader"]:::util
    Expiration["Expiration & Clock"]:::util
    Stats["Statistics Collector"]:::util
    Utils["Utility Helpers"]:::util
    EventHandlers["Event Handlers (Evicted, Added, Purge)"]:::external

    %% Connections
    ClientApp -->|"uses Builder"| BuilderInterface
    BuilderInterface -->|"Build() returns Cache"| CoreManager

    CoreManager -->|"Get()/Set()"| MapStorage
    CoreManager -->|"update order"| ListStorage
    CoreManager -->|"evict()"| Simple
    CoreManager -->|"evict()"| LRU
    CoreManager -->|"evict()"| LFU
    CoreManager -->|"evict()"| ARC
    CoreManager -->|"load() on miss"| Loader
    Loader -->|"calls LoaderFunc"| CoreManager
    CoreManager -->|"expireCheck()"| Expiration
    CoreManager -->|"record hit/miss"| Stats
    CoreManager -->|"trigger hooks"| EventHandlers

    Simple -->|"uses helpers"| Utils
    LRU -->|"uses list & helpers"| ListStorage
    LRU -->|"uses helpers"| Utils
    LFU -->|"uses helpers"| Utils
    ARC -->|"uses list & helpers"| ListStorage
    ARC -->|"uses helpers"| Utils

    %% Click Events
    click ClientApp "https://github.com/projectdiscovery/gcache/blob/main/examples/autoloading/autoloading_cache.go"
    click BuilderInterface "https://github.com/projectdiscovery/gcache/blob/main/cache.go"
    click CoreManager "https://github.com/projectdiscovery/gcache/blob/main/cache.go"
    click Simple "https://github.com/projectdiscovery/gcache/blob/main/simple.go"
    click LRU "https://github.com/projectdiscovery/gcache/blob/main/lru.go"
    click LFU "https://github.com/projectdiscovery/gcache/blob/main/lfu.go"
    click ARC "https://github.com/projectdiscovery/gcache/blob/main/arc.go"
    click ListStorage "https://github.com/projectdiscovery/gcache/blob/main/internal/list/list.go"
    click Loader "https://github.com/projectdiscovery/gcache/blob/main/singleflight.go"
    click Expiration "https://github.com/projectdiscovery/gcache/blob/main/clock.go"
    click EventHandlers "https://github.com/projectdiscovery/gcache/blob/main/cache.go"
    click Stats "https://github.com/projectdiscovery/gcache/blob/main/stats.go"
    click Utils "https://github.com/projectdiscovery/gcache/blob/main/utils.go"

    %% Styles
    classDef core fill:#e0f7fa,stroke:#006064,color:#004d40;
    classDef strategy fill:#e8f5e9,stroke:#1b5e20,color:#2e7d32;
    classDef util fill:#fff3e0,stroke:#e65100,color:#bf360c;
    classDef external fill:#eceff1,stroke:#455a64,color:#263238;
```
