Skip to content

CacheHelpers ​

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

CacheHelpers is the primary trait consumed by ModularousCacheService. It wraps standard Laravel cache operations (remember, get, put, forget, flush) with tag awareness — automatically applying the correct tag set when the cache store supports tags, and falling back to untagged operations otherwise.

Methods ​

MethodSignatureDescription
rememberremember(string $key, int $ttl, Closure $cb, ?string $module, ?string $route)Tag-aware Cache::remember()
rememberForeverrememberForever(string $key, Closure $cb, ?string $module, ?string $route)Tag-aware Cache::rememberForever()
rememberWithRelationsrememberWithRelations(string $key, int $ttl, Closure $cb, ?string $module, ?string $route, array $relations)remember + adds relation tags for granular per-record invalidation
getget(string $key, $default, ?string $module, ?string $route)Tag-aware Cache::get()
putput(string $key, $value, int $ttl, ?string $module, ?string $route): boolTag-aware Cache::put()
putWithRelationsputWithRelations(string $key, $value, int $ttl, ?string $module, ?string $route, array $relations): boolput + relation tags
hashas(string $key, ?string $module, ?string $route): boolTag-aware existence check
forgetforget(string $key, ?string $module, ?string $route): boolTag-aware key deletion (uses onlyRoute: true tag for narrow scope)
flushflush(): boolFlush all modularous caches (by global prefix tag, or pattern fallback)

Tag Selection Logic ​

Each method applies tags as follows when the store supports them and $module is provided:

$module$routeTags used
setsetgetModuleRouteTags($module, $route)
setnullgetModuleTags($module)
null—No tags (plain store call)

isEnabled Guard ​

Every method checks isEnabled($module, $route) first. If caching is disabled for the given scope, remember / rememberForever / rememberWithRelations call the callback directly and return its value; get returns $default; put / putWithRelations return false.

Example ​

php
// In a repository
$result = $cacheService->remember(
    key: $cacheKey,
    ttl: 3600,
    callback: fn() => $this->model->with('tags')->paginate(),
    moduleName: 'Orders',
    moduleRouteName: 'order'
);

// With relation tags for granular invalidation
$result = $cacheService->rememberWithRelations(
    key: $cacheKey,
    ttl: 3600,
    callback: fn() => Order::with('company')->find($id),
    moduleName: 'Orders',
    moduleRouteName: 'order',
    relations: ['Company' => $order->company_id]
);