承接 iperamuna/laravel-html-fragment-cache 相关项目开发

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

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

iperamuna/laravel-html-fragment-cache

Composer 安装命令:

composer require iperamuna/laravel-html-fragment-cache

包简介

A lightweight, generic HTML fragment caching package for Laravel. Cache any rendered HTML by identifier with support for all Laravel cache drivers, Blade directives, and simple invalidation.

README 文档

README

Latest Version on Packagist Total Downloads License

A lightweight, generic HTML fragment caching package for Laravel. Cache any rendered HTML by identifier (customer/org/user/etc.) with support for all Laravel cache drivers, Blade directives, and simple invalidation.

Features

  • 🚀 Laravel 10/11/12 compatible
  • 💾 Universal cache support - Works with Redis, Memcached, Database, File, Array, DynamoDB, and Octane
  • 🎯 Identifier-based caching - Cache by customer, organization, user, or any identifier
  • 🏷️ Blade directive - Simple @fragmentCache directive for quick caching
  • Performance optimized - Skip database queries and Blade rendering on cache hits
  • 🔧 Configurable cache store - Use any cache store from your Laravel configuration
  • 🎨 Clean API - Simple facade and trait-based usage
  • 🛠️ Artisan commands - Interactive commands for cache management

Installation

You can install the package via Composer:

composer require iperamuna/laravel-html-fragment-cache

The package will automatically register its service provider and facade.

Publish Configuration (Optional)

php artisan vendor:publish --provider="Iperamuna\HtmlFragmentCache\HtmlFragmentCacheServiceProvider" --tag=config

This will publish config/fragment-cache.php where you can customize the default settings.

Configuration

The package uses Laravel's cache system and supports all cache drivers. You can configure which cache store to use and enable/disable caching globally:

# In your .env file
FRAGMENT_CACHE_ENABLED=true
FRAGMENT_CACHE_STORE=redis
# or
FRAGMENT_CACHE_STORE=memcached
# or
FRAGMENT_CACHE_STORE=database
# etc.

Available Configuration Options

// config/fragment-cache.php
return [
    // Enable or disable fragment caching globally
    'enabled' => env('FRAGMENT_CACHE_ENABLED', true),
    
    // Cache store to use for fragment caching (must be one from config/cache.php stores)
    'cache_store' => env('FRAGMENT_CACHE_STORE', 'default'),
    
    // Default TTL for cached fragments
    'default_ttl' => env('FRAGMENT_CACHE_TTL', '6 hours'),
    
    // Default key parts
    'variant' => env('FRAGMENT_CACHE_VARIANT', 'html_fragment'),
    'version' => env('FRAGMENT_CACHE_VERSION', 'v1'),
    
    // Identifier resolution
    'identifier' => [
        'prefix' => env('FRAGMENT_CACHE_ID_PREFIX', ''),
        'sources' => [
            ['type' => 'property', 'path' => 'customer.id', 'label' => 'customer'],
            ['type' => 'property', 'path' => 'organization.id', 'label' => 'org'],
            ['type' => 'route', 'name' => 'customer', 'label' => 'customer'],
            ['type' => 'route', 'name' => 'organization', 'label' => 'org'],
        ],
        'resolver' => Iperamuna\HtmlFragmentCache\Resolvers\DefaultIdentifierResolver::class,
    ],
];

Enabling/Disabling Caching

You can globally enable or disable fragment caching using the enabled configuration option:

# Disable fragment caching (useful for development or debugging)
FRAGMENT_CACHE_ENABLED=false

# Enable fragment caching (default)
FRAGMENT_CACHE_ENABLED=true

When caching is disabled:

  • All rememberHtml() calls will execute the builder function directly without caching
  • forget() calls will do nothing
  • Blade directives will render content directly without caching
  • This is useful for development, debugging, or when you want to temporarily disable caching

Usage

Basic Usage with Facade

use FragmentCache;

public function show(Customer $customer)
{
    $html = FragmentCache::rememberHtml(
        identifier: "customer:{$customer->id}",
        builder: function () use ($customer) {
            $products = $customer->products()
                ->select('id','name','image_url','category')
                ->orderBy('name')
                ->get();

            return view('components.dashboard.products-widget', compact('products'))->render();
        },
        ttl: '6 hours',
        variant: 'products_widget_html',
        version: 'v1'
    );

    return view('dashboard.index', ['productsWidgetHtml' => $html]);
}

Blade Directive

The package provides a convenient Blade directive for quick caching:

@fragmentCache('customer:'.$customer->id)
    {{-- expensive HTML here --}}
    <div class="products-widget">
        @foreach($customer->products as $product)
            <div class="product-item">{{ $product->name }}</div>
        @endforeach
    </div>
@endFragmentCache

You can also pass an array as identifier to include additional context:

@fragmentCache([$customer->id, app()->getLocale(), 'v2'])
    {{-- HTML with locale and version context --}}
@endFragmentCache

Using the Trait

For more complex scenarios, use the UsesHtmlFragmentCache trait:

use Iperamuna\HtmlFragmentCache\Concerns\UsesHtmlFragmentCache;

class ProductController extends Controller
{
    use UsesHtmlFragmentCache;

    public function show(Customer $customer)
    {
        $html = $this->cacheFragmentHtml(
            identifier: "customer:{$customer->id}",
            builder: function () use ($customer) {
                return view('components.products-widget', [
                    'products' => $customer->products()->orderBy('name')->get()
                ])->render();
            },
            variant: 'products_widget',
            version: 'v1'
        );

        return view('dashboard.index', ['productsWidgetHtml' => $html]);
    }
}

Livewire Integration

Perfect for caching expensive Livewire component rendering:

Using the Trait (Recommended)

<?php

namespace App\Livewire\Dashboard;

use App\Models\Product;
use Illuminate\Support\HtmlString;
use Iperamuna\HtmlFragmentCache\Concerns\Livewire\CachesRenderedHtml;
use Livewire\Component;
use Livewire\Livewire;

class ActiveProducts extends Component
{
    use CachesRenderedHtml;

    public function render()
    {
        // This will short-circuit on cache hit. The closure only runs on a cache MISS.
        return $this->renderCached(function () {
            // Query (only on cache miss)
            $products = Product::all();

            // Render each row ONCE into HTML (only when building cache)
            $items = $products->map(
                fn ($p) => Livewire::mount('dashboard.active-product', ['product' => $p])
            )->implode('');

            // Render your dashboard-section wrapper and inject the items into the slot
            return view('components.dashboard-section', [
                'title' => 'Active Products',
                'url'   => url('/dashboard/products'),
            ])->with('slot', new HtmlString($items));
        },
            // optional overrides (you can omit these to fully rely on config defaults)
            identifier: 'organization:'. auth()->user()->current_organization_id,                       // null → resolve via config (customer/org)
            variant: 'dashboard_active_products',   // bucket
            version: 'v1',                          // bump on markup change
            ttl: '6 hours'                          // or int/DateInterval
        );
    }
}

Using the Facade

<?php

namespace App\Livewire\Dashboard;

use Livewire\Component;
use FragmentCache;

class ActiveProducts extends Component
{
    public string $identifier; // e.g., 'customer:123'

    public function mount(string $identifier): void
    {
        $this->identifier = $identifier;
    }

    public function render()
    {
        $html = FragmentCache::rememberHtml(
            identifier: $this->identifier,
            builder: function () {
                $products = \App\Models\Product::query()
                    ->where('is_active', true)
                    ->orderBy('name')
                    ->get();

                $items = $products->map(function ($product) {
                    return \Livewire\Livewire::mount('dashboard.active-product', ['product' => $product])->html();
                })->implode('');

                return view('livewire.dashboard.active-products', ['itemsHtml' => $items])->render();
            },
            ttl: '6 hours',
            variant: 'active_products_widget',
            version: 'v1'
        );

        return view('livewire.dashboard.active-products-cached', ['html' => $html]);
    }
}

Cache Invalidation

Forget Specific Fragments

// Forget by identifier
FragmentCache::forget("customer:{$customerId}", variant: 'products_widget_html');

// Forget with version
FragmentCache::forget("customer:{$customerId}", variant: 'products_widget_html', version: 'v1');

Artisan Commands

The package provides interactive Artisan commands for cache management:

# Interactive cache management
php artisan fragment-cache:forget

The fragment-cache:forget command provides three modes:

  • Forget by Identifier: Remove specific cached fragments
  • Forget by Pattern: Clear fragments matching a pattern (flushes entire cache store)
  • Flush All: Clear all fragments from the configured cache store

Advanced Usage

Custom Identifier Resolution

You can customize how identifiers are resolved by modifying the configuration or creating a custom resolver:

// Custom resolver
class CustomIdentifierResolver implements IdentifierResolver
{
    public function resolve(?object $context = null): ?string
    {
        // Your custom logic here
        return 'custom:identifier';
    }
}

Multiple Cache Stores

You can use different cache stores for different environments:

# Development
FRAGMENT_CACHE_STORE=array

# Staging
FRAGMENT_CACHE_STORE=file

# Production
FRAGMENT_CACHE_STORE=redis

Performance Benefits

  • Skip Database Queries: Cache the final rendered HTML, not just database results
  • Skip Blade Rendering: Avoid expensive view compilation on cache hits
  • Universal Compatibility: Works with any Laravel cache driver
  • Memory Efficient: Uses Laravel's built-in cache system

Testing

composer test

The test suite uses Orchestra Testbench with the array cache driver for portability.

Requirements

  • PHP 8.2+
  • Laravel 10.0+ | 11.0+ | 12.0+
  • Laravel Prompts (for interactive commands)

Cache Driver Support

Driver Support Notes
Array ✅ Full Perfect for testing
File ✅ Full Good for single-server setups
Database ✅ Full Persistent across servers
Redis ✅ Full High performance, recommended for production
Memcached ✅ Full High performance, in-memory
DynamoDB ✅ Full AWS managed cache
Octane ✅ Full Ultra-high performance

Contributing

Please see CONTRIBUTING.md for details.

Security

If you discover any security related issues, please email iperamuna@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

Made with ❤️ for the Laravel community

iperamuna/laravel-html-fragment-cache 适用场景与选型建议

iperamuna/laravel-html-fragment-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「cache」 「html」 「laravel」 「blade」 「fragment」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 iperamuna/laravel-html-fragment-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 iperamuna/laravel-html-fragment-cache 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-15