yared/laravel-smart-cache
Composer 安装命令:
composer require yared/laravel-smart-cache
包简介
Automatic cache invalidation for Laravel — clear cache when models change, no manual work
README 文档
README
Automatic cache invalidation for Laravel — clear cache when models change, no manual work.
The Problem
In Laravel, developers write cache like this:
Cache::remember('products', 3600, function () { return Product::all(); });
But when a product changes:
Product::create([...]); Product::update([...]); Product::delete();
The cache still contains old data. Developers must manually clear it:
Cache::forget('products');
This becomes messy and error-prone.
The Solution
Smart Cache automatically clears cache when related models change.
use SmartCache; // Register the dependency SmartCache::watch(Product::class, 'products'); // Use it SmartCache::remember('products', function () { return Product::all(); });
Now when Product::create(), Product::update(), or Product::delete() runs, the cache is automatically cleared. No manual work.
Installation
composer require yared/laravel-smart-cache
Publish config (optional):
php artisan vendor:publish --tag=smart-cache-config
Quick Start
1. Register model → cache mapping
use SmartCache; SmartCache::watch(Product::class, 'products');
Or watch multiple keys:
SmartCache::watch(Product::class, ['products', 'homepage_products']);
2. Use SmartCache instead of Cache
$products = SmartCache::remember('products', function () { return Product::all(); });
That's it. When any Product is created, updated, deleted, or restored, the products cache is automatically invalidated.
Core Features
Automatic Model Watching
The package listens to Laravel model events:
createdupdateddeletedrestored
Cache Dependency Mapping
Map one model to multiple cache keys:
SmartCache::watch(Product::class, [ 'products', 'homepage_products', 'featured_products', ]);
Cache Tag Support (Redis / Memcached)
For tagged caches:
SmartCache::tags(['products']) ->watch(Product::class) ->remember('list', fn () => Product::all());
When a product changes, the entire products tag is flushed.
Smart Query Hash Cache
Automatically generate cache key from query:
$products = SmartCache::query( Product::where('category_id', 1)->where('price', '>', 100) );
The cache key is generated from the query hash. When any Product changes, related query caches are invalidated.
CacheableModel Trait
Define cache keys directly on your model:
use Yared\SmartCache\Traits\CacheableModel; class Product extends Model { use CacheableModel; public static function cacheKeys(): array { return ['products', 'homepage_products']; } }
Configuration
Publish the config file:
php artisan vendor:publish --tag=smart-cache-config
Config-based mappings
Define mappings in config/smart-cache.php:
'mappings' => [ 'App\Models\Product' => ['products', 'homepage_products'], 'App\Models\Category' => ['categories', 'menu_categories'], ],
Debug mode
Enable to log cache hits, misses, and invalidations:
SMART_CACHE_DEBUG=true
Dashboard
Enable the cache monitoring dashboard:
SMART_CACHE_DASHBOARD=true
Then visit /cache-monitor to see:
- Cache hits / misses / hit rate
- Invalidations
- Model → cache mappings
Advanced Usage
Custom TTL
SmartCache::remember('products', fn () => Product::all(), 7200); // 2 hours
Manual invalidation
SmartCache::forget('products');
Invalidate by model (programmatic)
SmartCache::invalidateForModel(Product::class, 'Manual refresh');
Package Structure
laravel-smart-cache/
├── src/
│ ├── Services/
│ │ ├── CacheManager.php
│ │ ├── CacheWatcher.php
│ │ └── TaggedCacheManager.php
│ ├── Listeners/
│ │ └── ModelEventListener.php
│ ├── Debug/
│ │ └── CacheDebugger.php
│ ├── Facades/
│ │ └── SmartCache.php
│ ├── Traits/
│ │ └── CacheableModel.php
│ └── SmartCacheServiceProvider.php
├── config/
│ └── smart-cache.php
└── README.md
Requirements
- PHP 8.2+
- Laravel 10.x, 11.x, or 12.x
- Illuminate Cache (Redis/Memcached for tag support)
License
MIT
yared/laravel-smart-cache 适用场景与选型建议
yared/laravel-smart-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「laravel」 「model-events」 「smart-cache」 「cache-invalidation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yared/laravel-smart-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yared/laravel-smart-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yared/laravel-smart-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Laravel 5 - Repositories to the database layer
Package to add userstamps to models.
Automatically track the user who created, updated, or deleted Eloquent models in Laravel, with database macros and model traits.
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15