yared/laravel-smart-cache 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱: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

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固