承接 yared/laravel-smart-cache 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Latest Version License

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:

  • created
  • updated
  • deleted
  • restored

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 yared/laravel-smart-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-15