Skip to content

Module Route Cache Overview ​

Module-route cache stores admin panel query and transformation results in Redis (by default), keyed by module name, route name, cache type, and a hash of request parameters. Writes flow through repositories and controllers; invalidation is driven by Eloquent observers and a cross-module relationship graph.

Architecture ​

mermaid
flowchart TB
    subgraph Admin["Admin panel request"]
        Ctrl["BaseController / PanelController"]
        Repo["Repository + CacheableTrait"]
    end

    subgraph CacheLayer["Cache layer"]
        Svc["ModularousCacheService"]
        Redis[("Redis / Memcached")]
        Graph["RelationshipGraph"]
    end

    subgraph Invalidation["Invalidation"]
        Obs["CacheObserver"]
        Tags["Relation tags rel:Model:id"]
        Deps["config cache.dependencies"]
    end

    Ctrl -->|"formItem, formattedItem"| Svc
    Repo -->|"counts, index, record"| Svc
    Svc --> Redis
    Obs --> Svc
    Obs --> Graph
    Obs --> Deps
    Obs --> Tags
    Graph --> Obs

Request Flow ​

mermaid
sequenceDiagram
    participant C as Controller
    participant R as Repository
    participant M as ModularousCache
    participant DB as Database

    C->>R: getPaginator() / getByIdCached()
    R->>M: shouldUseCache(type)?
    alt cache enabled + hit
        M-->>R: cached payload
        R-->>C: result
    else miss
        R->>DB: query
        DB-->>R: models
        R->>M: remember / putWithRelations
        R-->>C: result
    end

    Note over C,M: formattedItem / formItem skip repository;<br/>controller calls rememberCache directly

Cache Key Format ​

Keys are built by ModularousCacheService::generateCacheKey():

{prefix}:{ModuleName}:{RouteName}:{type}:{params_hash}
SegmentExampleSource
prefixmodularousconfig('modularous.cache.prefix')
ModuleNamePressReleaseStudlyCase module name
RouteNamePressReleasePaymentStudlyCase route / entity basename
typecounts, index, formattedItem:42Cache type (+ optional record id in specifier)
params_hasha1b2c3…md5(serialize(normalizedParams)) or default

User-aware caching: When user_aware is true and the repository uses scopes like CreatorTrait or AssignmentTrait, _user is added to params before hashing:

{prefix}:{module}:{route}:{type}:{hash_with_user_context}

Relation tags (for granular invalidation) use a separate tag namespace:

{prefix}:rel:{ModelBasename}:{id}

Example: modularous:rel:Company:5 — flushed when Company id 5 is updated.

Trait and Class Map ​

ArtifactLocationRole
ModularousCacheServicesrc/Services/ModularousCacheService.phpDriver connection, remember, tags, TTL resolution, stats
Cacheablesrc/Traits/Cache/Cacheable.phpshouldUseCache, rememberCache, module/route name resolution
CacheKeyGeneratorssrc/Traits/Cache/CacheKeyGenerators.phpType-specific specifier keys (count:all, formattedItem:42)
HasUserAwareCachesrc/Traits/Cache/HasUserAwareCache.phpPer-user cache keys when repository traits add user scopes
CacheableTraitsrc/Repositories/Logic/CacheableTrait.phpRepository caching: counts, index, record, relation tagging
CacheableResponsesrc/Http/Controllers/Traits/CacheableResponse.phpController caching: formItem, relation extraction
WarmupCachesrc/Traits/Cache/WarmupCache.phpPost-invalidation and artisan warm helpers
HasCachingsrc/Entities/Traits/Core/HasCaching.phpRegisters CacheObserver on the model
CacheObserversrc/Entities/Observers/CacheObserver.phpInvalidates on create/update/delete/restore/forceDelete

Layer Responsibilities ​

LayerWhat is cachedTypical producer
Repositorycounts, index, recordCountBuilders, QueryBuilder::getPaginator, getByIdCached
ControllerformItem, formattedItemFormPageUtility::getFormItem, TableItem::getFormattedIndexItem
ConfigTTL, per-route type togglesconfig/merges/cache.php + app cache.modules
ObserverInvalidation onlyCacheObserver on models with HasCaching

Graceful Degradation ​

ModularousCacheService pings Redis on boot. If the connection fails, isEnabled() returns false for all operations — queries run against the database with no exception thrown. This keeps admin usable when cache infrastructure is down.

See Also ​