定制 shammaa/laravel-intelligent-cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

shammaa/laravel-intelligent-cache

最新稳定版本:v1.0.9

Composer 安装命令:

composer require shammaa/laravel-intelligent-cache

包简介

An intelligent response caching package for Laravel with automatic invalidation and smart headers.

README 文档

README

Laravel Intelligent Cache is a high-performance, context-aware response caching package. It is designed to significantly boost your application's speed and optimize SEO by intelligently managing Cache-Control headers and providing automatic cache invalidation.

🌟 Main Objectives

  1. Boost Speed: Achieve near-zero TTFB (Time to First Byte) by caching full HTML responses.
  2. Improve SEO: Automatically fix the no-cache issue that hurts Google rankings by providing proper caching headers.
  3. Always Fresh: Keep content updated automatically. When you add a new article, the cache refreshes itself.

🛠 Installation

You can install the package via composer:

composer require shammaa/laravel-intelligent-cache

Publish Config

php artisan vendor:publish --provider="Shammaa\IntelligentCache\IntelligentCacheServiceProvider"

🚀 How to Use

1. The "Smart" Part (Auto-Update)

Add the HasSmartCache trait to any Model (like Article.php). This will automatically clear the cache whenever a new item is added or updated.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Shammaa\IntelligentCache\Traits\HasSmartCache;

class Article extends Model
{
    use HasSmartCache;
}

2. Speed Up Your Pages

Option A: Global Registration (Recommended)

To solve the no-cache issue site-wide, you must register the middleware at the beginning of the stack (Outermost). This ensures it overrides any headers set by other middlewares (like Session).

Laravel 11 (bootstrap/app.php):

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(prepend: [ // Use PREPEND here
        \Shammaa\IntelligentCache\Http\Middleware\CacheResponse::class,
    ]);
})

Laravel 10 & 9 (app/Http/Kernel.php): Add it at the TOP of your web middleware group:

protected $middlewareGroups = [
    'web' => [
        \Shammaa\IntelligentCache\Http\Middleware\CacheResponse::class, // Put it at the top
        // ... other middlewares
    ],
];

Option B: Route Specific

Alternatively, apply it to specific routes using the automatically registered alias smart_cache:

Route::middleware(['smart_cache'])->group(function () {
    Route::get('/articles', [ArticleController::class, 'index']);
});

⚙️ Configuration

You can find the settings in config/intelligent-cache.php:

  • lifetime: How long to keep the cache (in seconds).
  • exclude: URL patterns that should never be cached (like /admin/*).
  • headers: Custom cache rules for browsers and Google.

📈 Performance Impact

  • LCP (Largest Contentful Paint): Faster server responses lead to better user experience scores.
  • TTFB (Time to First Byte): Eliminates database overhead for repeat visitors.

License

The MIT License (MIT). Created with ❤️ by Shammaa.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固