PHP code example of iperamuna / laravel-html-fragment-cache
1. Go to this page and download the library: Download iperamuna/laravel-html-fragment-cache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iperamuna / laravel-html-fragment-cache example snippets
// 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,
],
];
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]);
}
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]);
}
}
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
);
}
}
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]);
}
}
// Forget by identifier
FragmentCache::forget("customer:{$customerId}", variant: 'products_widget_html');
// Forget with version
FragmentCache::forget("customer:{$customerId}", variant: 'products_widget_html', version: 'v1');
// Custom resolver
class CustomIdentifierResolver implements IdentifierResolver
{
public function resolve(?object $context = null): ?string
{
// Your custom logic here
return 'custom:identifier';
}
}