# oschina/j2cache architecture

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

- Interactive diagram: https://gitdiagram.com/oschina/j2cache
- Repository: https://github.com/oschina/j2cache

## Overview

##### Main Components Identification

The system should be represented with these key components:

a. Level 1 Cache (L1):
- Caffeine (default)
- Ehcache 2.x (alternative)
- Ehcache 3.x (alternative)

b. Level 2 Cache (L2):
- Redis (primary)
- Memcached (alternative)

c. Broadcast Mechanisms:
- Redis Pub/Sub
- JGroups
- RabbitMQ
- RocketMQ

##### Architecture Layers

a. API Layer:
- CacheChannel interface
- J2CacheBuilder
- Configuration management

b. Core Layer:
- Cache providers
- Serialization utilities
- Cluster policies

c. Integration Layer:
- Spring Boot starters
- Hibernate modules
- MyBatis support
- Session management

##### Design Elements

Use these visual elements:

a. Components:
- Rectangles for cache providers
- Hexagons for message brokers

b. Connections:
- Double lines for data synchronization

- Blue: L1 cache components
- Green: L2 cache components
- Orange: Broadcasting mechanisms
- Gray: Integration modules

##### Key Workflows

Primary flows:

a. Read Path:
##### Populate both cache levels

b. Write Path:
##### Broadcast invalidation message

c. Cache Synchronization:

##### Integration Points

Integration with:
- Spring Framework
- Hibernate (3.x, 4.x, 5.x)
- MyBatis
- Web Session Management

##### Configuration Components

Configuration sources:
- j2cache.properties
- caffeine.properties
- ehcache.xml/ehcache3.xml
- network.xml

##### Deployment View

Multiple application nodes:
- Application instances with L1 cache
- Shared L2 cache (Redis/Memcached)
- Message broker for synchronization
- Database backend

##### Configuration management

- Cache levels
- Data flow directions
- Synchronization mechanisms
- Integration points
- Configuration elements

Keep the abstraction level high enough to show the overall architecture while including sufficient detail to understand the caching strategy and synchronization mechanisms.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph API
        CC[CacheChannel Interface]:::api
        JB[J2Cache Builder]:::api
        CM[Configuration Management]:::api
    end

    subgraph "Level 1 Cache"
        L1[L1 Cache Controller]:::l1
        CAF[Caffeine]:::l1
        EH2[Ehcache 2.x]:::l1
        EH3[Ehcache 3.x]:::l1
    end

    subgraph "Level 2 Cache"
        L2[L2 Cache Controller]:::l2
        RD[(Redis)]:::l2
        MC[(Memcached)]:::l2
    end

    subgraph "Broadcast Mechanisms"
        RPS{Redis Pub/Sub}:::broadcast
        JG{JGroups}:::broadcast
        RMQ{RabbitMQ}:::broadcast
        RKM{RocketMQ}:::broadcast
    end

    subgraph "Integration Layer"
        SB[Spring Boot]:::integration
        HB3[Hibernate 3]:::integration
        HB4[Hibernate 4]:::integration
        HB5[Hibernate 5]:::integration
        MB[MyBatis]:::integration
        SM[Session Manager]:::integration
    end

    subgraph "Configuration"
        J2P[j2cache.properties]:::config
        CP[caffeine.properties]:::config
        EC[ehcache.xml]:::config
        E3C[ehcache3.xml]:::config
        NC[network.xml]:::config
    end

    %% Connections
    CC --> L1
    CC --> L2
    JB --> CC
    CM --> J2P
    
    L1 --> CAF
    L1 --> EH2
    L1 --> EH3
    
    L2 --> RD
    L2 --> MC
    
    L2 -.-> RPS
    L2 -.-> JG
    L2 -.-> RMQ
    L2 -.-> RKM
    
    RPS -.-> L1
    JG -.-> L1
    RMQ -.-> L1
    RKM -.-> L1

    CC --> SB
    CC --> HB3
    CC --> HB4
    CC --> HB5
    CC --> MB
    CC --> SM

    %% Styles
    classDef api fill:#f9f,stroke:#333,stroke-width:2px
    classDef l1 fill:#b9e0ff,stroke:#333,stroke-width:2px
    classDef l2 fill:#98ff98,stroke:#333,stroke-width:2px
    classDef broadcast fill:#ffa07a,stroke:#333,stroke-width:2px
    classDef integration fill:#dcdcdc,stroke:#333,stroke-width:2px
    classDef config fill:#fffacd,stroke:#333,stroke-width:2px

    %% Click Events
    click CAF "https://github.com/oschina/J2Cache/tree/master/core/src/net/oschina/j2cache/caffeine/"
    click EH2 "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/ehcache/EhCache.java"
    click EH3 "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/ehcache/EhCache3.java"
    click RD "https://github.com/oschina/J2Cache/tree/master/core/src/net/oschina/j2cache/redis/"
    click MC "https://github.com/oschina/J2Cache/tree/master/core/src/net/oschina/j2cache/memcached/"
    click RPS "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/redis/RedisPubSubClusterPolicy.java"
    click JG "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/cluster/JGroupsClusterPolicy.java"
    click RMQ "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/cluster/RabbitMQClusterPolicy.java"
    click RKM "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/cluster/RocketMQClusterPolicy.java"
    click CC "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/CacheChannel.java"
    click JB "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/J2CacheBuilder.java"
    click CM "https://github.com/oschina/J2Cache/blob/master/core/src/net/oschina/j2cache/J2CacheConfig.java"
    click SB "https://github.com/oschina/J2Cache/tree/master/modules/spring-boot-starter/"
    click HB3 "https://github.com/oschina/J2Cache/tree/master/modules/hibernate3/"
    click HB4 "https://github.com/oschina/J2Cache/tree/master/modules/hibernate4/"
    click HB5 "https://github.com/oschina/J2Cache/tree/master/modules/hibernate5/"
    click MB "https://github.com/oschina/J2Cache/tree/master/modules/mybatis/"
    click SM "https://github.com/oschina/J2Cache/tree/master/modules/session-manager/"
    click J2P "https://github.com/oschina/J2Cache/blob/master/core/resources/j2cache.properties"
    click CP "https://github.com/oschina/J2Cache/blob/master/core/resources/caffeine.properties"
    click EC "https://github.com/oschina/J2Cache/blob/master/core/resources/ehcache.xml"
    click E3C "https://github.com/oschina/J2Cache/blob/master/core/resources/ehcache3.xml"
    click NC "https://github.com/oschina/J2Cache/blob/master/core/resources/network.xml"
```
