Download the PHP package lunzai/laravel-cache-dependency without Composer
On this page you can find all versions of the php package lunzai/laravel-cache-dependency. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lunzai/laravel-cache-dependency
More information about lunzai/laravel-cache-dependency
Files in lunzai/laravel-cache-dependency
Package laravel-cache-dependency
Short Description Dependency-based caching for Laravel with tag and database dependencies inspired by Yii2
License MIT
Homepage https://github.com/lunzai/laravel-cache-dependency
Informations about the package laravel-cache-dependency
Laravel Cache Dependency
A dependency-based caching system for Laravel inspired by Yii2. Features tag dependencies with O(1) invalidation and database dependencies for automatic cache freshness.
Why This Package?
Laravel's built-in cache tags have fundamental limitations:
- Retrieval requires exact tag match — You must provide the exact same tags used when caching
- Limited driver support — Only works with Redis and Memcached
- No database dependencies — No automatic invalidation when data changes
This package solves all three problems.
Features
- Tag Dependencies: Tags stored as metadata, retrieval without tags, O(1) invalidation via version counters
- Database Dependencies: Automatic cache invalidation when query results change
- Universal Driver Support: Works with all Laravel cache drivers (file, array, database, Redis, Memcached)
- Cache Interoperability: Works seamlessly with Laravel's Cache facade
- Configurable Behavior: Fail-open or fail-closed when database queries fail
Installation
Optionally publish the config:
Requirements
- PHP 8.2+
- Laravel 11+
Quick Start
Tag Dependencies
How It Works
Tags are stored as metadata with the cached entry. A version counter is maintained for each tag. When you invalidate a tag, the counter increments. On retrieval, cached entries check if their stored tag versions match current versions. If any tag version has increased, the cache is stale.
This is an O(1) operation — no iteration over cached items needed.
Usage
Database Dependencies
How It Works
When caching, you provide a SQL query. The query result is stored as a "baseline". On retrieval, the query is re-executed and compared to the baseline. If they differ, the cache is stale.
Usage
Failure Handling
When a database query fails during cache retrieval:
- Fail Closed (default): Treat as cache miss, fetch fresh data
- Fail Open: Return cached value (prioritize availability)
Configure in config/cache-dependency.php:
Combined Dependencies
You can combine tags and database dependencies:
Tag Design Patterns
Since the package uses explicit tagging (no wildcards), proper tag design is essential:
RBAC / Permission System
E-commerce / Product Catalog
Multi-tenant Application
Tag Naming Conventions
| Pattern | Use Case | Example |
|---|---|---|
entity.{id} |
All caches for a specific entity | user.123, product.456 |
entity.{id}.aspect |
Specific aspect of an entity | user.123.permissions |
entity.aspect |
All entities' aspect caches | user.permissions |
domain |
Domain-wide caches | rbac, products, settings |
parent.{id}.children |
Parent-child relationships | category.5.products |
Configuration
Publish the configuration file:
Available options:
API Reference
CacheDependency Facade
PendingDependency Methods
Testing
Credits
- HL (Lunzai)
- Inspired by Yii2's Cache Dependencies
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-cache-dependency with dependencies
illuminate/cache Version ^11.0|^12.0
illuminate/contracts Version ^11.0|^12.0
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0