Skip to content

CacheInvalidation ​

File: src/Services/Concerns/CacheInvalidation.php
Uses: CacheTags, ModularModel, WarmupCache

CacheInvalidation provides all cache-clearing operations. Methods range from coarse (flush an entire module) to fine-grained (flush caches tagged with a specific model record).

Methods ​

MethodSignatureDescription
invalidateModuleinvalidateModule(string $moduleName): boolFlush all caches for a module (all routes)
invalidateModuleRouteinvalidateModuleRoute(string $module, string $route): boolFlush all caches for one route within a module
invalidateByRelatedModelinvalidateByRelatedModel(string $modelClass, $id): boolFlush only caches tagged with rel:{Model}:{id}
invalidateByRelatedModelsinvalidateByRelatedModels(array $relations): intBulk version; returns count of tags flushed
invalidateByPatterninvalidateByPattern(string $pattern): intRedis SCAN pattern delete (no-tags fallback only)
invalidateCountCachesinvalidateCountCaches(string $module, string $route, bool $onlyRoute): voidFlush count-type caches for a route
invalidateIndexCachesinvalidateIndexCaches(string $module, string $route, bool $onlyRoute): voidFlush index/listing caches for a route
invalidateFormattedItemCacheinvalidateFormattedItemCache(string $module, string $route, $id): voidFlush formatted-item cache for one record
invalidateFormItemCacheinvalidateFormItemCache(string $module, string $route, $id): voidFlush form-item cache for one record
invalidateForModelinvalidateForModel(Model $model, array $types, array $options): voidFull model invalidation — flushes all relevant cache types and optionally warms up new caches

invalidateForModel Detail ​

This is the primary entry point called from model observers. It:

  1. Resolves $moduleName and $moduleRouteName from the model via getModuleNameFromModel() / getModuleRouteNameFromModel().
  2. With tags: flushes the route tag entirely.
  3. Without tags: selectively invalidates counts, index, formattedItem, formItem by pattern, only if each type is enabled and the model is not newly created.
  4. Optionally calls warmupByModel($model) after invalidation (controlled by $options['warmup'], default true).

Tag vs Pattern Behaviour ​

Store typeInvalidation
Redis / Memcached (tags supported)Cache::tags([...])->flush() — fast, atomic
File / database (no tags)invalidateByPattern() via Redis SCAN — only works when the cache driver is Redis without tags

invalidateByPattern() logs a warning if called while tags are enabled, because tagged cache keys have a hashed namespace prefix that makes pattern matching impossible.

Example ​

php
// After saving an Order model
app(ModularousCacheService::class)->invalidateForModel($order);

// Selectively — only invalidate index and count caches
app(ModularousCacheService::class)->invalidateForModel($order, [
    'index'  => true,
    'counts' => true,
    'formattedItem' => false,
    'formItem'      => false,
], ['warmup' => false]);

// Granular — flush only caches that referenced Company:5
app(ModularousCacheService::class)->invalidateByRelatedModel(Company::class, 5);