craftcms/laravel-dependency-aware-cache

A dependency aware cache repository for Laravel

1.0.5 2025-09-04 14:00 UTC

This package is auto-updated.

Last update: 2025-09-22 16:52:41 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

An implementation of Yii's cache with dependencies for Laravel

Installation

You can install the package via composer:

composer require craftcms/laravel-dependency-aware-cache

Usage

The dependency aware repository is hooked up automatically and will be returned when using the Cache Facade.

This package also provides a \CraftCms\DependencyAwareCache\Facades\DependencyCache Facade which extends the default Cache Facade but provides updated docblocks.

use Illuminate\Support\Facades\Cache;
use CraftCms\DependencyAwareCache\DependencyAwareRepository;
use CraftCms\DependencyAwareCache\Facades\DependencyCache;

/** @var DependencyAwareRepository $cache */
$cache = Cache::store();

/** @var DependencyAwareRepository $cache */
$cache = DependencyCache::store();

Invalidation

When using the cache, you can specify a dependency that may trigger cache invalidation. Below is an example using the tag dependency:

use CraftCms\DependencyAwareCache\Dependency\TagDependency;
use Illuminate\Support\Facades\Cache;

Cache::put('item_42_price', 10, null, new TagDependency('item_42'));
Cache::put('item_42_total', 100, null, new TagDependency('item_42'));

Cache::get('item_42_price'); // 10
Cache::get('item_42_total'); // 100

TagDependency::invalidate('item_42');

Cache::get('item_42_price'); // null
Cache::get('item_42_total'); // null

Other dependencies:

  • CallbackDependency - invalidates when the result of a callback changes.

  • FileDependency - invalidates the cache based on file modification time.

  • ValueDependency - invalidates the cache when specified value changes.

  • You may combine multiple dependencies using AnyDependency or AllDependencies.

You can implement your own dependency by extending Dependency.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

Please see License File for more information.