|
| 1 | +# Dependency-aware cache for Laravel |
| 2 | + |
| 3 | +[](https://packagist.org/packages/craftcms/laravel-dependency-aware-cache) |
| 4 | +[](https://github.com/craftcms/laravel-dependency-aware-cache/actions?query=workflow%3Arun-tests+branch%3Amain) |
| 5 | +[](https://github.com/craftcms/laravel-dependency-aware-cache/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
| 6 | +[](https://packagist.org/packages/craftcms/laravel-dependency-aware-cache) |
| 7 | + |
| 8 | +An implementation of [Yii's cache with dependencies](https://github.com/yiisoft/cache) for Laravel |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +You can install the package via composer: |
| 13 | + |
| 14 | +```bash |
| 15 | +composer require craftcms/laravel-dependency-aware-cache |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +The dependency aware repository is hooked up automatically and will be returned when using the `Cache` Facade. |
| 21 | + |
| 22 | +This package also provides a `\Craft\DependencyAwareCache\Facades\DependencyCache` Facade which extends the default `Cache` Facade but provides updated docblocks. |
| 23 | + |
| 24 | +```php |
| 25 | +use Illuminate\Support\Facades\Cache; |
| 26 | +use Craft\DependencyAwareCache\Facades\DependencyCache; |
| 27 | + |
| 28 | +/** @var DependencyAwareRepository $cache */ |
| 29 | +$cache = Cache::store(); |
| 30 | + |
| 31 | +/** @var DependencyAwareRepository $cache */ |
| 32 | +$cache = DependencyCache::store(); |
| 33 | +``` |
| 34 | + |
| 35 | +### Invalidation |
| 36 | + |
| 37 | +When using the cache, you can specify a dependency that may trigger cache invalidation. Below is an example using the tag dependency: |
| 38 | + |
| 39 | +```php |
| 40 | +use Craft\DependencyAwareCache\Dependency\TagDependency; |
| 41 | +use Illuminate\Support\Facades\Cache; |
| 42 | + |
| 43 | +Cache::put('item_42_price', 10, null, new TagDependency('item_42')); |
| 44 | +Cache::put('item_42_total', 100, null, new TagDependency('item_42')); |
| 45 | + |
| 46 | +Cache::get('item_42_price'); // 10 |
| 47 | +Cache::get('item_42_total'); // 100 |
| 48 | + |
| 49 | +TagDependency::invalidate('item_42'); |
| 50 | + |
| 51 | +Cache::get('item_42_price'); // null |
| 52 | +Cache::get('item_42_total'); // null |
| 53 | +``` |
| 54 | + |
| 55 | +Other dependencies: |
| 56 | + |
| 57 | +- `Craft\DependencyAwareCache\Dependency\CallbackDependency` - invalidates when the result of a callback changes. |
| 58 | +- `Craft\DependencyAwareCache\Dependency\FileDependency` - invalidates the cache based on file modification time. |
| 59 | +- `Craft\DependencyAwareCache\Dependency\ValueDependency` - invalidates the cache when specified value changes. |
| 60 | + |
| 61 | +- You may combine multiple dependencies using `Craft\DependencyAwareCache\Dependency\AnyDependency` or `Craft\DependencyAwareCache\Dependency\AllDependencies`. |
| 62 | + |
| 63 | +You can implement your own dependency by extending `Craft\DependencyAwareCache\Dependency\Dependency`. |
| 64 | + |
| 65 | +## Testing |
| 66 | + |
| 67 | +```bash |
| 68 | +composer test |
| 69 | +``` |
| 70 | + |
| 71 | +## Changelog |
| 72 | + |
| 73 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 74 | + |
| 75 | +## Contributing |
| 76 | + |
| 77 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 78 | + |
| 79 | +## Security Vulnerabilities |
| 80 | + |
| 81 | +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. |
| 82 | + |
| 83 | +## Credits |
| 84 | + |
| 85 | +- [Pixel & Tonic](https://github.com/craftcms) |
| 86 | +- [Yii Software](https://www.yiiframework.com/) |
| 87 | +- [All Contributors](../../contributors) |
| 88 | + |
| 89 | +## License |
| 90 | + |
| 91 | +Please see [License File](LICENSE.md) for more information. |
0 commit comments